var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/dataSourceComponents/dataSourceTable/filter/utils.tsx
var utils_exports = {};
__export(utils_exports, {
  field2Cpt: () => field2Cpt,
  genFilterItems: () => genFilterItems,
  genGetField: () => genGetField,
  getStoredFilterOrder: () => getStoredFilterOrder,
  saveFilterOrder: () => saveFilterOrder
});
module.exports = __toCommonJS(utils_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_fields = require("../../fields");
var genGetField = (dataSource) => {
  const { fields = [] } = dataSource || {};
  const fieldMap = fields.reduce((acc, field) => {
    acc[field.name] = field;
    return acc;
  }, {});
  return (key) => {
    return fieldMap[key];
  };
};
var fieldType2FilterMap = {
  Checkbox: "FormItemSelect",
  ColorPicker: "FormItemInput",
  DatePicker: "FormItemDateRangePicker",
  Input: "FormItemInput",
  Radio: "FormItemSelect",
  Select: "FormItemSelect",
  Percent: "FormItemPercent",
  InputNumber: "FormItemInputNumber",
  "Input.JSON": "FormItemInput",
  "Input.Email": "FormItemInput",
  "Input.URL": "FormItemInput",
  "Input.Password": "FormItemInput",
  "Radio.Group": "FormItemSelect",
  "Checkbox.Group": "FormItemSelect",
  "Translation": "FormItemTranslation"
};
var getCptProps = (field, cptType, item) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
  let props = ((_a = field == null ? void 0 : field.uiSchema) == null ? void 0 : _a["x-component-props"]) || {};
  props.style = {
    ...(props == null ? void 0 : props.style) || {},
    width: "200px",
    margin: 0
  };
  props.placeholder = item.label;
  props.allowClear = true;
  if (cptType === "FormItemSelect") {
    props.mode = "multiple";
    props.showSearch = true;
    props.optionFilterProp = "label";
  }
  if (["Select", "Radio.Group", "Checkbox.Group"].includes(
    (_b = field == null ? void 0 : field.uiSchema) == null ? void 0 : _b["x-component"]
  ) && (((_d = (_c = field == null ? void 0 : field.uiSchema) == null ? void 0 : _c.enum) == null ? void 0 : _d.length) || 0) > 0) {
    if (["Radio.Group", "Checkbox.Group"].includes(
      (_e = field == null ? void 0 : field.uiSchema) == null ? void 0 : _e["x-component"]
    )) {
      props.direction = "horizontal";
    } else {
      props.showSearch = true;
      props.optionFilterProp = "label";
    }
    props.options = (_f = field == null ? void 0 : field.uiSchema) == null ? void 0 : _f.enum;
  }
  if (((_g = field == null ? void 0 : field.uiSchema) == null ? void 0 : _g["x-component"]) === "ColorPicker") {
    props.defaultValue = (_h = field == null ? void 0 : field.uiSchema) == null ? void 0 : _h.default;
  }
  if (cptType === "DateRangePicker") {
    props.style = {
      ...(props == null ? void 0 : props.style) || {},
      width: "240px"
    };
  }
  if (((_i = field == null ? void 0 : field.uiSchema) == null ? void 0 : _i["x-component"]) === "Checkbox" && cptType === "Select") {
    props.mode = void 0;
    props.allowClear = true;
    props.options = [
      {
        label: "是",
        value: true
      },
      {
        label: "否",
        value: false
      }
    ];
  }
  props = {
    ...props,
    ...((_j = item == null ? void 0 : item.other) == null ? void 0 : _j.fieldProps) || {}
  };
  return props;
};
var field2Cpt = (field, item, otherProps) => {
  const { isCustom, type: configType } = item;
  const { uiSchema } = field || {};
  const { "x-component": xComponent } = uiSchema || {};
  let cptType = fieldType2FilterMap[xComponent];
  if (isCustom) {
    cptType = configType;
  }
  const Cpt = import_fields.formFieldMap[cptType];
  if (!Cpt) {
    return null;
  }
  const props = getCptProps(field, cptType, item);
  const style = (0, import_utils.isFunction)(otherProps == null ? void 0 : otherProps.style) ? otherProps == null ? void 0 : otherProps.style(props.style) : (otherProps == null ? void 0 : otherProps.style) ? { ...props.style, ...otherProps == null ? void 0 : otherProps.style } : props.style;
  return /* @__PURE__ */ import_react.default.createElement(
    Cpt,
    {
      ...props,
      label: item.label,
      name: item.name || item.value,
      ...otherProps,
      style
    }
  );
};
var genFilterItems = (list, dataSource) => {
  return list.map((item) => {
    if (!item) {
      return null;
    }
    const { name } = item;
    const getField = genGetField(dataSource);
    const field = getField(name);
    const cptDom = field2Cpt(field, item);
    return cptDom;
  });
};
var FILTER_STORAGE_KEY = "filter-setting-";
var getStorageKey = (componentId) => `${FILTER_STORAGE_KEY}${componentId}`;
var getStoredFilterOrder = (componentId) => {
  try {
    const stored = localStorage.getItem(getStorageKey(componentId));
    return stored ? JSON.parse(stored) : null;
  } catch (e) {
    console.error("Failed to parse stored filter order:", e);
    return null;
  }
};
var saveFilterOrder = (componentId, order) => {
  try {
    localStorage.setItem(getStorageKey(componentId), JSON.stringify(order));
  } catch (e) {
    console.error("Failed to save filter order:", e);
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  field2Cpt,
  genFilterItems,
  genGetField,
  getStoredFilterOrder,
  saveFilterOrder
});
