import { defineStore } from 'pinia';
import '../utils/index.mjs';
import watermark from '../utils/watermark.mjs';
import { useThemeConfig } from './themeConfig.mjs';
import '../api/index.mjs';
import { useSysApi } from '../api/sys/index.mjs';
import { Local, Session } from '../utils/storage.mjs';

const useUserInfo = defineStore("userInfo", {
  state: () => ({
    userInfos: {},
    // 用户信息
    constList: [],
    // 常量列表
    dictList: {},
    // 字典列表
    userTableList: []
    // 用户表格字段列表
  }),
  getters: {
    // 获取当前用户信息
    getUserInfos: (state) => state.userInfos,
    // 获取当前用户公司ID
    getCmpId: (state) => {
      return Number(state.userInfos.cmpId > 0 ? state.userInfos.cmpId : state.userInfos.tenantId);
    },
    // 获取当前用户租户ID
    getTenantId: (state) => {
      return Number(state.userInfos.tenantId);
    }
  },
  actions: {
    // 存储用户信息到浏览器缓存
    async setUserInfos() {
      var _a;
      this.userInfos = (_a = Session.get("userInfo")) != null ? _a : await this.getApiUserInfo();
    },
    // 存储常量信息到浏览器缓存
    async setConstList() {
      var _a;
      this.constList = (_a = Session.get("constList")) != null ? _a : await this.getSysConstList();
      if (!Session.get("constList")) Session.set("constList", this.constList);
    },
    // 存储字典信息到浏览器缓存
    async setDictList() {
      var dictList = await useSysApi().getAllDictList().then((res) => {
        var _a;
        return (_a = res.data.result) != null ? _a : {};
      });
      var dictListTemp = JSON.parse(JSON.stringify(dictList));
      await Promise.all(
        Object.keys(dictList).map(async (key) => {
          dictList[key].forEach((da, index) => {
            setDictLangMessageAsync(dictListTemp[key][index]);
          });
        })
      );
      this.dictList = dictListTemp;
    },
    // 存储用户表格列到浏览器缓存
    async setUserTables() {
      var _a;
      this.userTableList = (_a = Session.get("userTable")) != null ? _a : await this.getUserTableList();
      if (!Session.get("userTable")) Session.set("userTable", this.userTableList);
    },
    // 获取当前用户信息
    getApiUserInfo() {
      return new Promise((resolve) => {
        useSysApi().getUserInfo().then((res) => {
          var _a, _b;
          if (res.data.result == null) return;
          var d = res.data.result;
          const userInfos = {
            id: d.id,
            tenantId: d.tenantId,
            account: d.account,
            realName: d.realName,
            phone: d.phone,
            idCardNum: d.idCardNum,
            email: d.email,
            accountType: d.accountType,
            avatar: (_a = d.avatar) != null ? _a : "/upload/logo.png",
            address: d.address,
            signature: d.signature,
            orgId: d.orgId,
            orgName: d.orgName,
            cmpId: d.cmpId,
            depId: d.depId,
            groupId: d.groupId,
            posName: d.posName,
            posId: d.posId,
            roles: d.roles,
            powers: d.powers,
            limits: d.limit,
            formDes: d.formDes,
            authApiList: d.apis,
            lastChangePasswordTime: d.lastChangePasswordTime,
            time: (/* @__PURE__ */ new Date()).getTime()
          };
          const storesThemeConfig = useThemeConfig();
          storesThemeConfig.themeConfig.watermarkText = (_b = d.watermarkText) != null ? _b : "";
          if (storesThemeConfig.themeConfig.isWatermark) watermark.set(storesThemeConfig.themeConfig.watermarkText);
          else watermark.del();
          Local.remove("themeConfig");
          Local.set("themeConfig", storesThemeConfig.themeConfig);
          resolve(userInfos);
        });
      });
    },
    // 获取当前用户表格列集合
    getUserTableList() {
      return new Promise((resolve) => {
        useSysApi().getUserRoleTableList().then(async (res) => {
          var _a;
          resolve((_a = res.data.result) != null ? _a : []);
        });
      });
    },
    // 获取常量集合
    getSysConstList() {
      return new Promise((resolve) => {
        useSysApi().getConstList().then(async (res) => {
          var _a;
          resolve((_a = res.data.result) != null ? _a : []);
        });
      });
    },
    // 根据常量类名获取常量数据
    getConstDataByTypeCode(typeCode) {
      var _a, _b;
      return ((_b = (_a = this.constList.find((item) => item.code === typeCode)) == null ? void 0 : _a.data) == null ? void 0 : _b.result) || [];
    },
    // 根据常量类名和编码获取常量值
    getConstItemNameByType(typeCode, itemCode) {
      var _a;
      const data = this.getConstDataByTypeCode(typeCode);
      return (_a = data.find((item) => item.code === itemCode)) == null ? void 0 : _a.name;
    },
    // 常量编码和名称转换
    codeToName(code, type) {
      var _a;
      return (_a = this.constList.find((x) => x.code === type).data.result.find((x) => x.code === code)) == null ? void 0 : _a.name;
    },
    // 根据字典类型获取字典数据
    getDictDataByCode(dictTypeCode) {
      return this.dictList[dictTypeCode] || [];
    },
    // 根据字典类型和值获取取字典项
    getDictItemByValue(dictTypeCode, value) {
      if (value != void 0 && value !== "") {
        const _value = value.toString();
        const ds = this.dictList[dictTypeCode] || [];
        for (const element of ds) {
          if (element.value == _value) {
            return element;
          }
        }
      }
      return {};
    },
    // 根据字典类型和名称获取取字典项
    getDictItemByLabel(dictTypeCode, label) {
      if (label != void 0 && label !== "") {
        const _label = label.toString();
        const ds = this.dictList[dictTypeCode] || [];
        for (const element of ds) {
          if (element.label == _label) {
            return element;
          }
        }
      }
      return {};
    }
  }
});
const setDictLangMessageAsync = async (dict) => {
};

export { useUserInfo };
