import Bluebird from 'bluebird'
import {AuthError} from 'modules/lib'
import {extractStudentIds} from './student-ids'
import {COURSES_URL} from './urls'
import {fetchHtml, getText} from './lib'
import {selectOne} from 'css-select'

export function checkIfLoggedIn() {
	if (typeof window !== 'undefined' && window.location.hostname !== 'www.stolaf.edu') {
		return Bluebird.reject(new AuthError('Wrong domain. Student import will only work under the www.stolaf.edu domain.'))
	}
	return fetchHtml(COURSES_URL).then(response => {
		let errorMsg = selectOne('.sis-error', response)
		let badMsg = 'Sorry, your session has timed out; please login again.'
		if (errorMsg && getText(errorMsg) === badMsg) {
			throw new AuthError('Not logged in. Please log into the SIS in another tab, then try again.')
		}
		else if (errorMsg) {
			throw new Error(errorMsg)
		}
		return extractStudentIds(response)
	})
}

@@ -262,16 +262,19 @@
 In() %7B%0A%09
+// 
 if (type
@@ -302,62 +302,16 @@
 ned'
- && window.location.hostname !== 'www.stolaf.edu'
 ) %7B%0A%09
+// 
 %09ret
@@ -426,16 +426,19 @@
 in.'))%0A%09
+// 
 %7D%0A%09retur
@@ -483,16 +483,19 @@
  =%3E %7B%0A%09%09
+// 
 let erro
@@ -537,16 +537,19 @@
 onse)%0A%09%09
+// 
 let badM
@@ -612,16 +612,19 @@
 ain.'%0A%09%09
+// 
 if (erro
@@ -657,32 +657,35 @@
 === badMsg) %7B%0A%09%09
+// 
 %09throw new AuthE
@@ -765,20 +765,26 @@
 in.')%0A%09%09
+// 
 %7D%0A%09%09
+// 
 else if 
@@ -798,16 +798,19 @@
 sg) %7B%0A%09%09
+// 
 %09throw n
@@ -830,16 +830,19 @@
 rMsg)%0A%09%09
+// 
 %7D%0A%09%09retu

disable the login checks in logged-in