import PisellModal from "../../pisellModal/index.js";
import PisellFooter from "../../pisellLayout/footer.js";
import { mergeClassNames, modifierClassName } from "../utils/classNames.js";
import FooterActions from "./FooterActions.js";
import React, { Children, cloneElement, isValidElement, useMemo } from "react";
//#region src/components/PisellProcedure/components/ProcedureFooter.tsx
/**
* ProcedureFooter 底部操作区组件
* 
* @description 包含左侧自定义内容和右侧操作按钮（支持溢出收纳功能）
* @author Pisell Team
*/
const ProcedureFooter = ({ footerProps, position, className, isGlobal = false, sidebarContent, summaryModalOpen = false, onOpenSummaryModal, onCloseSummaryModal, goPrev, goNext, mode = "page" }) => {
	/** 底部区域类名 */
	const footerCls = useMemo(() => {
		return mergeClassNames("pisell-procedure-footer", className, footerProps === null || footerProps === void 0 ? void 0 : footerProps.className, modifierClassName("pisell-procedure-footer", position));
	}, [
		className,
		footerProps === null || footerProps === void 0 ? void 0 : footerProps.className,
		position
	]);
	/** 获取操作按钮内容 */
	const actionsContent = useMemo(() => {
		const actions = footerProps === null || footerProps === void 0 ? void 0 : footerProps.actions;
		if (!actions) return null;
		if (Children.toArray(actions).length === 0) return null;
		return Children.map(actions, (child, index) => {
			if (!isValidElement(child)) return child;
			const childProps = child.props;
			const dataAction = childProps === null || childProps === void 0 ? void 0 : childProps["data-action"];
			const originalOnClick = childProps === null || childProps === void 0 ? void 0 : childProps.onClick;
			let injectedAction;
			if (dataAction === "prev") injectedAction = goPrev;
			else if (dataAction === "next") injectedAction = goNext;
			if (injectedAction) return cloneElement(child, { onClick: (...args) => {
				originalOnClick === null || originalOnClick === void 0 || originalOnClick(...args);
				injectedAction === null || injectedAction === void 0 || injectedAction();
			} });
			return child;
		});
	}, [
		footerProps === null || footerProps === void 0 ? void 0 : footerProps.actions,
		goPrev,
		goNext
	]);
	/**
	* 包装 summaryButton 添加点击事件
	* 如果用户传入的按钮已有 onClick，则先执行用户的 onClick，再打开弹窗
	*/
	const wrappedSummaryButton = useMemo(() => {
		var _button$props;
		const button = footerProps === null || footerProps === void 0 ? void 0 : footerProps.summaryButton;
		if (!button || !isValidElement(button)) return button;
		const originalOnClick = (_button$props = button.props) === null || _button$props === void 0 ? void 0 : _button$props.onClick;
		return cloneElement(button, { onClick: (e) => {
			if (typeof originalOnClick === "function") originalOnClick(e);
			onOpenSummaryModal === null || onOpenSummaryModal === void 0 || onOpenSummaryModal();
		} });
	}, [footerProps === null || footerProps === void 0 ? void 0 : footerProps.summaryButton, onOpenSummaryModal]);
	/** 渲染底部操作区内容 */
	const footerContent = useMemo(() => {
		const hasSummaryButton = mode === "modal" ? false : !!wrappedSummaryButton;
		const hasLeftSlot = (footerProps === null || footerProps === void 0 ? void 0 : footerProps.leftSlot) !== void 0 && (footerProps === null || footerProps === void 0 ? void 0 : footerProps.leftSlot) !== null;
		const hasActions = actionsContent !== null && actionsContent !== void 0;
		if (!hasSummaryButton && !hasLeftSlot && !hasActions) return null;
		return /* @__PURE__ */ React.createElement(React.Fragment, null, hasSummaryButton && wrappedSummaryButton && /* @__PURE__ */ React.createElement("div", { className: "pisell-procedure-footer__summary" }, wrappedSummaryButton), (hasLeftSlot || hasActions) && ((footerProps === null || footerProps === void 0 ? void 0 : footerProps.enableOverflow) !== false ? /* @__PURE__ */ React.createElement(FooterActions, {
			leftSlot: footerProps === null || footerProps === void 0 ? void 0 : footerProps.leftSlot,
			moreText: footerProps === null || footerProps === void 0 ? void 0 : footerProps.moreText
		}, actionsContent) : /* @__PURE__ */ React.createElement("div", { className: "pisell-procedure-footer__content" }, (footerProps === null || footerProps === void 0 ? void 0 : footerProps.leftSlot) && /* @__PURE__ */ React.createElement("div", { className: "pisell-procedure-footer__left" }, footerProps.leftSlot), hasActions && /* @__PURE__ */ React.createElement("div", { className: "pisell-procedure-footer__actions" }, actionsContent))));
	}, [
		mode,
		wrappedSummaryButton,
		footerProps === null || footerProps === void 0 ? void 0 : footerProps.enableOverflow,
		footerProps === null || footerProps === void 0 ? void 0 : footerProps.leftSlot,
		footerProps === null || footerProps === void 0 ? void 0 : footerProps.moreText,
		actionsContent
	]);
	/** 移动端信息区弹窗 */
	const summaryModal = useMemo(() => {
		if (!(footerProps === null || footerProps === void 0 ? void 0 : footerProps.summaryButton) || !sidebarContent) return null;
		return /* @__PURE__ */ React.createElement(PisellModal, {
			open: summaryModalOpen,
			onCancel: onCloseSummaryModal,
			title: (footerProps === null || footerProps === void 0 ? void 0 : footerProps.summaryModalTitle) || "详情",
			platform: "h5",
			footer: null,
			destroyOnClose: true
		}, sidebarContent);
	}, [
		footerProps === null || footerProps === void 0 ? void 0 : footerProps.summaryButton,
		footerProps === null || footerProps === void 0 ? void 0 : footerProps.summaryModalTitle,
		sidebarContent,
		summaryModalOpen,
		onCloseSummaryModal
	]);
	if (!footerContent) return null;
	if (isGlobal) return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(PisellFooter, {
		className: footerCls,
		style: footerProps === null || footerProps === void 0 ? void 0 : footerProps.style
	}, footerContent), summaryModal);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", {
		className: footerCls,
		style: footerProps === null || footerProps === void 0 ? void 0 : footerProps.style
	}, footerContent), summaryModal);
};
ProcedureFooter.displayName = "ProcedureFooter";
//#endregion
export { ProcedureFooter as default };
