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/appointmentBooking/components/Header/index.tsx
var Header_exports = {};
__export(Header_exports, {
  default: () => Header_default
});
module.exports = __toCommonJS(Header_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_progressBar = __toESM(require("../../../eventBooking/components/progressBar"));
var import_context = require("../../context");
var import_hooks = require("../../hooks");
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_utils2 = require("../../utils");
var import_index = require("./index.less");
var Header = () => {
  var _a;
  const {
    currentStep,
    appointmentBooking,
    setTimes,
    carts,
    currentStepParams,
    currentSelectedResources
  } = (0, import_context.useAppointmentBookingContext)();
  const shopDiscount = (0, import_hooks.useShopDiscount)();
  const context = (0, import_useEngineContext.default)();
  const { stepList, updateStep, removeStep } = (0, import_hooks.useAppointmentBookingStep)();
  const { setOtherData, getOtherData } = (0, import_hooks.useAppointmentBookingSetting)();
  const { checkProductType } = (0, import_hooks.useAppointmentBookingProduct)();
  const utils = (0, import_hooks.contextUtils)();
  const stepName = (0, import_react.useMemo)(() => {
    var _a2, _b, _c;
    const key = (_a2 = currentStep == null ? void 0 : currentStep.step) == null ? void 0 : _a2.key;
    if (!key) return "";
    if (key.startsWith("select-resource")) {
      const currentResource = (_b = currentSelectedResources == null ? void 0 : currentSelectedResources[0]) == null ? void 0 : _b.currentSelectedResource;
      const resourceItem = (currentResource == null ? void 0 : currentResource.resourceItem) || {};
      const name = (0, import_utils2.getResourceTitle)(
        appointmentBooking,
        resourceItem == null ? void 0 : resourceItem.code,
        utils,
        resourceItem
      );
      return import_utils.locales.getText("pisell2.event.select.category")(name);
    }
    return ((_c = currentStep == null ? void 0 : currentStep.step) == null ? void 0 : _c.description) || "";
  }, [(_a = currentStep == null ? void 0 : currentStep.step) == null ? void 0 : _a.key, currentSelectedResources]);
  const handleBack = async () => {
    var _a2;
    const key = (_a2 = currentStep == null ? void 0 : currentStep.step) == null ? void 0 : _a2.key;
    const types = {
      "select-time": "time",
      "select-resource": "resource",
      "fill-form": "relationForms"
    };
    if (types[key]) {
      appointmentBooking.deleteCartItemInfo(types[key]);
    }
    if (key === "select-date") {
      appointmentBooking.clearDateRange();
    }
    if (key === "select-service") {
      appointmentBooking.clearCart();
      if (appointmentBooking.isCartHasDurationProduct()) {
        appointmentBooking.deleteCartItemInfo("time");
      }
    }
    if (key === "select-time") {
      setTimes([]);
    }
    if (key.startsWith("fill-form")) {
      const form_key = "booking_form";
      let booking_form = getOtherData(form_key);
      removeStep(key);
      setOtherData(form_key, { ...booking_form, [key]: {} });
    }
    appointmentBooking.back();
    const prevStepData = stepList[(currentStepParams == null ? void 0 : currentStepParams.index) - 1];
    const currentStepData = stepList[currentStepParams == null ? void 0 : currentStepParams.index];
    if ((currentStepData == null ? void 0 : currentStepData.isSkip) || (prevStepData == null ? void 0 : prevStepData.isSkip)) {
      const key2 = (currentStepData == null ? void 0 : currentStepData.isSkip) ? currentStepData == null ? void 0 : currentStepData.key : prevStepData == null ? void 0 : prevStepData.key;
      if (key2 === "select-resource") {
        appointmentBooking.deleteCartItemInfo("resource");
      }
      updateStep(key2, {
        isSkip: false
      });
    }
    const isServiceStep = (prevStepData == null ? void 0 : prevStepData.key) === "select-service";
    const isResourceStep = (prevStepData == null ? void 0 : prevStepData.key) === "select-resource";
    if (isServiceStep || isResourceStep) {
      stepList.forEach((item) => {
        if (item.key.startsWith("select-resource_") || item.key.startsWith("fill-form_")) {
          removeStep(item.key);
        }
      });
    }
  };
  const handleClose = () => {
    var _a2, _b;
    appointmentBooking.destroy();
    (_a2 = shopDiscount == null ? void 0 : shopDiscount.destroy) == null ? void 0 : _a2.call(shopDiscount);
    const utils2 = context.appHelper.utils || {};
    (_b = utils2 == null ? void 0 : utils2.history) == null ? void 0 : _b.replace("/");
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_progressBar.default,
    {
      title: currentStep ? `${import_utils.locales.getText("pisell2.appointment.step")(
        (currentStep == null ? void 0 : currentStep.index) + 1
      )}` : "",
      description: stepName,
      showBack: (currentStep == null ? void 0 : currentStep.index) > 0,
      onBack: () => handleBack(),
      onClose: () => handleClose()
    }
  );
};
var Header_default = Header;
