const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../locales/index.js");
const require_platform = require("../../utils/platform.js");
const require_index$1 = require("./components/InlineMenu/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _ant_design_icons = require("@ant-design/icons");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let react_dom = require("react-dom");
let _pisell_icon_es_SwitchVertical01 = require("@pisell/icon/es/SwitchVertical01");
_pisell_icon_es_SwitchVertical01 = require_runtime.__toESM(_pisell_icon_es_SwitchVertical01);
require("./index.less");
//#region src/components/sort/index.tsx
const prefix = "pisell-lowcode-";
/** 二级项 key 必含 `:asc` / `:desc`；点一级仅展开子菜单时不应触发排序回调，否则会写入半套 sort 并触发重绘，下拉子菜单会闪一下 */
function isSortLeafMenuKey(key) {
	const s = key == null ? "" : String(key);
	return /:(asc|desc)(:|$)/.test(s);
}
const Label = ({ content }) => {
	return /* @__PURE__ */ react.default.createElement("div", { className: `${prefix}sort-menu-item-label` }, /* @__PURE__ */ react.default.createElement("span", null, content), /* @__PURE__ */ react.default.createElement(_ant_design_icons.CheckOutlined, null));
};
const sortChildrenMap = {
	date: (key, isLocal) => [{
		key: `${key}:asc${isLocal ? ":date" : ""}`,
		label: /* @__PURE__ */ react.default.createElement(Label, { content: require_index.getText("sort-oldest-to-newest") })
	}, {
		key: `${key}:desc${isLocal ? ":date" : ""}`,
		label: /* @__PURE__ */ react.default.createElement(Label, { content: require_index.getText("sort-newest-to-oldest") })
	}],
	time: (key, isLocal) => [{
		key: `${key}:desc${isLocal ? ":time" : ""}`,
		label: /* @__PURE__ */ react.default.createElement(Label, { content: require_index.getText("sort-from-morning-to-night") })
	}, {
		key: `${key}:asc${isLocal ? ":time" : ""}`,
		label: /* @__PURE__ */ react.default.createElement(Label, { content: require_index.getText("sort-from-evening-to-morning") })
	}],
	string: (key, isLocal) => [{
		key: `${key}:asc${isLocal ? ":string" : ""}`,
		label: /* @__PURE__ */ react.default.createElement(Label, { content: require_index.getText("sort-a-z") })
	}, {
		key: `${key}:desc${isLocal ? ":string" : ""}`,
		label: /* @__PURE__ */ react.default.createElement(Label, { content: require_index.getText("sort-z-a") })
	}],
	number: (key, isLocal) => [{
		key: `${key}:asc${isLocal ? ":number" : ""}`,
		label: /* @__PURE__ */ react.default.createElement(Label, { content: require_index.getText("sort-0-9") })
	}, {
		key: `${key}:desc${isLocal ? ":number" : ""}`,
		label: /* @__PURE__ */ react.default.createElement(Label, { content: require_index.getText("sort-9-0") })
	}],
	option: (key, isLocal) => [{
		key: `${key}:asc${isLocal ? ":option" : ""}`,
		label: /* @__PURE__ */ react.default.createElement(Label, { content: require_index.getText("sort-first-last") })
	}, {
		key: `${key}:desc${isLocal ? ":option" : ""}`,
		label: /* @__PURE__ */ react.default.createElement(Label, { content: require_index.getText("sort-last-first") })
	}]
};
const Sort = ({ list = [], onChange, button, style, value, placement, localPagination }) => {
	const [dropdownOpen, setDropdownOpen] = (0, react.useState)(false);
	const sortItems = (0, react.useMemo)(() => {
		return list.map((d) => {
			const children = d.getSortItems ? d.getSortItems(d.name).map(({ key, label }) => ({
				key,
				label: /* @__PURE__ */ react.default.createElement(Label, { content: label })
			})) : sortChildrenMap[d.type](d.name, !!(d === null || d === void 0 ? void 0 : d.localFilter));
			return {
				key: d.name,
				label: d.label,
				children,
				popupClassName: `${prefix}sort-sub-menu-popup-wrap ${prefix}dropdown-css-var`,
				className: `${d.isHidden ? `${prefix}sort-menu-wrap-hide` : ""}`
			};
		});
	}, [list, localPagination]);
	const onMenuClick = (0, react.useCallback)((detail) => {
		var _detail$keyPath;
		const leaf = (_detail$keyPath = detail.keyPath) === null || _detail$keyPath === void 0 ? void 0 : _detail$keyPath[0];
		if (!isSortLeafMenuKey(leaf)) return;
		const leafStr = String(leaf);
		/** 先同步收起浮层再通知上层，避免 sort 更新导致 Menu 重绘与关窗动画叠在一起出现「闪两下」 */
		(0, react_dom.flushSync)(() => {
			setDropdownOpen(false);
		});
		if (leafStr === value) {
			onChange === null || onChange === void 0 || onChange("", detail);
			return;
		}
		onChange === null || onChange === void 0 || onChange(leafStr, detail);
	}, [value, onChange]);
	const dropdownMenu = (0, react.useMemo)(() => ({
		items: sortItems,
		onClick: onMenuClick,
		selectable: true,
		selectedKeys: value != null && value !== "" ? [value] : []
	}), [
		sortItems,
		onMenuClick,
		value
	]);
	const isMobileBool = require_platform.isMobile();
	const btnContent = (0, react.useMemo)(() => {
		return button || /* @__PURE__ */ react.default.createElement(antd.Button, {
			icon: /* @__PURE__ */ react.default.createElement(_pisell_icon_es_SwitchVertical01.default, null),
			style,
			className: (0, classnames.default)({ [`${prefix}sort-button-active`]: !!value }),
			size: "large"
		}, isMobileBool ? null : require_index.getText("sort-button-text"));
	}, [
		button,
		style,
		value,
		isMobileBool
	]);
	const content = (0, react.useMemo)(() => {
		return /* @__PURE__ */ react.default.createElement(require_index$1.default, {
			items: sortItems,
			value: value || "",
			onChange
		});
	}, [
		value,
		sortItems,
		onChange
	]);
	if (require_platform.isMobile()) return /* @__PURE__ */ react.default.createElement(antd.Popover, {
		placement,
		content,
		arrow: false,
		overlayClassName: `${prefix}mobile-sort-overlay`,
		trigger: ["click"]
	}, btnContent);
	return /* @__PURE__ */ react.default.createElement(antd.Dropdown, {
		trigger: ["click"],
		placement,
		menu: dropdownMenu,
		open: dropdownOpen,
		onOpenChange: setDropdownOpen
	}, btnContent);
};
//#endregion
exports.default = Sort;
