import { IUser } from './user.d';
import axios from 'axios'
// #ifdef DEV
import dictionary from './user.store';
console.groupCollapsed('user');
console.log('port : ', location.port);
console.log(`dictionary[${location.port}] : `, dictionary[location.port]);
console.groupEnd();
localStorage.setItem(
  "userInfo",
  JSON.stringify(
    dictionary[location.port]
  )
)

// #endif

let user = reactive<IUser>(JSON.parse(localStorage.getItem('userInfo') as string))
console.log('user :>> ', user);
export default user

const ROLE_MAP: { [key: string]: 'teacher' | 'student' } = { '72': 'teacher', '75': 'teacher', '76': 'student' }
let TempUserRole: 'teacher' | 'student' | 'admin' | 'other' = user.sm_info.sm_id === '739' ? 'admin' : (ROLE_MAP[user.user_role_id] ?? 'other')

await axios.create({
  baseURL: window.GLOBAL_CONFIG.api,
  timeout: 6 * 1000,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  method: 'post',
})({
  url: '/dyaw/xlfw_pbgl/index',
  data: {
    token: user.token,
    client: 'web',
    site: 'dyaw',
    api: 'json',
    user_id: user.user_id
  }
}).then(resp => {
  const res = resp.data
  if (res.code === '1') {
    if (res.data.page_data?.length >= 1) {
      if (TempUserRole === 'admin')
        TempUserRole = 'teacher'
    }
  }
})


export const UserRole = TempUserRole

console.log('UserRole : ', UserRole)

// #ifdef DEV
document.title = `${UserRole.toLocaleUpperCase()}-${document.title}`
// #endif
