import { reactive } from 'vue';
import { useRoute } from 'vue-router';
import { ls } from '@/utils/local-storage';
import { cloneDeep } from 'lodash-es';
// import store from '@/store';
// import { localStorage } from '@/utils/local-storage';

// 基座应用穿过来的参数
export let baseParams = reactive({
  userInfo: undefined,
  token: '',
  lang: '',
  tiemZone: '',
});
try {
  if (window.$wujie) {
    baseParams = window.$wujie?.props;
  }
} catch (error) {
  console.log('参数错误');
}

// 获取路由相关的参数
export const getParams = (data, path) => {
  if (Array.isArray(data)) {
    for (let i = 0; i < data.length; i++) {
      if (data[i]?.path == path) {
        return data[i];
      }
      if (data[i]?.children && data[i]?.children.length) {
        const val = getParams(data[i].children, path);
        if (val) return val;
      }
    }
  } else {
    if (data?.path == path) {
      return data;
    }
    if (data?.children && data?.children.length) {
      const val = getParams(data.children, path);
      if (val) return val;
    }
  }
};
// 获取路由、个人信息等参数
export const getInfo = config => {
  // 如果有基座应用，取基座应用的userinfo
  if (baseParams && baseParams.userInfo) {
    return baseParams.userInfo;
  }
  const route = useRoute();
  const router = window.sessionStorage.getItem('HomeAdminRouter');
  const routerMenu = router ? JSON.parse(router) : [];
  // const USER_INFO = JSON.parse(ls.get('USER_INFO') || '{}');
  const USER_INFO = ls.get('USER_INFO') || {};

  const handelUserInfo = cloneDeep(USER_INFO);
  handelUserInfo.project = config.project
    ? config.project
    : USER_INFO.productLine
    ? USER_INFO.productLine
    : '';
  handelUserInfo.connect = config?.connect
    ? config.connect
    : USER_INFO.connect
    ? USER_INFO.connect
    : '';
  // TODO: 暂时添加一些登录相关的信息
  const userInfo = {
    companyId: 'P_SYS_COMPANY_155f6a331e804b69a6fc47e6273699fc',
    orgId: 'P_SYS_COMPANY_155f6a331e804b69a6fc47e6273699fc',
    userDept: 'P_SYS_DEPT_21a0a27ac5de49609028dcfe8976a5ca',
    userGroup: 'P_SYS_COMPANY_155f6a331e804b69a6fc47e6273699fc',
    userId: 'P_SYS_USER_99abfffd40da4d6eb66dc692d3f815fc',
    project: 'PLMBASE',
    //jjf临时修改
    projectCode: 'PLMBASE',
    productLine: 'PLMBASE,PLM_PRODUCT,PLMPRODUCT',
    timeZone: 8,
    connect: 'MYSQL',
    user_type: '1',
    TIME_ZONE: 8,
    lang: 'zhs',
    user_id: 'P_SYS_USER_99abfffd40da4d6eb66dc692d3f815fc',
  };
  const params = route && getParams(routerMenu, route.path);
  if (USER_INFO.user_id) {
    return {
      ...handelUserInfo,
      ...(params?.extra || params),
      TIME_ZONE: window.localStorage.getItem('TIME_ZONE'),
      lang: window.localStorage.getItem('lang'),
    };
  } else {
    // TODO: 暂时添加一些登录相关的信息给单独启动的子应用使用
    const userInfo = {
      companyId: 'P_SYS_COMPANY_155f6a331e804b69a6fc47e6273699fc',
      orgId: 'P_SYS_COMPANY_155f6a331e804b69a6fc47e6273699fc',
      userDept: 'P_SYS_DEPT_21a0a27ac5de49609028dcfe8976a5ca',
      userGroup: 'P_SYS_COMPANY_155f6a331e804b69a6fc47e6273699fc',
      userId: 'P_SYS_USER_99abfffd40da4d6eb66dc692d3f815fc',
      project: 'PLMBASE',
      projectCode: 'PLMBASE',
      productLine: 'PLM_PROJECT;baicsrm;PLM_CAPP;PLMBASE;PLMPRODUCT;loxamom',
      timeZone: 8,
      connect: 'MYSQL',
      user_type: '1',
      TIME_ZONE: 8,
      lang: 'zhs',
      user_id: 'P_SYS_USER_99abfffd40da4d6eb66dc692d3f815fc',
    };
    return {
      ...handelUserInfo,
      ...(params?.extra || params),
      ...userInfo,
      TIME_ZONE: window.localStorage.getItem('TIME_ZONE'),
      lang: window.localStorage.getItem('lang'),
    };
  }
};
//平台参数配置
export const ICP_CONFIG = {
  project: 'PLMBASE,PLM_PRODUCT,PLMPRODUCT',
  connect: 'MYSQL',
};
export const ALL_CONFIG = {
  project: 'PLMBASE;',
};
