function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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, { useMemo, useEffect, useState, useRef } from 'react';
import { isMobile, locales } from '@pisell/utils';
import CartItem from "../Cart/Item";
import Header from "../Header";
import Footer from "../Footer";
import Services from "../Services";
import Date from "../Date";
import Resource from "../Resource";
import TimeSlicing from "../TimeSlicing";
import Guest from "../Guest";
import Form from "../Form";
import ConfirmInformation from "../ConfirmInformation";
import { useAppointmentBookingContext } from "../../context";
import PolicyModal from "../../deposit/components/PolicyModal";
import { useAppointmentBookingCart, useAppointmentBookingStep, useAppointmentBookingSetting, useAppointmentBookingLogin, useAppointmentBookingAccount, useShopDiscount } from "../../hooks";
import "./index.less";
var Layout = function Layout() {
  var _currentStep$step, _currentStep$step3, _currentStep$step4, _currentStep$step6, _currentStep$step8;
  var stepRef = useRef();
  var _ref = useAppointmentBookingContext(),
    currentStep = _ref.currentStep,
    setResourceOtherParams = _ref.setResourceOtherParams,
    resourceOtherParams = _ref.resourceOtherParams,
    setIsAgreeDepositPolicy = _ref.setIsAgreeDepositPolicy,
    appointmentBooking = _ref.appointmentBooking,
    setLoading = _ref.setLoading,
    policyParams = _ref.policyParams,
    closePolicy = _ref.closePolicy,
    currentStepParams = _ref.currentStepParams,
    setIsErrorHolder = _ref.setIsErrorHolder;
  var _useAppointmentBookin = useAppointmentBookingSetting(),
    isDateSelectedFirst = _useAppointmentBookin.isDateSelectedFirst,
    isFormSubject = _useAppointmentBookin.isFormSubject,
    isRetailTemplate = _useAppointmentBookin.isRetailTemplate;
  var _useAppointmentBookin2 = useAppointmentBookingLogin(),
    isLogin = _useAppointmentBookin2.isLogin;
  var _useAppointmentBookin3 = useAppointmentBookingAccount(),
    getHolders = _useAppointmentBookin3.getHolders;
  var _useAppointmentBookin4 = useAppointmentBookingStep(),
    removeStep = _useAppointmentBookin4.removeStep;
  var _useShopDiscount = useShopDiscount(),
    shopDiscount = _useShopDiscount.shopDiscount;

  // 不要删除
  useAppointmentBookingCart();
  useAppointmentBookingStep();
  var _useState = useState({}),
    _useState2 = _slicedToArray(_useState, 2),
    params = _useState2[0],
    setParams = _useState2[1];
  useEffect(function () {
    appointmentBooking.removeStep('fill-form');
    return function () {
      var _shopDiscount$destroy;
      setIsAgreeDepositPolicy(false);
      appointmentBooking.destroy();
      shopDiscount === null || shopDiscount === void 0 || (_shopDiscount$destroy = shopDiscount.destroy) === null || _shopDiscount$destroy === void 0 || _shopDiscount$destroy.call(shopDiscount);
    };
  }, []);
  useEffect(function () {
    var _currentStepParams$st;
    if ((currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.index) < 0) return;
    var resourceStepIndex = currentStepParams === null || currentStepParams === void 0 || (_currentStepParams$st = currentStepParams.stepLists) === null || _currentStepParams$st === void 0 ? void 0 : _currentStepParams$st.findIndex(function (item) {
      return item.key === 'select-resource';
    });

    // 清空当前所选择的资源
    if ((currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.index) < resourceStepIndex) {
      appointmentBooking.setOtherData('current_selected_resources', null);
    }
  }, [currentStepParams]);
  useEffect(function () {
    try {
      var _appointmentBooking$o;
      if (!(appointmentBooking !== null && appointmentBooking !== void 0 && appointmentBooking.otherParams) || isRetailTemplate) return;

      // 显示预约添加人
      var showAddGuest = appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$o = appointmentBooking.otherParams) === null || _appointmentBooking$o === void 0 || (_appointmentBooking$o = _appointmentBooking$o.productModuleParams) === null || _appointmentBooking$o === void 0 ? void 0 : _appointmentBooking$o.showAddGuest;

      // 如果是表单主体或者是预约添加人不显示，则删除添加人步骤
      if (isFormSubject || !showAddGuest) {
        removeStep('select-guest');
      } else {
        appointmentBooking.addStep({
          key: 'select-guest',
          description: locales.getText('pisell2.appointment.add-guests')
        }, 'select-service');
      }
    } catch (error) {}
  }, [isFormSubject, isRetailTemplate]);
  useEffect(function () {
    if (isFormSubject && isLogin && !isRetailTemplate) {
      getHolders();
    }
  }, [isFormSubject, isLogin, isRetailTemplate]);
  useEffect(function () {
    if (!(currentStep !== null && currentStep !== void 0 && currentStep.step)) return;

    // 清空底部按钮的loading
    setLoading(false);

    // 清空资源分配失败数据
    setResourceOtherParams(_objectSpread(_objectSpread({}, resourceOtherParams), {}, {
      errorList: null
    }));
    setIsErrorHolder(false);
    window.scrollTo({
      top: 0,
      behavior: 'smooth'
    });
  }, [currentStep === null || currentStep === void 0 || (_currentStep$step = currentStep.step) === null || _currentStep$step === void 0 ? void 0 : _currentStep$step.key]);
  var components = {
    // 选择服务
    'select-service': Services,
    // 添加客户
    'select-guest': Guest,
    // 选择时间
    'select-date': Date,
    // 时间切片
    'select-time': TimeSlicing,
    // 确认信息
    'confirm-information': ConfirmInformation
  };
  var Com = useMemo(function () {
    var _currentStep$step2;
    var key = currentStep === null || currentStep === void 0 || (_currentStep$step2 = currentStep.step) === null || _currentStep$step2 === void 0 ? void 0 : _currentStep$step2.key;
    if (!key) return null;
    var Com;
    if (key.startsWith('select-resource')) {
      // 选择资源
      Com = Resource;
    } else if (key.startsWith('fill-form')) {
      // 填写表单
      Com = Form;
    } else {
      Com = components[key];
    }
    return /*#__PURE__*/React.createElement(Com, {
      onChange: setParams,
      params: params,
      ref: stepRef
    });
  }, [currentStep === null || currentStep === void 0 || (_currentStep$step3 = currentStep.step) === null || _currentStep$step3 === void 0 ? void 0 : _currentStep$step3.key]);

  /**
   * @title: 动态计算中间内容区域的高度
   * @description:
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-02-18 15:36
   */
  useEffect(function () {
    if (isMobile()) {
      var _header$getBoundingCl, _footer$getBoundingCl;
      var header = document.querySelector('.event-progress-bar');
      var footer = document.querySelector('.pisell-section-footers-wrapper');
      var content = document.querySelector('.parallel-content-mobile-wrap');
      var headerHeight = (header === null || header === void 0 || (_header$getBoundingCl = header.getBoundingClientRect()) === null || _header$getBoundingCl === void 0 ? void 0 : _header$getBoundingCl.height) || 0;
      var footerHeight = (footer === null || footer === void 0 || (_footer$getBoundingCl = footer.getBoundingClientRect()) === null || _footer$getBoundingCl === void 0 ? void 0 : _footer$getBoundingCl.height) || 0;
      var windowHeight = window.innerHeight;
      var contentHeight = windowHeight - headerHeight - footerHeight;
      if (content && content !== null && content !== void 0 && content.style) {
        content.style.height = "".concat(contentHeight, "px");
        content.style.overflowY = 'auto';
      }
    }
  }, [currentStep === null || currentStep === void 0 || (_currentStep$step4 = currentStep.step) === null || _currentStep$step4 === void 0 ? void 0 : _currentStep$step4.key]);
  var isShowCartCom = useMemo(function () {
    var _currentStep$step5;
    var key = currentStep === null || currentStep === void 0 || (_currentStep$step5 = currentStep.step) === null || _currentStep$step5 === void 0 ? void 0 : _currentStep$step5.key;
    if (isMobile()) return null;

    // 先选择日期，并且当前步骤是选择日期，则不显示购物车
    if (isDateSelectedFirst && key === 'select-date') {
      return null;
    }
    return /*#__PURE__*/React.createElement("div", {
      className: "parallel-content-right"
    }, /*#__PURE__*/React.createElement(CartItem, {
      onNext: function onNext() {
        var _stepRef$current, _stepRef$current$next;
        return stepRef === null || stepRef === void 0 || (_stepRef$current = stepRef.current) === null || _stepRef$current === void 0 || (_stepRef$current$next = _stepRef$current.next) === null || _stepRef$current$next === void 0 ? void 0 : _stepRef$current$next.call(_stepRef$current);
      }
    }));
  }, [isDateSelectedFirst, currentStep === null || currentStep === void 0 || (_currentStep$step6 = currentStep.step) === null || _currentStep$step6 === void 0 ? void 0 : _currentStep$step6.key]);

  /**
   * @title: 是否显示底部
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-05-10 17:42
   */
  var isFooter = useMemo(function () {
    var _currentStep$step7;
    if (!isMobile()) return false;
    var key = currentStep === null || currentStep === void 0 || (_currentStep$step7 = currentStep.step) === null || _currentStep$step7 === void 0 ? void 0 : _currentStep$step7.key;

    // 先选日期并且当前步骤是选择日期，则不显示底部
    if (isDateSelectedFirst && key === 'select-date') return false;
    return key !== 'confirm-information';
  }, [isDateSelectedFirst, currentStep === null || currentStep === void 0 || (_currentStep$step8 = currentStep.step) === null || _currentStep$step8 === void 0 ? void 0 : _currentStep$step8.key]);

  /**
   * @title: 获取协议方法需要在组件中定义才能传递给子组件，子组件无法获取到context中的方法
   * @description:
   * @param {string} id
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-07-07 10:06
   */
  var depositProtocolRequest = function depositProtocolRequest(id) {
    var _appointmentBooking$g;
    return appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$g = appointmentBooking.getProtocol) === null || _appointmentBooking$g === void 0 ? void 0 : _appointmentBooking$g.call(appointmentBooking, id);
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, !isRetailTemplate ? /*#__PURE__*/React.createElement(Header, null) : null, /*#__PURE__*/React.createElement("div", {
    className: "parallel-content-wrap ".concat(isMobile() ? 'parallel-content-mobile-wrap' : '', " ").concat(isRetailTemplate ? 'parallel-content-retail-wrap' : '')
  }, /*#__PURE__*/React.createElement("div", {
    className: "parallel-content-left"
  }, Com), isShowCartCom), isFooter ? /*#__PURE__*/React.createElement(Footer
  // isOpen={params?.isOpen}
  , {
    onNext: function onNext() {
      var _stepRef$current2, _stepRef$current2$nex;
      return stepRef === null || stepRef === void 0 || (_stepRef$current2 = stepRef.current) === null || _stepRef$current2 === void 0 || (_stepRef$current2$nex = _stepRef$current2.next) === null || _stepRef$current2$nex === void 0 ? void 0 : _stepRef$current2$nex.call(_stepRef$current2);
    }
  }) : null, policyParams !== null && policyParams !== void 0 && policyParams.open ? /*#__PURE__*/React.createElement(PolicyModal, {
    key: policyParams === null || policyParams === void 0 ? void 0 : policyParams._ts,
    open: policyParams === null || policyParams === void 0 ? void 0 : policyParams.open,
    onCancel: function onCancel() {
      return closePolicy(false);
    },
    onConfirm: function onConfirm() {
      return closePolicy(true);
    },
    item: policyParams === null || policyParams === void 0 ? void 0 : policyParams.content,
    onChange: function onChange() {
      return setLoading(false);
    },
    isHideCheckout: false,
    depositProtocolRequest: depositProtocolRequest
  }) : null);
};
export default Layout;