const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../locales/index.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_PisellBasicGrid = require("../PisellBasicGrid/PisellBasicGrid.js");
const require_GraphicTextCard = require("../PisellCards/components/GraphicTextCard/GraphicTextCard.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./PisellBatchActionBar.less");
//#region src/components/pisellBatchActionBar/PisellBatchActionBar.tsx
/** 优先 utils；若 utils 返回 key（未命中）则用 materials RecordBoard 再回退 batch-action-bar-* */
function getBatchBarText(key) {
	var _getText;
	const fullKey = `pisell2.salesManagement.batchActionBar.${key}`;
	const fromUtils = _pisell_utils.locales.getText(fullKey);
	if (fromUtils !== fullKey) return fromUtils;
	return (_getText = require_index.getText(fullKey)) !== null && _getText !== void 0 ? _getText : require_index.getText(`batch-action-bar-${key}`);
}
/**
* PisellBatchActionBar 批量操作栏组件
*
* @description 选择感知型的批量操作组件，用于承载基于选中对象的批量行为操作
* @author Pisell Team
* @version 1.0.0
*
* @example
* ```tsx
* <PisellBatchActionBar
*   selectedKeys={selectedKeys}
*   selectedRows={selectedRows}
*   total={total}
*   defaultActions={{
*     selectAll: true,
*     clear: true,
*   }}
*   onSelectAll={() => {
*     // 全选逻辑
*   }}
*   onClear={() => {
*     // 清空选择逻辑
*   }}
* />
* ```
*/
const PisellBatchActionBar = (props) => {
	const { className, style, selectedKeys, selectedRows, total, currentPageTotal, currentPageKeys, position = "float", offset = 0, showSelectedCount = true, selectedCountText, defaultActions, actions = [], actionOrder, enableOverflow = true, overflowThreshold = 5, moreText = getBatchBarText("more"), onSelectAll, onClear, onInvert, onDelete, deleteConfirmConfig, onActionClick } = props;
	/**
	* 用于防止删除按钮重复点击的标记
	*/
	const isDeleteModalOpenRef = (0, react.useRef)(false);
	/**
	* "更多"按钮的展开状态
	*/
	const [moreExpanded, setMoreExpanded] = (0, react.useState)(false);
	/**
	* 计算选择状态
	*/
	const selectionState = (0, react.useMemo)(() => {
		const selectedCount = selectedKeys.length;
		return {
			selectedCount,
			hasSelected: selectedCount > 0,
			isAllSelected: total ? selectedCount === total : false,
			isCurrentPageAllSelected: (() => {
				if (!currentPageKeys || !currentPageTotal) return false;
				return currentPageKeys.every((key) => selectedKeys.includes(key));
			})()
		};
	}, [
		selectedKeys,
		total,
		currentPageKeys,
		currentPageTotal
	]);
	/**
	* 计算容器样式
	*/
	const containerStyle = (0, react.useMemo)(() => {
		const baseStyle = require_objectSpread2._objectSpread2({
			padding: "12px 16px",
			background: "rgba(29, 41, 57, 0.75)",
			border: "none",
			borderRadius: "8px",
			color: "#FFFFFF"
		}, style);
		switch (position) {
			case "sticky-top": return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, baseStyle), {}, {
				position: "sticky",
				top: offset || 0,
				zIndex: 100
			});
			case "sticky-bottom": return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, baseStyle), {}, {
				position: "sticky",
				bottom: offset || 0,
				zIndex: 100
			});
			case "float": return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, baseStyle), {}, {
				position: "fixed",
				bottom: offset || 24,
				left: "50%",
				transform: "translateX(-50%)",
				zIndex: 1e3,
				boxShadow: "0 2px 8px rgba(0,0,0,0.3)"
			});
			default: return baseStyle;
		}
	}, [
		position,
		offset,
		style
	]);
	/**
	* 计算选中数量提示文案
	*/
	const countText = (0, react.useMemo)(() => {
		if (!showSelectedCount) return null;
		const count = selectionState.selectedCount;
		if (typeof selectedCountText === "function") return selectedCountText(count);
		if (selectedCountText) return selectedCountText.replace("{count}", String(count));
		const i18nText = getBatchBarText("selectedCount");
		if (typeof i18nText === "function") return i18nText(count);
		return i18nText || `已选择 ${count} 项`;
	}, [
		showSelectedCount,
		selectedCountText,
		selectionState.selectedCount
	]);
	/**
	* 渲染默认操作按钮
	* @description 固定四个按钮：全选、反选、取消选择、删除
	*/
	const renderDefaultActions = (0, react.useCallback)(() => {
		if ((defaultActions === null || defaultActions === void 0 ? void 0 : defaultActions.showDefaultActions) === false) return [];
		const defaultActionNodes = [];
		if ((defaultActions === null || defaultActions === void 0 ? void 0 : defaultActions.showSelectAll) !== false) defaultActionNodes.push(/* @__PURE__ */ react.default.createElement(require_GraphicTextCard.default, {
			key: "select-all",
			text: getBatchBarText("selectAll"),
			icon: "pisell2-check-square",
			variant: "horizontal",
			size: "small",
			clickable: true,
			className: "pisell-batch-action-bar-button",
			graphicConfig: {
				iconSize: 24,
				width: 24,
				height: 24,
				background: "transparent",
				iconColor: "#FFFFFF"
			},
			textConfig: {
				color: "#FFFFFF",
				fontSize: 14
			},
			style: {
				background: "#1B1B1B",
				border: "none",
				padding: "8px 12px",
				gap: 4,
				minWidth: "120px",
				height: "48px"
			},
			onClick: () => {
				onSelectAll === null || onSelectAll === void 0 || onSelectAll();
			}
		}));
		if ((defaultActions === null || defaultActions === void 0 ? void 0 : defaultActions.showInvert) !== false) defaultActionNodes.push(/* @__PURE__ */ react.default.createElement(require_GraphicTextCard.default, {
			key: "invert",
			text: getBatchBarText("invert"),
			icon: "pisell2-check-done-01",
			variant: "horizontal",
			size: "small",
			clickable: true,
			className: "pisell-batch-action-bar-button",
			graphicConfig: {
				iconSize: 24,
				width: 24,
				height: 24,
				background: "transparent",
				iconColor: "#FFFFFF"
			},
			textConfig: {
				color: "#FFFFFF",
				fontSize: 14
			},
			style: {
				background: "#1B1B1B",
				border: "none",
				padding: "8px 12px",
				gap: 4,
				minWidth: "120px",
				height: "48px"
			},
			onClick: () => {
				onInvert === null || onInvert === void 0 || onInvert();
			}
		}));
		if ((defaultActions === null || defaultActions === void 0 ? void 0 : defaultActions.showClear) !== false) defaultActionNodes.push(/* @__PURE__ */ react.default.createElement(require_GraphicTextCard.default, {
			key: "clear",
			text: getBatchBarText("clear"),
			icon: "pisell2-square",
			variant: "horizontal",
			size: "small",
			clickable: true,
			className: "pisell-batch-action-bar-button",
			graphicConfig: {
				iconSize: 24,
				width: 24,
				height: 24,
				background: "transparent",
				iconColor: "#FFFFFF"
			},
			textConfig: {
				color: "#FFFFFF",
				fontSize: 14
			},
			style: {
				background: "#1B1B1B",
				border: "none",
				padding: "8px 12px",
				gap: 4,
				minWidth: "120px",
				height: "48px"
			},
			onClick: () => {
				onClear === null || onClear === void 0 || onClear();
			}
		}));
		if ((defaultActions === null || defaultActions === void 0 ? void 0 : defaultActions.showDelete) !== false) defaultActionNodes.push(/* @__PURE__ */ react.default.createElement(require_GraphicTextCard.default, {
			key: "delete",
			text: getBatchBarText("delete"),
			icon: "pisell2-delete_wght300_24px",
			variant: "horizontal",
			size: "small",
			clickable: true,
			className: "pisell-batch-action-bar-button",
			graphicConfig: {
				iconSize: 24,
				width: 24,
				height: 24,
				background: "transparent",
				iconColor: "#FFFFFF"
			},
			textConfig: {
				color: "#FFFFFF",
				fontSize: 14
			},
			style: {
				background: "#1B1B1B",
				border: "none",
				padding: "8px 12px",
				gap: 4,
				minWidth: "120px",
				height: "48px"
			},
			onClick: () => {
				var _deleteConfirmConfig$, _deleteConfirmConfig$2, _deleteConfirmConfig$3, _deleteConfirmConfig$4;
				if (isDeleteModalOpenRef.current) return;
				isDeleteModalOpenRef.current = true;
				const i18nContent = getBatchBarText("deleteConfirmContent");
				const deleteContent = typeof i18nContent === "function" ? i18nContent(selectionState.selectedCount) : i18nContent || `确定要删除选中的 ${selectionState.selectedCount} 项吗？`;
				antd.Modal.confirm({
					title: (_deleteConfirmConfig$ = deleteConfirmConfig === null || deleteConfirmConfig === void 0 ? void 0 : deleteConfirmConfig.title) !== null && _deleteConfirmConfig$ !== void 0 ? _deleteConfirmConfig$ : getBatchBarText("deleteConfirmTitle"),
					content: (_deleteConfirmConfig$2 = deleteConfirmConfig === null || deleteConfirmConfig === void 0 ? void 0 : deleteConfirmConfig.description) !== null && _deleteConfirmConfig$2 !== void 0 ? _deleteConfirmConfig$2 : deleteContent,
					okText: (_deleteConfirmConfig$3 = deleteConfirmConfig === null || deleteConfirmConfig === void 0 ? void 0 : deleteConfirmConfig.okText) !== null && _deleteConfirmConfig$3 !== void 0 ? _deleteConfirmConfig$3 : getBatchBarText("deleteConfirmOk"),
					cancelText: (_deleteConfirmConfig$4 = deleteConfirmConfig === null || deleteConfirmConfig === void 0 ? void 0 : deleteConfirmConfig.cancelText) !== null && _deleteConfirmConfig$4 !== void 0 ? _deleteConfirmConfig$4 : getBatchBarText("deleteConfirmCancel"),
					okButtonProps: { danger: true },
					onOk: () => {
						onDelete === null || onDelete === void 0 || onDelete(selectedKeys, selectedRows);
					},
					onCancel: () => {
						isDeleteModalOpenRef.current = false;
					},
					afterClose: () => {
						isDeleteModalOpenRef.current = false;
					}
				});
			}
		}));
		return defaultActionNodes;
	}, [
		onSelectAll,
		onInvert,
		onClear,
		onDelete,
		selectedKeys,
		selectedRows,
		selectionState.selectedCount,
		defaultActions === null || defaultActions === void 0 ? void 0 : defaultActions.showDefaultActions,
		defaultActions === null || defaultActions === void 0 ? void 0 : defaultActions.showSelectAll,
		defaultActions === null || defaultActions === void 0 ? void 0 : defaultActions.showInvert,
		defaultActions === null || defaultActions === void 0 ? void 0 : defaultActions.showClear,
		defaultActions === null || defaultActions === void 0 ? void 0 : defaultActions.showDelete,
		deleteConfirmConfig === null || deleteConfirmConfig === void 0 ? void 0 : deleteConfirmConfig.title,
		deleteConfirmConfig === null || deleteConfirmConfig === void 0 ? void 0 : deleteConfirmConfig.description,
		deleteConfirmConfig === null || deleteConfirmConfig === void 0 ? void 0 : deleteConfirmConfig.okText,
		deleteConfirmConfig === null || deleteConfirmConfig === void 0 ? void 0 : deleteConfirmConfig.cancelText
	]);
	/**
	* 处理自定义操作按钮点击
	*/
	const handleActionClick = (0, react.useCallback)((action) => {
		if (action.onClick) action.onClick(selectedKeys, selectedRows);
		else if (onActionClick) onActionClick(action.key, selectedKeys, selectedRows);
	}, [
		selectedKeys,
		selectedRows,
		onActionClick
	]);
	/**
	* 渲染自定义操作按钮
	*/
	const renderCustomActions = (0, react.useCallback)(() => {
		const visibleActions = actions.filter((action) => {
			return typeof action.visible === "function" ? action.visible(selectedKeys, selectedRows) : action.visible !== false;
		});
		return (actionOrder ? visibleActions.sort((a, b) => {
			const indexA = actionOrder.indexOf(a.key);
			const indexB = actionOrder.indexOf(b.key);
			if (indexA === -1 && indexB === -1) return 0;
			if (indexA === -1) return 1;
			if (indexB === -1) return -1;
			return indexA - indexB;
		}) : visibleActions).map((action) => {
			const disabled = typeof action.disabled === "function" ? action.disabled(selectedKeys, selectedRows) : action.disabled || false;
			const card = /* @__PURE__ */ react.default.createElement(require_GraphicTextCard.default, {
				key: action.key,
				text: action.label,
				icon: action.icon,
				variant: "horizontal",
				size: action.size || "small",
				clickable: !disabled && !action.loading,
				disabled: disabled ? { value: true } : void 0,
				loading: action.loading ? { value: true } : void 0,
				className: (0, classnames.default)("pisell-batch-action-bar-button", action.className),
				graphicConfig: require_objectSpread2._objectSpread2({
					iconSize: 24,
					width: 24,
					height: 24,
					background: "transparent",
					iconColor: "#FFFFFF"
				}, typeof action.icon === "string" ? {} : {}),
				textConfig: { color: "#FFFFFF" },
				style: require_objectSpread2._objectSpread2({
					background: "#1B1B1B",
					border: "none",
					padding: "8px 12px",
					minWidth: "120px",
					height: "48px"
				}, action.style),
				onClick: () => handleActionClick(action)
			});
			if (action.confirm) {
				const confirmConfig = typeof action.confirm === "object" ? action.confirm : {};
				return /* @__PURE__ */ react.default.createElement(antd.Popconfirm, {
					key: action.key,
					title: confirmConfig.title || getBatchBarText("confirmTitle"),
					description: confirmConfig.description,
					okText: confirmConfig.okText || getBatchBarText("deleteConfirmOk"),
					cancelText: confirmConfig.cancelText || getBatchBarText("deleteConfirmCancel"),
					onConfirm: () => handleActionClick(action)
				}, card);
			}
			return card;
		});
	}, [
		actions,
		actionOrder,
		selectedKeys,
		selectedRows,
		handleActionClick
	]);
	/**
	* 合并所有操作按钮
	*/
	const allActions = (0, react.useMemo)(() => {
		const defaultActions = renderDefaultActions();
		const customActions = renderCustomActions();
		return [...defaultActions, ...customActions];
	}, [renderDefaultActions, renderCustomActions]);
	/**
	* 计算溢出处理
	*/
	const { visibleActions, overflowActions, hasOverflow } = (0, react.useMemo)(() => {
		if (!enableOverflow || allActions.length <= overflowThreshold) return {
			visibleActions: allActions,
			overflowActions: [],
			hasOverflow: false
		};
		return {
			visibleActions: allActions.slice(0, overflowThreshold),
			overflowActions: allActions.slice(overflowThreshold),
			hasOverflow: true
		};
	}, [
		enableOverflow,
		overflowThreshold,
		allActions
	]);
	/**
	* 渲染更多操作下拉菜单
	*/
	const renderMoreActions = (0, react.useCallback)(() => {
		if (!hasOverflow || overflowActions.length === 0) return null;
		const modifiedOverflowActions = overflowActions.map((action) => {
			if (react.default.isValidElement(action)) {
				var _el$props;
				const el = action;
				const prevStyle = (_el$props = el.props) === null || _el$props === void 0 ? void 0 : _el$props.style;
				return react.default.cloneElement(el, { style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prevStyle || {}), {}, {
					minWidth: "160px",
					width: "160px"
				}) });
			}
			return action;
		});
		const content = /* @__PURE__ */ react.default.createElement(require_PisellBasicGrid.default, {
			dataSource: modifiedOverflowActions,
			renderItem: (item) => item,
			direction: "vertical",
			gap: 0
		});
		return /* @__PURE__ */ react.default.createElement(antd.Popover, {
			key: "more-actions",
			content,
			trigger: "click",
			placement: "topLeft",
			arrow: false,
			open: moreExpanded,
			onOpenChange: setMoreExpanded,
			overlayClassName: "pisell-batch-action-bar__more-popover",
			overlayInnerStyle: {
				padding: 0,
				background: "rgba(29, 41, 57, 1)"
			}
		}, /* @__PURE__ */ react.default.createElement(require_GraphicTextCard.default, {
			text: moreText,
			icon: "pisell2-dots-horizontal",
			variant: "horizontal",
			size: "small",
			clickable: true,
			active: { value: moreExpanded },
			className: "pisell-batch-action-bar-button",
			graphicConfig: {
				iconSize: 24,
				width: 24,
				height: 24,
				background: "transparent",
				iconColor: "#FFFFFF"
			},
			textConfig: {
				color: "#FFFFFF",
				fontSize: 14
			},
			style: {
				background: moreExpanded ? "rgba(93, 63, 159, 1)" : "#1B1B1B",
				border: "none",
				padding: "8px 12px",
				gap: 4,
				minWidth: "120px",
				height: "48px"
			}
		}));
	}, [
		hasOverflow,
		overflowActions,
		moreText,
		moreExpanded
	]);
	/**
	* 合并类名
	*/
	const mergedClassName = (0, classnames.default)("pisell-batch-action-bar", className, { [`pisell-batch-action-bar--${position}`]: position });
	return /* @__PURE__ */ react.default.createElement("div", {
		className: mergedClassName,
		style: containerStyle
	}, countText && /* @__PURE__ */ react.default.createElement("div", { className: "pisell-batch-action-bar__count" }, countText), /* @__PURE__ */ react.default.createElement(require_PisellBasicGrid.default, {
		dataSource: [...visibleActions, renderMoreActions()].filter(Boolean),
		renderItem: (item) => item,
		direction: "horizontal",
		wrap: "nowrap",
		gap: 12,
		justify: "start",
		align: "center",
		className: "pisell-batch-action-bar__actions"
	}));
};
PisellBatchActionBar.displayName = "PisellBatchActionBar";
//#endregion
exports.default = PisellBatchActionBar;
