function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { createContext, useContext, useState, useEffect, useMemo, useRef } from 'react';
import { useAppointmentBooking } from "./hooks";
import useEngineContext from "../../hooks/useEngineContext";
import { TasksManager } from "../../utils/tasks";
import { getCartCurrentResourceData } from "./utils";
var AppointmentBookingContext = /*#__PURE__*/createContext(undefined);
export var AppointmentBookingProvider = function AppointmentBookingProvider(_ref) {
  var _appHelper$utils, _appointmentBooking$s2, _currentStep$step2, _appointmentBooking$s3, _currentStep$step4;
  var children = _ref.children;
  var policyIdsRef = useRef([]);
  var appointmentBooking = useAppointmentBooking();
  // 总计计算
  var _useState = useState({}),
    _useState2 = _slicedToArray(_useState, 2),
    summary = _useState2[0],
    setSummary = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    times = _useState4[0],
    setTimes = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    resources = _useState6[0],
    setResources = _useState6[1];
  var _useState7 = useState(),
    _useState8 = _slicedToArray(_useState7, 2),
    currentStep = _useState8[0],
    setCurrentStep = _useState8[1];
  var _useState9 = useState([]),
    _useState10 = _slicedToArray(_useState9, 2),
    carts = _useState10[0],
    setCarts = _useState10[1];
  var _useState11 = useState({}),
    _useState12 = _slicedToArray(_useState11, 2),
    resourceOtherParams = _useState12[0],
    setResourceOtherParams = _useState12[1];
  var _useState13 = useState(null),
    _useState14 = _slicedToArray(_useState13, 2),
    selectedTime = _useState14[0],
    setSelectedTime = _useState14[1];
  var _useState15 = useState(false),
    _useState16 = _slicedToArray(_useState15, 2),
    isAgreeDepositPolicy = _useState16[0],
    setIsAgreeDepositPolicy = _useState16[1];
  var _useState17 = useState(null),
    _useState18 = _slicedToArray(_useState17, 2),
    currentResource = _useState18[0],
    setCurrentResource = _useState18[1];
  var _useState19 = useState(false),
    _useState20 = _slicedToArray(_useState19, 2),
    loading = _useState20[0],
    setLoading = _useState20[1];
  var _useState21 = useState(false),
    _useState22 = _slicedToArray(_useState21, 2),
    isErrorHolder = _useState22[0],
    setIsErrorHolder = _useState22[1];
  // 初始化 TasksManager
  var _useState23 = useState(function () {
      return new TasksManager();
    }),
    _useState24 = _slicedToArray(_useState23, 1),
    tasksManager = _useState24[0];
  var _useState25 = useState(null),
    _useState26 = _slicedToArray(_useState25, 2),
    policyParams = _useState26[0],
    setPolicyParams = _useState26[1];
  var _useState27 = useState(),
    _useState28 = _slicedToArray(_useState27, 2),
    currentSelectedResources = _useState28[0],
    setCurrentSelectedResources = _useState28[1];
  var _useEngineContext = useEngineContext(),
    appHelper = _useEngineContext.appHelper;
  var symbolic = appHelper === null || appHelper === void 0 || (_appHelper$utils = appHelper.utils) === null || _appHelper$utils === void 0 ? void 0 : _appHelper$utils.getSymbolic();
  useEffect(function () {
    var currentStep = appointmentBooking === null || appointmentBooking === void 0 ? void 0 : appointmentBooking.getCurrentStep();
    setCurrentStep(currentStep);
  }, []);

  /**
   * @title: 设置协议数据
   * @description:
   * @param {any} ids
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-05-12 21:55
   */
  var setPolicyIds = function setPolicyIds(ids) {
    policyIdsRef.current = ids;
  };

  /**
   * @title: 打开协议
   * @description:
   * @param {any} content
   * @param {any} callback
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-05-12 17:56
   */
  var openPolicy = function openPolicy(content, callback) {
    setPolicyParams({
      open: true,
      content: content,
      callback: callback,
      _ts: Date.now()
    });
  };

  /**
   * @title: 关闭协议
   * @description:
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-05-12 17:57
   */
  var closePolicy = function closePolicy(isCallback) {
    setPolicyParams({
      open: false,
      content: {},
      callback: null
    });
    if (policyParams !== null && policyParams !== void 0 && policyParams.callback && isCallback) {
      policyParams.callback();
    }
  };
  var stepLists = useMemo(function () {
    var _appointmentBooking$s;
    return (appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$s = appointmentBooking.store) === null || _appointmentBooking$s === void 0 || (_appointmentBooking$s = _appointmentBooking$s.step) === null || _appointmentBooking$s === void 0 || (_appointmentBooking$s = _appointmentBooking$s.store) === null || _appointmentBooking$s === void 0 ? void 0 : _appointmentBooking$s.stepList) || [];
  }, [appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$s2 = appointmentBooking.store) === null || _appointmentBooking$s2 === void 0 || (_appointmentBooking$s2 = _appointmentBooking$s2.step) === null || _appointmentBooking$s2 === void 0 || (_appointmentBooking$s2 = _appointmentBooking$s2.store) === null || _appointmentBooking$s2 === void 0 ? void 0 : _appointmentBooking$s2.stepList]);

  /**
   * @title: 是否是最后一步
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-05-10 15:18
   */
  var isLastStep = useMemo(function () {
    var _currentStep$step;
    var key = currentStep === null || currentStep === void 0 || (_currentStep$step = currentStep.step) === null || _currentStep$step === void 0 ? void 0 : _currentStep$step.key;
    return key === 'confirm-information';
  }, [currentStep === null || currentStep === void 0 || (_currentStep$step2 = currentStep.step) === null || _currentStep$step2 === void 0 ? void 0 : _currentStep$step2.key, appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$s3 = appointmentBooking.store) === null || _appointmentBooking$s3 === void 0 || (_appointmentBooking$s3 = _appointmentBooking$s3.step) === null || _appointmentBooking$s3 === void 0 || (_appointmentBooking$s3 = _appointmentBooking$s3.store) === null || _appointmentBooking$s3 === void 0 ? void 0 : _appointmentBooking$s3.stepList]);
  var currentStepParams = useMemo(function () {
    var _currentStep$step3;
    var key = currentStep === null || currentStep === void 0 || (_currentStep$step3 = currentStep.step) === null || _currentStep$step3 === void 0 ? void 0 : _currentStep$step3.key;
    var index = stepLists.findIndex(function (item) {
      return item.key === key;
    });
    return {
      key: key,
      index: index,
      stepLists: stepLists
    };
  }, [currentStep === null || currentStep === void 0 || (_currentStep$step4 = currentStep.step) === null || _currentStep$step4 === void 0 ? void 0 : _currentStep$step4.key, stepLists]);
  useEffect(function () {
    var _currentStepParams$ke, _currentStepParams$ke2;
    // 如果当前步骤是选择资源，则更新当前资源的数据
    if (currentStepParams !== null && currentStepParams !== void 0 && (_currentStepParams$ke = currentStepParams.key) !== null && _currentStepParams$ke !== void 0 && _currentStepParams$ke.startsWith('select-resource') || currentStepParams !== null && currentStepParams !== void 0 && (_currentStepParams$ke2 = currentStepParams.key) !== null && _currentStepParams$ke2 !== void 0 && _currentStepParams$ke2.startsWith('fill-form')) {
      if (!(carts !== null && carts !== void 0 && carts.length) || !(resources !== null && resources !== void 0 && resources.length)) {
        return setCurrentSelectedResources(null);
      }
      var _currentSelectedResources = [];
      carts.forEach(function (item) {
        if (appointmentBooking.isTargetCartIdNormalProduct(item === null || item === void 0 ? void 0 : item._id)) {
          return;
        }
        _currentSelectedResources.push({
          _id: item === null || item === void 0 ? void 0 : item._id,
          currentSelectedResource: getCartCurrentResourceData(item, currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.key, resources)
        });
      });
      setCurrentSelectedResources(_currentSelectedResources);

      // 并且将当前资源的数据设置到本地
      appointmentBooking.setOtherData('current_selected_resources', _currentSelectedResources);
    }
  }, [currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.key, carts, resources]);
  useEffect(function () {
    var currentSelectedResources = appointmentBooking.getOtherData('current_selected_resources');
    setCurrentSelectedResources(currentSelectedResources || []);
  }, []);
  var value = {
    appointmentBooking: appointmentBooking,
    times: times,
    resources: resources,
    summary: summary,
    currentStep: currentStep,
    carts: carts,
    setTimes: setTimes,
    setResources: setResources,
    setSummary: setSummary,
    setCurrentStep: setCurrentStep,
    setCarts: setCarts,
    appHelper: appHelper,
    symbolic: symbolic,
    resourceOtherParams: resourceOtherParams,
    setResourceOtherParams: setResourceOtherParams,
    selectedTime: selectedTime,
    setSelectedTime: setSelectedTime,
    isAgreeDepositPolicy: isAgreeDepositPolicy,
    setIsAgreeDepositPolicy: setIsAgreeDepositPolicy,
    currentResource: currentResource,
    setCurrentResource: setCurrentResource,
    loading: loading,
    setLoading: setLoading,
    isErrorHolder: isErrorHolder,
    setIsErrorHolder: setIsErrorHolder,
    isLastStep: isLastStep,
    tasksManager: tasksManager,
    policyParams: policyParams,
    setPolicyParams: setPolicyParams,
    policyIdsRef: policyIdsRef,
    setPolicyIds: setPolicyIds,
    openPolicy: openPolicy,
    closePolicy: closePolicy,
    currentStepParams: currentStepParams,
    currentSelectedResources: currentSelectedResources,
    setCurrentSelectedResources: setCurrentSelectedResources
  };
  return /*#__PURE__*/React.createElement(AppointmentBookingContext.Provider, {
    value: value
  }, children);
};
export var useAppointmentBookingContext = function useAppointmentBookingContext() {
  try {
    var context = useContext(AppointmentBookingContext);
    if (context === undefined) {
      throw new Error('useAppointmentBookingContext must be used within a AppointmentBookingProvider');
    }
    return context;
  } catch (error) {
    console.log(error, 'useAppointmentBookingContext');
  }
};