"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("../axios/index.js");
const datasource = require("./datasource.js");
const env = require("./env.js");
const baseUrl = env.default.api_base_url;
const getOptions = ({
  ctx,
  dataSources,
  formData = [],
  fields = [],
  optionSource,
  options: staticOptions,
  dataSourceValue,
  displayField,
  valueField,
  dictionaryValue
}) => {
  return new Promise(async (resolve, reject) => {
    var _a, _b;
    switch (optionSource) {
      case "custom":
        resolve(staticOptions);
        break;
      case "dictionary":
        if (!dictionaryValue) {
          resolve([]);
          break;
        }
        const response = await ctx.$http.postForm(
          "/base-server/parm/sysParmDic/getDicListByKey",
          {
            keyName: dictionaryValue
          },
          false
        );
        const dictionaryData = response;
        const dictItems = dictionaryData[dictionaryValue];
        if (dictItems) {
          const labelField2 = "optName";
          const keyField2 = "optCode";
          const options2 = dictItems.map((item) => ({
            label: item[labelField2],
            value: item[keyField2],
            disabled: item.disabled || false
          }));
          resolve(options2);
        } else {
          resolve([]);
        }
        break;
      case "dataSource":
        const dsManager = datasource.createDataSource({
          http: ctx.$http,
          dataSources,
          currentDataSourceId: dataSourceValue,
          data: formData,
          // fields: [...fieldsData.value, ...hiddenFields.value],
          fields
        });
        const data = await dsManager.fetch();
        let options = [];
        if (Array.isArray(data)) {
          options = data;
        } else if (typeof data === "object") {
          options = Array.isArray(data.data) ? data.data : ((_a = data.data) == null ? void 0 : _a.records) || ((_b = data.data) == null ? void 0 : _b.list) || [];
        }
        const labelField = displayField || "label";
        const keyField = valueField || "value";
        options = options.map((item) => ({
          label: item[labelField],
          value: item[keyField],
          disabled: item.disabled || false,
          ...item
        }));
        resolve(options);
        break;
    }
  });
};
const getTreeOptions = ({
  optionType,
  dataSource,
  dataSourceParams,
  staticOptions,
  optionsFieldNames
}) => {
  return new Promise((resolve, reject) => {
    switch (optionType) {
      case "static":
        resolve(staticOptions);
      case "library":
        const dataSourceRef = useDataSource(dataSource);
        dataSourceRef == null ? void 0 : dataSourceRef.load(dataSourceParams || {}).then((res) => {
          var _a;
          const data = ((_a = res == null ? void 0 : res.data) == null ? void 0 : _a.records) || (res == null ? void 0 : res.data) || res || [];
          resolve(data);
        }).catch((err) => {
          console.error(err);
        });
        break;
    }
  });
};
const getParmDicMapByKeys = async (dicKeys) => {
  const http = index.default();
  const res = await http.postForm(
    `${baseUrl}/parm/sysParmDic/getDicListByKey`,
    {
      keyName: dicKeys
    }
  );
  return res.data;
};
exports.getOptions = getOptions;
exports.getParmDicMapByKeys = getParmDicMapByKeys;
exports.getTreeOptions = getTreeOptions;
