'use strict';

var vue = require('vue');
var pinia = require('pinia');
var core = require('@vueuse/core');
var index = require('../../base/useIDBKeyval/index.js');
var objects = require('../../../utils/src/objects.js');
require('../../../api/guardBasic/controller/index.js');
var postBasicV1ConfigFrontListCustom = require('../../../api/guardBasic/controller/JiChuFuWu/QianDuanPeiZhiJieKou/postBasicV1ConfigFrontListCustom.js');
var getBasicV1ConfigFrontListCustom = require('../../../api/guardBasic/controller/JiChuFuWu/QianDuanPeiZhiJieKou/getBasicV1ConfigFrontListCustom.js');

const useCustomSettingsStore = pinia.defineStore("gmCustomSettings", () => {
  const { data: customSettings, initIndexedDBComplete } = index.useIDBKeyval("gmCustomSettingsStore", {}, { deep: true });
  const customResult = core.createEventHook();
  const isLoadingKeyWords = vue.ref(/* @__PURE__ */ new Set());
  const addCustomSettings = async (params) => {
    const res = await postBasicV1ConfigFrontListCustom.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.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 (objects.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] && objects.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 (!objects.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
  };
});

exports.useCustomSettingsStore = useCustomSettingsStore;
//# sourceMappingURL=index.js.map
