/**
 * 代码生成 store
 */
import { defineStore } from 'pinia';

export const useGeneratorStore = defineStore({
  id: 'generatorStore',
  state: () => ({
    // 当前选中的应用列表
    buinessSelectAppData:
      localStorage.getItem('BUSINESS_APP_DATA') != 'undefined' ? JSON.parse(localStorage.getItem('BUSINESS_APP_DATA')) ?? {} : {},
    // 单项目配置开关
    singleConfigSwitch: false,
    // 流程发起激活状态
    flowStartActiveTab: '1',
    // 前端代码 - 作者列表
    firstAuthorList: JSON.parse(localStorage.getItem('REAL_FIRST_AUTHOR_LIST')) ?? [],
    // 前端代码 - 表前缀移除列表
    firstRemoveTablePrefixList: JSON.parse(localStorage.getItem('REAL_FIRST_REMOVE_TABLE_PREFIX_LIST')) ?? [],
    // 后端代码 - 作者列表
    realAuthorList: JSON.parse(localStorage.getItem('REAL_END_AUTHOR_LIST')) ?? [],
    // 后端代码 - 项目包名列表
    realPackageNameList: JSON.parse(localStorage.getItem('REAL_END_PACKAGENAME_LIST')) ?? [],
    // 后端代码 - 表前缀移除列表
    realRemoveTablePrefixList: JSON.parse(localStorage.getItem('REAL_END_REMOVEtABLEPREFIX_LIST')) ?? [],
    // 业务生成 - 作者列表
    businessAuthorList: JSON.parse(localStorage.getItem('REAL_BUSINESS_AUTHOR_LIST')) ?? [],
    // 业务生成 - 项目包名列表
    businessPackageNameList: JSON.parse(localStorage.getItem('REAL_BUSINESS_PACKAGENAME_LIST')) ?? [],
    // 业务生成 - 表前缀移除列表
    businessRemoveTablePrefixList: JSON.parse(localStorage.getItem('REAL_BUSINESS_REMOVEtABLEPREFIX_LIST')) ?? [],
    // 接口调试信息
    apiDebugData: JSON.parse(localStorage.getItem('API_DEBUG_DATA') || '{}'),
    // 接口当前信息
    interfacingData: JSON.parse(localStorage.getItem('INTERFACING_DATA') || '{}'),
    // 接口模块当前信息
    interfaceModularData: JSON.parse(localStorage.getItem('INTERFACE_MODULAR_DATA') || '{}'),
    // 主机信息
    websocketData: {
      websocketRef: null,
      result: null,
    },
    useCaseHistory: JSON.parse(localStorage.getItem('USE_CASE_HISTORY')) ?? [],
    // 业务返回路径
    appBackUrl: localStorage.getItem('APP_BACK_URL') || '/mid/app/list',
  }),
  getters: {},
  actions: {
    // 设置选中的应用列表
    setBuinessSelectAppData(value) {
      localStorage.setItem('BUSINESS_APP_DATA', JSON.stringify(value));
      this.buinessSelectAppData = value;
    },

    // 设置流程发起激活状态
    setFlowStartActiveTab(value) {
      this.flowStartActiveTab = value;
    },

    // 设置前端、后端代码缓存列表
    setRealAuthorList(type, value) {
      localStorage.setItem(type, JSON.stringify(value));
      if (type == 'REAL_FIRST_AUTHOR_LIST') {
        this.firstAuthorList = value;
      } else if (type == 'REAL_FIRST_REMOVE_TABLE_PREFIX_LIST') {
        this.firstRemoveTablePrefixList = value;
      } else if (type == 'REAL_END_AUTHOR_LIST') {
        this.realAuthorList = value;
      } else if (type == 'REAL_END_PACKAGENAME_LIST') {
        this.realPackageNameList = value;
      } else if (type == 'REAL_END_REMOVEtABLEPREFIX_LIST') {
        this.realRemoveTablePrefixList = value;
      } else if (type == 'REAL_BUSINESS_AUTHOR_LIST') {
        this.businessAuthorList = value;
      } else if (type == 'REAL_BUSINESS_PACKAGENAME_LIST') {
        this.businessPackageNameList = value;
      } else if (type == 'REAL_BUSINESS_REMOVEtABLEPREFIX_LIST') {
        this.businessRemoveTablePrefixList = value;
      }
    },
    // 设置接口调试配置缓存
    setApiDebugData(value) {
      localStorage.setItem('API_DEBUG_DATA', JSON.stringify(value));
      this.apiDebugData = value;
    },
    // 设置接口当前信息
    setInterfacingData(value) {
      localStorage.setItem('INTERFACING_DATA', JSON.stringify(value));
      this.interfacingData = value;
    },
    // 设置接口模块当前信息
    setInterfaceModularData(value) {
      localStorage.setItem('INTERFACE_MODULAR_DATA', JSON.stringify(value));
      this.interfaceModularData = value;
    },
    // 设置主机连接
    setWebsocketData(value) {
      this.websocketData = value;
    },
    // 自动测试用例历史记录列表
    setUseCaseHistory(value) {
      localStorage.setItem('USE_CASE_HISTORY', JSON.stringify(value));
      this.useCaseHistory = value;
    },
    // 设置业务返回路径
    setAppBackUrl(value) {
      this.appBackUrl = value;
    },
  }
});
