var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/PisellProcedure/components/ProcedureHeader.tsx
var ProcedureHeader_exports = {};
__export(ProcedureHeader_exports, {
  default: () => ProcedureHeader_default
});
module.exports = __toCommonJS(ProcedureHeader_exports);
var import_react = __toESM(require("react"));
var import_header = __toESM(require("../../pisellLayout/header"));
var import_PisellSteps = __toESM(require("../../PisellSteps"));
var import_pisellModal = __toESM(require("../../pisellModal"));
var import_utils = require("../utils");
var import_locales = require("../../../locales");
var ProcedureHeader = ({
  headerProps,
  currentStep,
  className,
  goPrev,
  goToStep,
  onClose,
  onFirstStepBack,
  onBackConfirm,
  onCloseConfirm
}) => {
  var _a, _b;
  const [modalType, setModalType] = (0, import_react.useState)(null);
  const [targetStep, setTargetStep] = (0, import_react.useState)(-1);
  const headerCls = (0, import_react.useMemo)(() => {
    return (0, import_utils.mergeClassNames)(
      "pisell-procedure-header",
      className,
      headerProps == null ? void 0 : headerProps.className
    );
  }, [className, headerProps == null ? void 0 : headerProps.className]);
  const slotOrder = (headerProps == null ? void 0 : headerProps.slotOrder) || "default";
  const actualLeftSlot = (0, import_react.useMemo)(() => {
    return slotOrder === "reverse" ? headerProps == null ? void 0 : headerProps.right : headerProps == null ? void 0 : headerProps.left;
  }, [slotOrder, headerProps == null ? void 0 : headerProps.left, headerProps == null ? void 0 : headerProps.right]);
  const actualRightSlot = (0, import_react.useMemo)(() => {
    return slotOrder === "reverse" ? headerProps == null ? void 0 : headerProps.left : headerProps == null ? void 0 : headerProps.right;
  }, [slotOrder, headerProps == null ? void 0 : headerProps.left, headerProps == null ? void 0 : headerProps.right]);
  const showActualLeftArea = (0, import_react.useMemo)(() => {
    if (!actualLeftSlot) return false;
    if (slotOrder === "reverse") {
      return true;
    }
    if (currentStep === 0) {
      return (headerProps == null ? void 0 : headerProps.showBackOnFirstStep) ?? false;
    }
    return true;
  }, [actualLeftSlot, slotOrder, currentStep, headerProps == null ? void 0 : headerProps.showBackOnFirstStep]);
  const showActualRightArea = (0, import_react.useMemo)(() => {
    return !!actualRightSlot;
  }, [actualRightSlot]);
  const closeModal = (0, import_react.useCallback)(() => {
    setModalType(null);
    setTargetStep(-1);
  }, []);
  const handleBackClick = (0, import_react.useCallback)((step) => {
    if (currentStep === 0 && (headerProps == null ? void 0 : headerProps.showBackOnFirstStep) && (step === void 0 || step < 0)) {
      if (onFirstStepBack) {
        onFirstStepBack();
      } else {
        if (typeof window !== "undefined" && window.location) {
          window.location.href = "/";
        }
      }
      return;
    }
    const modalConfig2 = headerProps == null ? void 0 : headerProps.backConfirmModal;
    const isStepJump = step !== void 0 && step >= 0;
    setTargetStep(isStepJump ? step : -1);
    if (!(modalConfig2 == null ? void 0 : modalConfig2.enabled)) {
      if (isStepJump) {
        goToStep == null ? void 0 : goToStep(step);
      } else {
        goPrev == null ? void 0 : goPrev();
      }
      return;
    }
    setModalType("back");
  }, [currentStep, headerProps, onFirstStepBack, goPrev, goToStep]);
  const handleCloseClick = (0, import_react.useCallback)(() => {
    const modalConfig2 = headerProps == null ? void 0 : headerProps.closeConfirmModal;
    if (!(modalConfig2 == null ? void 0 : modalConfig2.enabled)) {
      onClose == null ? void 0 : onClose();
      return;
    }
    setModalType("close");
  }, [headerProps, onClose]);
  const handleConfirm = (0, import_react.useCallback)((action) => {
    const isConfirm = action === "ok" || action === "confirm";
    if (modalType === "back") {
      if (isConfirm) {
        onBackConfirm == null ? void 0 : onBackConfirm(action);
        if (targetStep >= 0) {
          goToStep == null ? void 0 : goToStep(targetStep);
        } else {
          goPrev == null ? void 0 : goPrev();
        }
      }
    } else if (modalType === "close") {
      if (isConfirm) {
        onCloseConfirm == null ? void 0 : onCloseConfirm(action);
        onClose == null ? void 0 : onClose();
      }
    }
    closeModal();
  }, [modalType, targetStep, onBackConfirm, onCloseConfirm, goPrev, goToStep, onClose, closeModal]);
  const modalConfig = (0, import_react.useMemo)(() => {
    if (modalType === "back") {
      return {
        config: headerProps == null ? 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 ? 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]);
  const processedFooter = (0, import_react.useMemo)(() => {
    var _a2;
    if (!modalConfig) return void 0;
    const footer = (_a2 = modalConfig.config) == null ? void 0 : _a2.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 = { ...btn };
      const originalOnClick = processedBtn.onClick;
      processedBtn.onClick = (...args) => {
        originalOnClick == null ? void 0 : originalOnClick(...args);
        handleConfirm(btn.action);
      };
      return processedBtn;
    });
  }, [modalConfig, handleConfirm]);
  const mergedStepsProps = (0, import_react.useMemo)(() => ({
    ...headerProps == null ? void 0 : headerProps.stepsProps,
    current: currentStep,
    onChange: (step) => {
      var _a2, _b2;
      (_b2 = (_a2 = headerProps == null ? void 0 : headerProps.stepsProps) == null ? void 0 : _a2.onChange) == null ? void 0 : _b2.call(_a2, step);
      handleBackClick(step);
    }
  }), [headerProps == null ? void 0 : headerProps.stepsProps, currentStep, handleBackClick]);
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(import_header.default, { className: headerCls, style: headerProps == null ? void 0 : headerProps.style }, showActualLeftArea && /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "pisell-procedure-header__left",
      onClick: () => {
        if (slotOrder === "reverse") {
          handleCloseClick();
        } else {
          handleBackClick();
        }
      }
    },
    actualLeftSlot
  ), (headerProps == null ? void 0 : headerProps.showSteps) !== false && /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-procedure-header__center" }, /* @__PURE__ */ import_react.default.createElement(
    import_PisellSteps.default,
    {
      ...mergedStepsProps,
      mobileMode: "auto",
      mobileBreakpoint: 744
    }
  )), showActualRightArea && /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "pisell-procedure-header__right",
      onClick: () => {
        if (slotOrder === "reverse") {
          handleBackClick();
        } else {
          handleCloseClick();
        }
      }
    },
    actualRightSlot
  )), modalType && modalConfig && /* @__PURE__ */ import_react.default.createElement(
    import_pisellModal.default.Information,
    {
      open: true,
      title: ((_a = modalConfig.config) == null ? void 0 : _a.title) || (0, import_locales.getText)(modalConfig.titleKey) || "",
      describe: ((_b = modalConfig.config) == null ? void 0 : _b.describe) || (0, import_locales.getText)(modalConfig.descKey) || "",
      footer: processedFooter,
      okText: (0, import_locales.getText)(modalConfig.okTextKey) || "",
      cancelText: (0, import_locales.getText)(modalConfig.cancelTextKey) || "",
      closable: true,
      onCancel: closeModal,
      onOk: () => handleConfirm("ok")
    }
  ));
};
ProcedureHeader.displayName = "ProcedureHeader";
var ProcedureHeader_default = ProcedureHeader;
