import { defineStore } from 'pinia';
import { reactive } from 'vue';
import '../api/index.mjs';
import { useBaseApi } from '../api/base/index.mjs';

const usefminputdropdownstore = defineStore("fminputdropdownstore", () => {
  const state = reactive({
    optionsData: {}
  });
  const getOptionsData = async (apiService, apiAction, basecode, reload = false, multibasecode = []) => {
    var _a, _b;
    if (!reload) {
      if (state.optionsData[basecode] && state.optionsData[basecode].length > 0) {
        return state.optionsData[basecode];
      }
    }
    const res = await useBaseApi(apiService).get(null, apiAction + "/?codetype=" + basecode + "&mcodes=" + multibasecode.join(","));
    if (multibasecode.length > 0) {
      const mdata = (_a = res.data.result) != null ? _a : [];
      multibasecode.forEach((p) => {
        const tempdata = mdata.filter((x) => {
          return x.code == p;
        });
        state.optionsData[p] = tempdata != null ? tempdata : [];
      });
    } else {
      return state.optionsData[basecode] = (_b = res.data.result) != null ? _b : [];
    }
  };
  return {
    state,
    getOptionsData
  };
});

export { usefminputdropdownstore };
