'use strict';

var pinia = require('pinia');
require('../utils/index.js');
var themeConfig = require('./themeConfig.js');
require('../api/index.js');
var index$4 = require('../locales/index.js');
var index = require('../api/sys/index.js');
var index$1 = require('../utils/watermark/index.js');
var index$2 = require('../utils/storage/index.js');
var index$3 = require('../utils/const/index.js');

const useUserInfo = pinia.defineStore("userInfo", {
  state: () => ({
    userInfos: {},
    // 用户信息
    constList: [],
    // 常量列表
    dictList: {},
    // 字典列表
    userTableList: []
    // 用户表格字段列表
  }),
  getters: {},
  actions: {
    // 存储用户信息到浏览器缓存
    async setUserInfos() {
      this.userInfos = index$2.Session.get("userInfo") ?? await this.getApiUserInfo();
    },
    // 存储常量信息到浏览器缓存
    async setConstList() {
      this.constList = index$2.Session.get("constList") ?? await this.getSysConstList();
      if (!index$2.Session.get("constList")) index$2.Session.set("constList", this.constList);
    },
    // 存储字典信息到浏览器缓存
    async setDictList() {
      var dictList = await index.useSysApi().getAllDictList().then((res) => res.data.result ?? {});
      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() {
      this.userTableList = index$2.Session.get(index$3.PUB.userTable) ?? await this.getUserTableList();
      if (!index$2.Session.get(index$3.PUB.userTable)) index$2.Session.set(index$3.PUB.userTable, this.userTableList);
    },
    // 获取当前用户信息
    getApiUserInfo() {
      return new Promise((resolve) => {
        index.useSysApi().getUserInfo().then((res) => {
          if (res.data.result == null) return;
          var d = res.data.result;
          const userInfos = {
            id: d.id,
            loginId: d.loginId,
            tenantId: d.tenantId,
            account: d.account,
            realName: d.realName,
            phone: d.phone,
            idCardNum: d.idCardNum,
            email: d.email,
            accountType: d.accountType,
            avatar: d.avatar ?? "/logo.png",
            address: d.address,
            signature: d.signature,
            orgId: d.orgId,
            orgName: d.orgName,
            posName: d.posName,
            posId: d.posId,
            roles: d.roleIds,
            authApiList: d.apis,
            lastChangePasswordTime: d.lastChangePasswordTime,
            time: (/* @__PURE__ */ new Date()).getTime()
          };
          const storesThemeConfig = themeConfig.useThemeConfig();
          storesThemeConfig.themeConfig.watermarkText = d.watermarkText ?? "";
          if (storesThemeConfig.themeConfig.isWatermark) index$1.Watermark.set(storesThemeConfig.themeConfig.watermarkText);
          else index$1.Watermark.del();
          index$2.Local.remove(index$3.PUB.themeConfig);
          index$2.Local.set(index$3.PUB.themeConfig, storesThemeConfig.themeConfig);
          resolve(userInfos);
        });
      });
    },
    // 获取当前用户表格列集合
    getUserTableList() {
      return new Promise((resolve) => {
        index.useSysApi().getUserRoleTableList().then(async (res) => {
          resolve(res.data.result ?? []);
        });
      });
    },
    // 获取常量集合
    getSysConstList() {
      return new Promise((resolve) => {
        index.useSysApi().getConstList().then(async (res) => {
          resolve(res.data.result ?? []);
        });
      });
    },
    // 根据常量类名获取常量数据
    getConstDataByTypeCode(typeCode) {
      return this.constList.find((item) => item.code === typeCode)?.data?.result || [];
    },
    // 根据常量类名和编码获取常量值
    getConstItemNameByType(typeCode, itemCode) {
      const data = this.getConstDataByTypeCode(typeCode);
      return data.find((item) => item.code === itemCode)?.name;
    },
    // 常量编码和名称转换
    codeToName(code, type) {
      return this.constList.find((x) => x.code === type).data.result.find((x) => x.code === code)?.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) => {
  return;
  dict.langMessage = `message.dictType.${dict.typeCode}_${dict.value}`;
  const text = index$4.i18n.global.t(dict.langMessage);
  dict.label = text !== dict.langMessage && !text.endsWith(`${dict.typeCode}_${dict.value}`) ? text : dict.label;
};

exports.useUserInfo = useUserInfo;
