"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const cloneDeep$1 = require("../../../../node_modules/lodash-es/cloneDeep.js");
const isEqual$1 = require("../../../../node_modules/lodash-es/isEqual.js");
function cloneDeep(data) {
  return cloneDeep$1.default(data);
}
function isEqual(data1, data2) {
  return isEqual$1.default(data1, data2);
}
function removeEmptyValues(data) {
  if (typeof data !== "object" || data === null) {
    return data;
  }
  if (Array.isArray(data)) {
    return data.map((item) => removeEmptyValues(item)).filter((item) => {
      return item !== null && item !== void 0 && item !== "";
    });
  }
  const result = {};
  for (const key in data) {
    if (Object.prototype.hasOwnProperty.call(data, key)) {
      const value = removeEmptyValues(data[key]);
      if (value !== null && value !== void 0 && value !== "") {
        result[key] = value;
      }
    }
  }
  return result;
}
const handleSearchData = (filterValues, completeFilterFields) => {
  const filters = {};
  Object.keys(filterValues).forEach((key) => {
    const value = filterValues[key];
    if (value !== "" && value !== null && value !== void 0) {
      const fieldConfig = completeFilterFields.find((field) => field.id == key);
      const fieldName = (fieldConfig == null ? void 0 : fieldConfig.fieldName) || key;
      const maxKey = `${key}_max`;
      const maxValue = filterValues[maxKey];
      if (maxValue !== "" && maxValue !== null && maxValue !== void 0) {
        filters[fieldName] = value + "|" + maxValue;
      } else if (key.endsWith("_max")) {
        return;
      } else {
        if (Array.isArray(value)) {
          filters[fieldName] = value.join("|");
        } else {
          filters[fieldName] = value;
        }
      }
    }
  });
  return filters;
};
const reverseHandleSearchData = (filters, completeFilterFields) => {
  const filterValues = {};
  if (!filters) {
    return filterValues;
  }
  Object.keys(filters).forEach((key) => {
    const value = filters[key];
    if (value === "" || value === null || value === void 0) {
      return;
    }
    const fieldConfig = completeFilterFields.find((field) => field.fieldName === key);
    const fieldId = (fieldConfig == null ? void 0 : fieldConfig.id) || key;
    if (key.endsWith("_max")) {
      return;
    }
    const maxKey = `${key}_max`;
    const maxValue = filters[maxKey];
    if (maxValue !== "" && maxValue !== null && maxValue !== void 0) {
      filterValues[fieldId] = isNaN(Number(value)) ? value : Number(value);
      filterValues[`${fieldId}_max`] = isNaN(Number(maxValue)) ? maxValue : Number(maxValue);
    } else if (typeof value === "string" && value.includes("|")) {
      filterValues[fieldId] = value.split("|");
    } else {
      filterValues[fieldId] = value;
    }
  });
  return filterValues;
};
const setFormDataByFieldMapping = (value, multiple, options, fieldMapping, formData, fields) => {
  let selectedOptions = [];
  if (multiple) {
    selectedOptions = options.filter((opt) => value.includes(opt.value));
  } else {
    selectedOptions = options.filter((opt) => opt.value == value);
  }
  if (fieldMapping && Array.isArray(fieldMapping)) {
    fieldMapping.forEach((item) => {
      const field = fields.find((field2) => field2.id === item.field);
      if (field) {
        const values = selectedOptions.map((row) => row[item.value]);
        formData.value[field.fieldName] = values.join(",");
      }
    });
  }
};
const loadComponentByPath = (path, router, currentComponent) => {
  const route = router == null ? void 0 : router.getRoutes().find((item) => item.path === path);
  if (!route) return;
  const comp = route.components.default;
  if (!comp) return;
  if (typeof comp === "function") {
    comp().then((module2) => {
      currentComponent.value = module2.default;
    });
  } else {
    currentComponent.value = comp;
  }
};
exports.cloneDeep = cloneDeep;
exports.handleSearchData = handleSearchData;
exports.isEqual = isEqual;
exports.loadComponentByPath = loadComponentByPath;
exports.removeEmptyValues = removeEmptyValues;
exports.reverseHandleSearchData = reverseHandleSearchData;
exports.setFormDataByFieldMapping = setFormDataByFieldMapping;
