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/pisellFilter/index.tsx
var pisellFilter_exports = {};
__export(pisellFilter_exports, {
  default: () => pisellFilter_default
});
module.exports = __toCommonJS(pisellFilter_exports);
var import_react = __toESM(require("react"));
var import_FilterButton = __toESM(require("../filter/components/FilterButton"));
var import_antd = require("antd");
var PisellFilter = (props) => {
  const {
    value: originalValue,
    quickFilterMaxLength,
    formFiltersPrefix,
    hiddenQuickFilter = true,
    hiddenOtherFilter = false,
    hasForm = true,
    onChange,
    /**
     * 筛选触发按钮尺寸；手机抽屉（phoneLayout）内 Form 固定 small，桌面弹层仍为 large。
     */
    size,
    phoneLayout,
    ...otherProps
  } = props;
  const formSize = phoneLayout ? "small" : "large";
  const [form] = import_antd.Form.useForm();
  const [value, setValue] = (0, import_react.useState)(() => ({
    quickFilter: (originalValue == null ? void 0 : originalValue.quickFilter) || [],
    otherFilter: (originalValue == null ? void 0 : originalValue.otherFilter) || [],
    values: (originalValue == null ? void 0 : originalValue.values) || {}
  }));
  const isInternalChange = (0, import_react.useRef)(false);
  (0, import_react.useEffect)(() => {
    setValue((prev) => ({
      quickFilter: (originalValue == null ? void 0 : originalValue.quickFilter) ?? prev.quickFilter,
      otherFilter: (originalValue == null ? void 0 : originalValue.otherFilter) ?? prev.otherFilter,
      values: (originalValue == null ? void 0 : originalValue.values) ?? prev.values ?? {}
    }));
  }, [
    originalValue == null ? void 0 : originalValue.quickFilter,
    originalValue == null ? void 0 : originalValue.otherFilter,
    originalValue == null ? void 0 : originalValue.values
  ]);
  (0, import_react.useEffect)(() => {
    if (!hasForm || typeof (originalValue == null ? void 0 : originalValue.values) !== "object") return;
    if (isInternalChange.current) {
      isInternalChange.current = false;
      return;
    }
    const nextValues = (originalValue == null ? void 0 : originalValue.values) ?? {};
    if (Object.keys(nextValues).length === 0) {
      const current = form.getFieldsValue(true);
      const cleared = Object.keys(current).reduce(
        (acc, k) => {
          acc[k] = void 0;
          return acc;
        },
        {}
      );
      if (Object.keys(cleared).length > 0) {
        form.setFieldsValue(cleared);
      }
    } else {
      form.setFieldsValue(nextValues);
    }
  }, [hasForm, form, originalValue == null ? void 0 : originalValue.values]);
  const handleChange = (0, import_react.useCallback)(
    (newVal) => {
      setValue((prev) => ({
        ...prev,
        ...newVal,
        values: newVal.values ?? prev.values
      }));
      onChange == null ? void 0 : onChange(newVal);
    },
    [onChange]
  );
  const handleFormValuesChange = (0, import_react.useCallback)(
    (_, allValues) => {
      if (typeof allValues !== "object" || allValues === null) return;
      isInternalChange.current = true;
      const mergedValues = { ...value.values, ...allValues };
      const next = {
        quickFilter: value.quickFilter,
        otherFilter: value.otherFilter,
        values: mergedValues
      };
      setValue(next);
      onChange == null ? void 0 : onChange(next);
    },
    [value.quickFilter, value.otherFilter, value.values, onChange]
  );
  const content = (0, import_react.useMemo)(() => {
    return /* @__PURE__ */ import_react.default.createElement(
      import_FilterButton.default,
      {
        ...otherProps,
        size,
        phoneLayout,
        onChange: handleChange,
        value: {
          quickFilter: value.quickFilter,
          otherFilter: value.otherFilter
        },
        hiddenQuickFilter,
        hiddenOtherFilter,
        quickFilterMaxLength: quickFilterMaxLength || 100,
        formFiltersPrefix: formFiltersPrefix || ""
      }
    );
  }, [
    value,
    size,
    phoneLayout,
    hiddenQuickFilter,
    hiddenOtherFilter,
    quickFilterMaxLength,
    formFiltersPrefix,
    handleChange
  ]);
  if (hasForm) {
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form,
      {
        form,
        size: formSize,
        layout: "vertical",
        initialValues: value.values,
        onValuesChange: handleFormValuesChange,
        ...otherProps
      },
      content
    );
  }
  return content;
};
var pisellFilter_default = PisellFilter;
