import { setCookie } from '@/H5Public/utils/cookie';
import { getUrlParams } from '@/H5Public/utils/handleString';

export function setLoginToUrl(url) {
  return localStorage.setItem('antd-pro-login-url', url);
}


export function clearToken() {
  localStorage.removeItem('antd-username');
  return localStorage.removeItem('antd-pro-token');
}

export function setToken(token) {
  setCookie('token', token);
  return localStorage.setItem('antd-pro-token', token);
}

export function getLoginToUrl() {
  return localStorage.getItem('antd-pro-login-url');
}

export function getToken() {
  let t = localStorage.getItem('antd-pro-token');
  let t2 = getUrlParams().token;
  if (t2) {
    setToken(t2);
  }
  if (!t || t.length < 10) {
    return t2 || '';
  }
  return localStorage.getItem('antd-pro-token');
}

export function isJSON(str) {
  if (typeof str === 'string') {
    try {
      JSON.parse(str);
      if (typeof JSON.parse(str) === 'number') {
        return false;
      }
      return true;
    } catch (e) {
      return false;
    }
  }
}

/**
 * 深拷贝函数
 * */
export function deepCopy(obj, parent = null) {
  if (['boolean', 'string', 'number'].indexOf(typeof obj) > -1 || !obj) {
    return obj;
  }
  let result;
  if (obj.constructor === Array) {
    result = [];
  } else {
    result = {};
  }
  let keys = Object.keys(obj),
    key = null,
    temp = null,
    _parent = parent;
  // 该字段有父级则需要追溯该字段的父级
  while (_parent) {
    // 如果该字段引用了它的父级则为循环引用
    if (_parent.originalParent === obj) {
      // 循环引用直接返回同级的新对象
      return _parent.currentParent;
    }
    _parent = _parent.parent;
  }
  for (let i = 0; i < keys.length; i++) {
    key = keys[i];
    temp = obj[key];
    // 如果字段的值也是一个对象
    if (temp && typeof temp === 'object') {
      // 递归执行深拷贝 将同级的待拷贝对象与新对象传递给 parent 方便追溯循环引用
      result[key] = deepCopy(temp, {
        originalParent: obj,
        currentParent: result,
        parent: parent,
      });

    } else {
      result[key] = temp;
    }
  }
  return result;
}

export function setUsername(username) {
  if (typeof username === 'object') {
    username = JSON.stringify(username);
  }
  localStorage.setItem('antd-username', username);
}

export function getUsername() {
  return localStorage.getItem('antd-username');
}

export function getIsA_Ba() { // 判断当前环境是不是阿坝学校 然后做定制需求. 主要用于定制开发
  return window.specialImportantSystemConfig?.schoolName && window.specialImportantSystemConfig?.schoolName.indexOf('阿坝') > -1;
}

export function getIsBei_Dian() {  // 判断当前环境是不是北电科学校 然后做定制需求. 主要用于定制开发
  if(window.specialImportantSystemConfig?.version === 'bdk'){
    return true;
  }
  return window.specialImportantSystemConfig?.schoolName && window.specialImportantSystemConfig?.schoolName.indexOf('北京电子科技') > -1;
}

export function getIsGui_Jian() {  // 判断当前环境是不是北电科学校 然后做定制需求. 主要用于定制开发
  return window.specialImportantSystemConfig?.schoolName && window.specialImportantSystemConfig?.schoolName.indexOf('贵州建设职业') > -1;
}

export function getIsQian_Nan(){
  return window.specialImportantSystemConfig?.schoolName && window.specialImportantSystemConfig?.schoolName.indexOf('黔南民族职业') > -1;
}
