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

const usefminputdropdownstore = defineStore("fminputdropdownstore", () => {
  const state = reactive({
    optionsData: {}
  });
  const getOptionsData = async (apiService, apiAction, basecode, reload = false, multibasecode = []) => {
    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 = res.data.result ?? [];
      multibasecode.forEach((p) => {
        const tempdata = mdata.filter((x) => {
          return x.code == p;
        });
        state.optionsData[p] = tempdata ?? [];
      });
    } else {
      return state.optionsData[basecode] = res.data.result ?? [];
    }
  };
  return {
    state,
    getOptionsData
  };
});

export { usefminputdropdownstore };
