import { defineStore } from 'pinia';

export const useSysStore = defineStore('sys', () => {
  const staticDictMap = {
    payment: {
      '0': '微信',
      '1': '支付宝',
      '2': '银行',
    },
    checkoff: {
      '0': '未对账',
      '1': '对账失败',
      '2': '对账成功',
    },
    source: {
      '0': '一路健康',
      '1': '吴门医述',
    },
    orderType: {
      '0': '充值',
      '1': 'vip',
      '2': '课',
      '3': '实物',
      '4': '培训班',
    },
  } as const;

  // 自动推断字典类型
  type DictType = keyof typeof staticDictMap;

  const getDictMap = (dictType: DictType, dictValue: number | string) => {
    const dict = staticDictMap[dictType];
    const key = String(dictValue);

    // 使用 Record 类型来避免索引签名问题
    const record = dict as Record<string, string>;
    return record[key] ?? '';
  };

  // 定义字典项类型
  type DictItem = {
    color?: string;
    label: string;
    value: string;
  };
  const staticDictList: Record<string, Array<DictItem>> = {
    payment: [
      {
        label: '微信',
        value: '0',
      },
      {
        label: '支付宝',
        value: '1',
      },
      {
        label: '银行',
        value: '2',
      },
    ],
    checkoff: [
      {
        label: '未对账',
        value: '0',
        color: 'warning',
      },
      {
        label: '对账失败',
        value: '1',
        color: 'error',
      },
      {
        label: '对账成功',
        value: '2',
        color: 'success',
      },
    ],
    source: [
      {
        label: '一路健康',
        value: '0',
      },
      {
        label: '吴门医述',
        value: '1',
      },
    ],
    orderType: [
      {
        label: '充值',
        value: '0',
      },
      {
        label: 'vip',
        value: '1',
      },
      {
        label: '课',
        value: '2',
      },
      {
        label: '实物',
        value: '3',
      },
      {
        label: '培训班',
        value: '4',
      },
    ],
  } as const;

  // 自动推断字典列表类型
  type DictListType = keyof typeof staticDictList;

  const getDictList = (dictType: DictListType) => {
    return staticDictList[dictType] ?? [];
  };

  const getDictByList = (dictType: DictListType, dictValue: number | string) => {
    const dictList = getDictList(dictType);
    const key = String(dictValue);

    // 确保返回的对象始终包含 color 属性
    return dictList.find((item: DictItem) => item.value === key) ?? { label: '', value: '' };
  };

  return {
    staticDictMap,
    staticDictList,
    getDictMap,
    getDictList,
    getDictByList,
    $reset: () => {}, // sys store 只包含静态数据，不需要重置
  };
});
