import { getText } from "../../../locales/index.js";
import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import PisellModal from "../../pisellModal/index.js";
import PisellHeader from "../../pisellLayout/header.js";
import PisellSteps_default from "../../PisellSteps/index.js";
import { mergeClassNames } from "../utils/classNames.js";
import React, { useCallback, useMemo, useState } from "react";
//#region src/components/PisellProcedure/components/ProcedureHeader.tsx
/**
* ProcedureHeader 头部区域组件
* 
* @description 包含左侧返回按钮、中间步骤条、右侧关闭按钮
* @author Pisell Team
*/
const ProcedureHeader = ({ headerProps, currentStep, className, goPrev, goToStep, onClose, onFirstStepBack, onBackConfirm, onCloseConfirm }) => {
	var _modalConfig$config2, _modalConfig$config3;
	/** 确认弹窗类型（back: 返回确认，close: 关闭确认，null: 不显示） */
	const [modalType, setModalType] = useState(null);
	/** 目标步骤索引（用于步骤跳转，-1 表示返回上一步） */
	const [targetStep, setTargetStep] = useState(-1);
	/** 头部区域类名 */
	const headerCls = useMemo(() => {
		return mergeClassNames("pisell-procedure-header", className, headerProps === null || headerProps === void 0 ? void 0 : headerProps.className);
	}, [className, headerProps === null || headerProps === void 0 ? void 0 : headerProps.className]);
	/** 插槽顺序，默认为默认顺序 */
	const slotOrder = (headerProps === null || headerProps === void 0 ? void 0 : headerProps.slotOrder) || "default";
	/** 根据 slotOrder 决定实际显示的左右插槽 */
	const actualLeftSlot = useMemo(() => {
		return slotOrder === "reverse" ? headerProps === null || headerProps === void 0 ? void 0 : headerProps.right : headerProps === null || headerProps === void 0 ? void 0 : headerProps.left;
	}, [
		slotOrder,
		headerProps === null || headerProps === void 0 ? void 0 : headerProps.left,
		headerProps === null || headerProps === void 0 ? void 0 : headerProps.right
	]);
	const actualRightSlot = useMemo(() => {
		return slotOrder === "reverse" ? headerProps === null || headerProps === void 0 ? void 0 : headerProps.left : headerProps === null || headerProps === void 0 ? void 0 : headerProps.right;
	}, [
		slotOrder,
		headerProps === null || headerProps === void 0 ? void 0 : headerProps.left,
		headerProps === null || headerProps === void 0 ? void 0 : headerProps.right
	]);
	/** 是否显示左侧区域（考虑插槽顺序） */
	const showActualLeftArea = useMemo(() => {
		if (!actualLeftSlot) return false;
		if (slotOrder === "reverse") return true;
		if (currentStep === 0) {
			var _headerProps$showBack;
			return (_headerProps$showBack = headerProps === null || headerProps === void 0 ? void 0 : headerProps.showBackOnFirstStep) !== null && _headerProps$showBack !== void 0 ? _headerProps$showBack : false;
		}
		return true;
	}, [
		actualLeftSlot,
		slotOrder,
		currentStep,
		headerProps === null || headerProps === void 0 ? void 0 : headerProps.showBackOnFirstStep
	]);
	/** 是否显示右侧区域（考虑插槽顺序） */
	const showActualRightArea = useMemo(() => {
		return !!actualRightSlot;
	}, [actualRightSlot]);
	/**
	* 关闭确认弹窗
	*/
	const closeModal = useCallback(() => {
		setModalType(null);
		setTargetStep(-1);
	}, []);
	/**
	* 统一处理返回和步骤跳转
	* @param step 目标步骤索引，如果未传递或 < 0 则表示返回上一步
	*/
	const handleBackClick = useCallback((step) => {
		if (currentStep === 0 && (headerProps === null || headerProps === void 0 ? void 0 : headerProps.showBackOnFirstStep) && (step === void 0 || step < 0)) {
			if (onFirstStepBack) onFirstStepBack();
			else if (typeof window !== "undefined" && window.location) window.location.href = "/";
			return;
		}
		const modalConfig = headerProps === null || headerProps === void 0 ? void 0 : headerProps.backConfirmModal;
		const isStepJump = step !== void 0 && step >= 0;
		setTargetStep(isStepJump ? step : -1);
		if (!(modalConfig === null || modalConfig === void 0 ? void 0 : modalConfig.enabled)) {
			if (isStepJump) goToStep === null || goToStep === void 0 || goToStep(step);
			else goPrev === null || goPrev === void 0 || goPrev();
			return;
		}
		setModalType("back");
	}, [
		currentStep,
		headerProps,
		onFirstStepBack,
		goPrev,
		goToStep
	]);
	/**
	* 处理关闭按钮点击
	*/
	const handleCloseClick = useCallback(() => {
		const modalConfig = headerProps === null || headerProps === void 0 ? void 0 : headerProps.closeConfirmModal;
		if (!(modalConfig === null || modalConfig === void 0 ? void 0 : modalConfig.enabled)) {
			onClose === null || onClose === void 0 || onClose();
			return;
		}
		setModalType("close");
	}, [headerProps, onClose]);
	/**
	* 处理确认操作
	*/
	const handleConfirm = useCallback((action) => {
		const isConfirm = action === "ok" || action === "confirm";
		if (modalType === "back") {
			if (isConfirm) {
				onBackConfirm === null || onBackConfirm === void 0 || onBackConfirm(action);
				if (targetStep >= 0) goToStep === null || goToStep === void 0 || goToStep(targetStep);
				else goPrev === null || goPrev === void 0 || goPrev();
			}
		} else if (modalType === "close") {
			if (isConfirm) {
				onCloseConfirm === null || onCloseConfirm === void 0 || onCloseConfirm(action);
				onClose === null || onClose === void 0 || onClose();
			}
		}
		closeModal();
	}, [
		modalType,
		targetStep,
		onBackConfirm,
		onCloseConfirm,
		goPrev,
		goToStep,
		onClose,
		closeModal
	]);
	/**
	* 获取当前弹窗的配置
	*/
	const modalConfig = useMemo(() => {
		if (modalType === "back") return {
			config: headerProps === null || headerProps === void 0 ? void 0 : headerProps.backConfirmModal,
			titleKey: "pisell-procedure-header-back-confirm-title",
			descKey: "pisell-procedure-header-back-confirm-desc",
			okTextKey: "pisell-procedure-header-back-confirm-yes",
			cancelTextKey: "pisell-procedure-header-back-confirm-no"
		};
		else if (modalType === "close") return {
			config: headerProps === null || headerProps === void 0 ? void 0 : headerProps.closeConfirmModal,
			titleKey: "pisell-procedure-header-close-confirm-title",
			descKey: "pisell-procedure-header-close-confirm-desc",
			okTextKey: "pisell-procedure-header-close-confirm-yes",
			cancelTextKey: "pisell-procedure-header-close-confirm-no"
		};
		return null;
	}, [modalType, headerProps]);
	/**
	* 处理 footer 按钮配置
	*/
	const processedFooter = useMemo(() => {
		var _modalConfig$config;
		if (!modalConfig) return void 0;
		const footer = (_modalConfig$config = modalConfig.config) === null || _modalConfig$config === void 0 ? void 0 : _modalConfig$config.footer;
		if (!footer || !Array.isArray(footer) || footer.length === 0) return void 0;
		return footer.map((btn) => {
			if (!btn || typeof btn !== "object" || !btn.action) return btn;
			const processedBtn = _objectSpread2({}, btn);
			const originalOnClick = processedBtn.onClick;
			processedBtn.onClick = (...args) => {
				originalOnClick === null || originalOnClick === void 0 || originalOnClick(...args);
				handleConfirm(btn.action);
			};
			return processedBtn;
		});
	}, [modalConfig, handleConfirm]);
	/** 合并步骤条配置 */
	const mergedStepsProps = useMemo(() => _objectSpread2(_objectSpread2({}, headerProps === null || headerProps === void 0 ? void 0 : headerProps.stepsProps), {}, {
		current: currentStep,
		onChange: (step) => {
			var _headerProps$stepsPro, _headerProps$stepsPro2;
			headerProps === null || headerProps === void 0 || (_headerProps$stepsPro = headerProps.stepsProps) === null || _headerProps$stepsPro === void 0 || (_headerProps$stepsPro2 = _headerProps$stepsPro.onChange) === null || _headerProps$stepsPro2 === void 0 || _headerProps$stepsPro2.call(_headerProps$stepsPro, step);
			handleBackClick(step);
		}
	}), [
		headerProps === null || headerProps === void 0 ? void 0 : headerProps.stepsProps,
		currentStep,
		handleBackClick
	]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(PisellHeader, {
		className: headerCls,
		style: headerProps === null || headerProps === void 0 ? void 0 : headerProps.style
	}, showActualLeftArea && /* @__PURE__ */ React.createElement("div", {
		className: "pisell-procedure-header__left",
		onClick: () => {
			if (slotOrder === "reverse") handleCloseClick();
			else handleBackClick();
		}
	}, actualLeftSlot), (headerProps === null || headerProps === void 0 ? void 0 : headerProps.showSteps) !== false && /* @__PURE__ */ React.createElement("div", { className: "pisell-procedure-header__center" }, /* @__PURE__ */ React.createElement(PisellSteps_default, _objectSpread2(_objectSpread2({}, mergedStepsProps), {}, {
		mobileMode: "auto",
		mobileBreakpoint: 744
	}))), showActualRightArea && /* @__PURE__ */ React.createElement("div", {
		className: "pisell-procedure-header__right",
		onClick: () => {
			if (slotOrder === "reverse") handleBackClick();
			else handleCloseClick();
		}
	}, actualRightSlot)), modalType && modalConfig && /* @__PURE__ */ React.createElement(PisellModal.Information, {
		open: true,
		title: ((_modalConfig$config2 = modalConfig.config) === null || _modalConfig$config2 === void 0 ? void 0 : _modalConfig$config2.title) || getText(modalConfig.titleKey) || "",
		describe: ((_modalConfig$config3 = modalConfig.config) === null || _modalConfig$config3 === void 0 ? void 0 : _modalConfig$config3.describe) || getText(modalConfig.descKey) || "",
		footer: processedFooter,
		okText: getText(modalConfig.okTextKey) || "",
		cancelText: getText(modalConfig.cancelTextKey) || "",
		closable: true,
		onCancel: closeModal,
		onOk: () => handleConfirm("ok")
	}));
};
ProcedureHeader.displayName = "ProcedureHeader";
//#endregion
export { ProcedureHeader as default };
