const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_index = require("../filter/components/FilterButton/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
//#region src/components/pisellFilter/index.tsx
const _excluded = [
	"value",
	"quickFilterMaxLength",
	"formFiltersPrefix",
	"hiddenQuickFilter",
	"hiddenOtherFilter",
	"hasForm",
	"onChange",
	"size"
];
const PisellFilter = (props) => {
	const { value: originalValue, quickFilterMaxLength, formFiltersPrefix, hiddenQuickFilter = true, hiddenOtherFilter = false, hasForm = true, onChange, size } = props, otherProps = require_objectWithoutProperties._objectWithoutProperties(props, _excluded);
	const [form] = antd.Form.useForm();
	const [value, setValue] = (0, react.useState)(() => ({
		quickFilter: (originalValue === null || originalValue === void 0 ? void 0 : originalValue.quickFilter) || [],
		otherFilter: (originalValue === null || originalValue === void 0 ? void 0 : originalValue.otherFilter) || [],
		values: (originalValue === null || originalValue === void 0 ? void 0 : originalValue.values) || {}
	}));
	const isInternalChange = (0, react.useRef)(false);
	(0, react.useEffect)(() => {
		setValue((prev) => {
			var _originalValue$quickF, _originalValue$otherF, _ref, _originalValue$values;
			return {
				quickFilter: (_originalValue$quickF = originalValue === null || originalValue === void 0 ? void 0 : originalValue.quickFilter) !== null && _originalValue$quickF !== void 0 ? _originalValue$quickF : prev.quickFilter,
				otherFilter: (_originalValue$otherF = originalValue === null || originalValue === void 0 ? void 0 : originalValue.otherFilter) !== null && _originalValue$otherF !== void 0 ? _originalValue$otherF : prev.otherFilter,
				values: (_ref = (_originalValue$values = originalValue === null || originalValue === void 0 ? void 0 : originalValue.values) !== null && _originalValue$values !== void 0 ? _originalValue$values : prev.values) !== null && _ref !== void 0 ? _ref : {}
			};
		});
	}, [
		originalValue === null || originalValue === void 0 ? void 0 : originalValue.quickFilter,
		originalValue === null || originalValue === void 0 ? void 0 : originalValue.otherFilter,
		originalValue === null || originalValue === void 0 ? void 0 : originalValue.values
	]);
	(0, react.useEffect)(() => {
		var _originalValue$values2;
		if (!hasForm || typeof (originalValue === null || originalValue === void 0 ? void 0 : originalValue.values) !== "object") return;
		if (isInternalChange.current) {
			isInternalChange.current = false;
			return;
		}
		const nextValues = (_originalValue$values2 = originalValue === null || originalValue === void 0 ? void 0 : originalValue.values) !== null && _originalValue$values2 !== void 0 ? _originalValue$values2 : {};
		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 || originalValue === void 0 ? void 0 : originalValue.values
	]);
	const handleChange = (0, react.useCallback)((newVal) => {
		setValue((prev) => {
			var _newVal$values;
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), newVal), {}, { values: (_newVal$values = newVal.values) !== null && _newVal$values !== void 0 ? _newVal$values : prev.values });
		});
		onChange === null || onChange === void 0 || onChange(newVal);
	}, [onChange]);
	const handleFormValuesChange = (0, react.useCallback)((_, allValues) => {
		if (typeof allValues !== "object" || allValues === null) return;
		isInternalChange.current = true;
		const mergedValues = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, value.values), allValues);
		const next = {
			quickFilter: value.quickFilter,
			otherFilter: value.otherFilter,
			values: mergedValues
		};
		setValue(next);
		onChange === null || onChange === void 0 || onChange(next);
	}, [
		value.quickFilter,
		value.otherFilter,
		value.values,
		onChange
	]);
	const content = (0, react.useMemo)(() => {
		return /* @__PURE__ */ react.default.createElement(require_index.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, otherProps), {}, {
			size,
			onChange: handleChange,
			value: {
				quickFilter: value.quickFilter,
				otherFilter: value.otherFilter
			},
			hiddenQuickFilter,
			hiddenOtherFilter,
			quickFilterMaxLength: quickFilterMaxLength || 100,
			formFiltersPrefix: formFiltersPrefix || ""
		}));
	}, [
		value,
		size,
		hiddenQuickFilter,
		hiddenOtherFilter,
		quickFilterMaxLength,
		formFiltersPrefix,
		handleChange
	]);
	if (hasForm) return /* @__PURE__ */ react.default.createElement(antd.Form, require_objectSpread2._objectSpread2({
		form,
		size: "large",
		layout: "vertical",
		initialValues: value.values,
		onValuesChange: handleFormValuesChange
	}, otherProps), content);
	return content;
};
//#endregion
exports.default = PisellFilter;
