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/context.tsx
var context_exports = {};
__export(context_exports, {
  AppointmentBookingProvider: () => AppointmentBookingProvider,
  useAppointmentBookingContext: () => useAppointmentBookingContext
});
module.exports = __toCommonJS(context_exports);
var import_react = __toESM(require("react"));
var import_hooks = require("./hooks");
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_tasks = require("../../utils/tasks");
var import_utils = require("./utils");
var AppointmentBookingContext = (0, import_react.createContext)(void 0);
var AppointmentBookingProvider = ({
  children
}) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
  const policyIdsRef = (0, import_react.useRef)([]);
  const appointmentBooking = (0, import_hooks.useAppointmentBooking)();
  const [summary, setSummary] = (0, import_react.useState)({});
  const [times, setTimes] = (0, import_react.useState)([]);
  const [resources, setResources] = (0, import_react.useState)([]);
  const [currentStep, setCurrentStep] = (0, import_react.useState)();
  const [carts, setCarts] = (0, import_react.useState)([]);
  const [resourceOtherParams, setResourceOtherParams] = (0, import_react.useState)({});
  const [selectedTime, setSelectedTime] = (0, import_react.useState)(null);
  const [isAgreeDepositPolicy, setIsAgreeDepositPolicy] = (0, import_react.useState)(false);
  const [currentResource, setCurrentResource] = (0, import_react.useState)(null);
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [isErrorHolder, setIsErrorHolder] = (0, import_react.useState)(false);
  const [cartDrawerOpen, setCartDrawerOpen] = (0, import_react.useState)(false);
  const [tasksManager] = (0, import_react.useState)(() => new import_tasks.TasksManager());
  const [policyParams, setPolicyParams] = (0, import_react.useState)(null);
  const [currentSelectedResources, setCurrentSelectedResources] = (0, import_react.useState)();
  const { appHelper } = (0, import_useEngineContext.default)();
  const symbolic = (_a = appHelper == null ? void 0 : appHelper.utils) == null ? void 0 : _a.getSymbolic();
  (0, import_react.useEffect)(() => {
    const currentStep2 = appointmentBooking == null ? void 0 : appointmentBooking.getCurrentStep();
    setCurrentStep(currentStep2);
  }, []);
  const setPolicyIds = (ids) => {
    policyIdsRef.current = ids;
  };
  const openPolicy = (content, callback) => {
    setPolicyParams({ open: true, content, callback, _ts: Date.now() });
  };
  const closePolicy = (isCallback) => {
    setPolicyParams({ open: false, content: {}, callback: null });
    if ((policyParams == null ? void 0 : policyParams.callback) && isCallback) {
      policyParams.callback();
    }
  };
  const stepLists = (0, import_react.useMemo)(() => {
    var _a2, _b2, _c2;
    return ((_c2 = (_b2 = (_a2 = appointmentBooking == null ? void 0 : appointmentBooking.store) == null ? void 0 : _a2.step) == null ? void 0 : _b2.store) == null ? void 0 : _c2.stepList) || [];
  }, [(_d = (_c = (_b = appointmentBooking == null ? void 0 : appointmentBooking.store) == null ? void 0 : _b.step) == null ? void 0 : _c.store) == null ? void 0 : _d.stepList]);
  const isLastStep = (0, import_react.useMemo)(() => {
    var _a2;
    const key = (_a2 = currentStep == null ? void 0 : currentStep.step) == null ? void 0 : _a2.key;
    return key === "confirm-information";
  }, [
    (_e = currentStep == null ? void 0 : currentStep.step) == null ? void 0 : _e.key,
    (_h = (_g = (_f = appointmentBooking == null ? void 0 : appointmentBooking.store) == null ? void 0 : _f.step) == null ? void 0 : _g.store) == null ? void 0 : _h.stepList
  ]);
  const currentStepParams = (0, import_react.useMemo)(() => {
    var _a2;
    const key = (_a2 = currentStep == null ? void 0 : currentStep.step) == null ? void 0 : _a2.key;
    const index = stepLists.findIndex((item) => item.key === key);
    return {
      key,
      index,
      stepLists
    };
  }, [(_i = currentStep == null ? void 0 : currentStep.step) == null ? void 0 : _i.key, stepLists]);
  (0, import_react.useEffect)(() => {
    var _a2, _b2;
    if (((_a2 = currentStepParams == null ? void 0 : currentStepParams.key) == null ? void 0 : _a2.startsWith("select-resource")) || ((_b2 = currentStepParams == null ? void 0 : currentStepParams.key) == null ? void 0 : _b2.startsWith("fill-form"))) {
      if (!(carts == null ? void 0 : carts.length) || !(resources == null ? void 0 : resources.length)) {
        return setCurrentSelectedResources(null);
      }
      const currentSelectedResources2 = [];
      carts.forEach((item) => {
        if (appointmentBooking.isTargetCartIdNormalProduct(item == null ? void 0 : item._id)) {
          return;
        }
        currentSelectedResources2.push({
          _id: item == null ? void 0 : item._id,
          currentSelectedResource: (0, import_utils.getCartCurrentResourceData)(
            item,
            currentStepParams == null ? void 0 : currentStepParams.key,
            resources
          )
        });
      });
      setCurrentSelectedResources(currentSelectedResources2);
      appointmentBooking.setOtherData(
        "current_selected_resources",
        currentSelectedResources2
      );
    }
  }, [currentStepParams == null ? void 0 : currentStepParams.key, carts, resources]);
  (0, import_react.useEffect)(() => {
    const currentSelectedResources2 = appointmentBooking.getOtherData(
      "current_selected_resources"
    );
    setCurrentSelectedResources(currentSelectedResources2 || []);
  }, []);
  const value = {
    appointmentBooking,
    times,
    resources,
    summary,
    currentStep,
    carts,
    setTimes,
    setResources,
    setSummary,
    setCurrentStep,
    setCarts,
    appHelper,
    symbolic,
    resourceOtherParams,
    setResourceOtherParams,
    selectedTime,
    setSelectedTime,
    isAgreeDepositPolicy,
    setIsAgreeDepositPolicy,
    currentResource,
    setCurrentResource,
    loading,
    setLoading,
    isErrorHolder,
    setIsErrorHolder,
    isLastStep,
    tasksManager,
    policyParams,
    setPolicyParams,
    policyIdsRef,
    setPolicyIds,
    openPolicy,
    closePolicy,
    currentStepParams,
    currentSelectedResources,
    setCurrentSelectedResources,
    cartDrawerOpen,
    setCartDrawerOpen
  };
  return /* @__PURE__ */ import_react.default.createElement(AppointmentBookingContext.Provider, { value }, children);
};
var useAppointmentBookingContext = () => {
  try {
    const context = (0, import_react.useContext)(AppointmentBookingContext);
    if (context === void 0) {
      throw new Error(
        "useAppointmentBookingContext must be used within a AppointmentBookingProvider"
      );
    }
    return context;
  } catch (error) {
    console.log(error, "useAppointmentBookingContext");
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  AppointmentBookingProvider,
  useAppointmentBookingContext
});
