import { ref } from 'vue';
import { defineStore } from 'pinia';
import { createEventHook } from '@vueuse/core';
import { useIDBKeyval } from '../../base/useIDBKeyval/index.mjs';
import { hasOwn } from '../../../utils/src/objects.mjs';
import '../../../api/guardBasic/controller/index.mjs';
import { postBasicV1ConfigFrontListCustom } from '../../../api/guardBasic/controller/JiChuFuWu/QianDuanPeiZhiJieKou/postBasicV1ConfigFrontListCustom.mjs';
import { getBasicV1ConfigFrontListCustom } from '../../../api/guardBasic/controller/JiChuFuWu/QianDuanPeiZhiJieKou/getBasicV1ConfigFrontListCustom.mjs';

const useCustomSettingsStore = defineStore("gmCustomSettings", () => {
  const { data: customSettings, initIndexedDBComplete } = useIDBKeyval("gmCustomSettingsStore", {}, { deep: true });
  const customResult = createEventHook();
  const isLoadingKeyWords = ref(/* @__PURE__ */ new Set());
  const addCustomSettings = async (params) => {
    const res = await postBasicV1ConfigFrontListCustom(params);
    if (res.data.code === 200) {
      if (!customSettings.value[params.key_word]) {
        customSettings.value[params.key_word] = {};
      }
      let value = params.value;
      try {
        value = JSON.parse(params.value);
      } catch {
      }
      customSettings.value[params.key_word][params.type] = value;
    }
    return res;
  };
  const syncRemoteSetting = async (params) => {
    const { key_word, type } = params;
    isLoadingKeyWords.value.add(key_word);
    try {
      const { data } = await getBasicV1ConfigFrontListCustom({ key_word });
      if (data.code !== 200) {
        isLoadingKeyWords.value.delete(key_word);
        customResult.trigger({ key_word, type });
        return;
      }
      for (const key in data.data) {
        if (hasOwn(data.data, key)) {
          const value = data.data[key];
          try {
            data.data[key] = JSON.parse(value);
          } catch {
            data.data[key] = value;
          }
        }
      }
      customSettings.value[key_word] = Array.isArray(data.data) ? {} : data.data;
      isLoadingKeyWords.value.delete(key_word);
      customResult.trigger({ key_word, type });
    } catch (e) {
      isLoadingKeyWords.value.delete(key_word);
      customResult.trigger({ key_word, type });
      throw e;
    }
  };
  const getCustomList = async (params) => {
    const { key_word, type } = params;
    await initIndexedDBComplete();
    if (customSettings.value[key_word] && hasOwn(customSettings.value[key_word], type)) {
      customResult.trigger({ key_word, type });
      return;
    }
    if (isLoadingKeyWords.value.has(key_word)) {
      await onCustomResultPromise({ key_word, type });
      return;
    }
    await syncRemoteSetting(params);
  };
  const onCustomResultPromise = (params) => new Promise((resolve) => {
    customResult.on((value) => {
      if (params.key_word === value.key_word) {
        resolve(value);
      }
    });
  });
  const getCustomSettings = async (params, options) => {
    await getCustomList(params);
    if (!customSettings.value[params.key_word]) {
      return;
    }
    if (!hasOwn(customSettings.value[params.key_word], params.type)) {
      customSettings.value[params.key_word][params.type] = options.defaultValue;
    }
    return customSettings.value[params.key_word][params.type];
  };
  return {
    addCustomSettings,
    getCustomSettings,
    syncRemoteSetting,
    customSettings
  };
});

export { useCustomSettingsStore };
//# sourceMappingURL=index.mjs.map
