import { getText } from "../../../../locales/index.js";
import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import Dropdown$1 from "../Dropdown/index.js";
import { isMobile as isMobile$1 } from "../../../../utils/platform.js";
import React, { useMemo, useRef, useState } from "react";
import { locales } from "@pisell/utils";
import { Button, Popover } from "antd";
import FilterLines from "@pisell/icon/es/FilterLines";
//#region src/components/filter/components/FilterButton/index.tsx
/** 与 Quick Filter 同源：优先 utils，若 utils 返回 key（未命中）则用 materials RecordBoard 再回退 table-action */
function getFilterButtonLabel(buttonText) {
	if (buttonText != null && buttonText !== "") return buttonText;
	const key = "pisell2.recordBoard.toolbar.filter";
	const fromUtils = locales.getText(key);
	if (fromUtils && fromUtils !== key) return fromUtils;
	return getText(key) || getText("table-action-filter-button");
}
const Filter = ({ value, hiddenQuickFilter, hiddenOtherFilter, onChange, quickFilterMaxLength, formFiltersPrefix, renderItem, placement, size = "large", buttonClassName, buttonText }) => {
	const ref = useRef(null);
	const [open, setOpen] = useState(false);
	const [dateRangeOpen, setDateRangeOpen] = useState(false);
	const quickFilter = useMemo(() => {
		return (value === null || value === void 0 ? void 0 : value.quickFilter) || [];
	}, [value === null || value === void 0 ? void 0 : value.quickFilter]);
	const otherFilter = useMemo(() => {
		return (value === null || value === void 0 ? void 0 : value.otherFilter) || [];
	}, [value === null || value === void 0 ? void 0 : value.otherFilter]);
	const handleChange = (newVal) => {
		onChange === null || onChange === void 0 || onChange(_objectSpread2(_objectSpread2({}, value), newVal));
	};
	const content = useMemo(() => {
		return /* @__PURE__ */ React.createElement(Dropdown$1, {
			hiddenQuickFilter,
			hiddenOtherFilter,
			quickFilter,
			otherFilter,
			onChange: handleChange,
			quickFilterMaxLength,
			formFiltersPrefix,
			onOpen: () => setDateRangeOpen(true),
			onClose: () => setDateRangeOpen(false),
			renderItem
		});
	}, [
		quickFilter,
		hiddenQuickFilter,
		hiddenOtherFilter,
		otherFilter,
		quickFilterMaxLength,
		formFiltersPrefix,
		renderItem
	]);
	const isMobileBool = isMobile$1();
	return /* @__PURE__ */ React.createElement("div", { ref }, /* @__PURE__ */ React.createElement(Popover, {
		content,
		arrow: false,
		getPopupContainer: () => ref.current,
		trigger: ["click"],
		open,
		onOpenChange: (visible) => {
			if (dateRangeOpen && !visible) return;
			setOpen(visible);
		},
		placement: placement !== null && placement !== void 0 ? placement : "bottom"
	}, /* @__PURE__ */ React.createElement(Button, {
		className: buttonClassName,
		icon: /* @__PURE__ */ React.createElement(FilterLines, null),
		size
	}, isMobileBool ? null : getFilterButtonLabel(buttonText))));
};
//#endregion
export { Filter as default };
