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 _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; }
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); }
import React, { useLayoutEffect, useMemo, useRef } from 'react';
import { Provider, createStore } from "./miniRedux";
import { locales } from '@pisell/utils';
import dayjs from 'dayjs';
import { cloneDeep } from 'lodash';
import TicketBooking from "./components/ticketBooking/index";
import { request } from "../../utils";
import useEngineContext from "../../hooks/useEngineContext";
import { getBookingTicketKey, getDefaultStartParam, getShopDiscountKey, rulesHooks } from "./utils";
import localeTexts from "./locales";
import MenuBar from "./components/menuBar";
import { useLocalStorageState } from 'ahooks';
import { OsKeyProvider } from "./context/OsKeyContext";
import { IsActiveProvider } from "./context/IsActiveContext";
import "./index.less";
export var ticketBookingInitialState = {
  amountSymbol: '$',
  modalState: {
    client: {
      showModal: false,
      clients: [],
      value: {
        id: 0,
        nickname: 'Select a customer'
      },
      clientsParams: {
        skip: 1,
        num: 100,
        search: ''
      },
      edit: false
    },
    date: {
      value: dayjs(),
      edit: false
    },
    service: {
      addModal: false,
      editModal: false,
      reloadResourceModal: false,
      services: [],
      resources: [],
      value: [],
      cacheItem: null,
      edit: false
    },
    pet: {
      showModal: false,
      pets: [],
      value: {},
      edit: false
    },
    addons: {
      showModal: false,
      value: [],
      productList: [],
      originData: [],
      edit: false,
      load: false
    },
    form: {
      value: {},
      forms: [],
      edit: false
    },
    notes: {
      orderNote: '',
      internalNotes: [],
      edit: false
    },
    // 是否禁用编辑
    disabledEdit: false,
    // 渠道禁止编辑
    channelDisabledEdit: false,
    // 预约状态
    appointment_status: 'new',
    eventOtherParams: undefined
  },
  bookingConfig: null,
  /** 平面图等入口传入的餐牌 ID，优先于 board menu_list 用于 getProducts */
  associated_menu_list_ids: undefined,
  bookingDetail: null,
  bookingId: 0,
  loading: false,
  open: false,
  isShowDeposit: false,
  activeKey: 'info',
  exitModal: false,
  rebookState: {
    open: false,
    date: '2024-03-13 18:00'
  },
  bookingFunctions: {},
  // Event活动预约抽屉
  eventOpen: false,
  renderType: 'booking',
  // 是否是rebook模式
  openRebook: false,
  // 禁用活动预约编辑
  disableEventBookingEdit: false,
  isShowInfo: false,
  // 是否pin住
  persistent: true,
  // persistent:
  //   this.utils.storage.get(`drawer-booking`) === 'true' ??
  //   this.utils.isTerminal(),
  // 是否定时设置当前时间
  intervalSetTime: true,
  // 时间是否更改
  isTimeChange: false,
  // 日期是否更改
  isDateChange: false,
  list: {
    /** 列表loading */
    isLoading: false,
    /** 预约列表数据 */
    bookingList: [],
    /** 预约列表查询参数 */
    params: _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, 'with', ['order', 'order.customer', 'contacts', 'item', 'appointmentForm.formRecord', 'holder.formRecord']), "booking_time_start_between", getDefaultStartParam()), "child_booking", 1), "skip", 1), "num", 1000),
    showList: [],
    listSelectType: 'all',
    search: '',
    sort: ''
  },
  confirmOpen: false,
  createCount: 0,
  verticalAddServiceKey: 0,
  currentProduct: null,
  bookingConfigParams: {
    item_type: 'appointment_booking'
  },
  productExtensionType: ['appointment_ticket', 'session_ticket', 'product_appointment', 'service_product', 'session_product', 'normal', 'product_appointment'],
  // 是否是平行资源预约的booking
  isParallelResourcesBooking: null,
  // 是否是弹窗编辑模式
  isEditTicketMode: false,
  // 订单已付金额数据
  paymentPaid: {
    value: {
      paidAmount: 0,
      paidAmountWithoutSurcharge: 0
    }
  }
};
export default (function (props) {
  var _context$appHelper, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4, _context$appHelper$ut5;
  var productExtensionType = props.productExtensionType,
    propsBookingConfigParams = props.bookingConfigParams,
    _props$isShowMenuBar = props.isShowMenuBar,
    isShowMenuBar = _props$isShowMenuBar === void 0 ? true : _props$isShowMenuBar,
    onPrerenderComplete = props.onPrerenderComplete,
    _props$isShowTimeBar = props.isShowTimeBar,
    propsIsShowTimeBar = _props$isShowTimeBar === void 0 ? true : _props$isShowTimeBar,
    _props$isShowFloatBut = props.isShowFloatButtons,
    propsIsShowFloatButtons = _props$isShowFloatBut === void 0 ? true : _props$isShowFloatBut,
    propsProductDisplayConfig = props.productDisplayConfig,
    _props$isActive = props.isActive,
    isActive = _props$isActive === void 0 ? true : _props$isActive,
    propsRulesHooks = props.rulesHooks,
    externalStoreRef = props.storeRef,
    editModeInitialState = props.editModeInitialState,
    secondaryScreenDisplay = props.secondaryScreenDisplay,
    bodyTopSlot = props.bodyTopSlot;
  var _useLocalStorageState = useLocalStorageState('pisell-ticket-booking-nav-key', {
      defaultValue: 'ticket'
    }),
    _useLocalStorageState2 = _slicedToArray(_useLocalStorageState, 2),
    activeNavKey = _useLocalStorageState2[0],
    setActiveNavKey = _useLocalStorageState2[1];

  // 根据 activeNavKey 动态配置（仅在显示菜单栏时生效）
  var bookingConfigParams = useMemo(function () {
    if (isShowMenuBar && activeNavKey === 'food') {
      return _objectSpread(_objectSpread({}, propsBookingConfigParams), {}, {
        sub_type: 'food',
        source_type: 'pos'
      });
    }
    return propsBookingConfigParams;
  }, [isShowMenuBar, activeNavKey, propsBookingConfigParams]);
  var isShowTimeBar = useMemo(function () {
    return isShowMenuBar && activeNavKey === 'food' ? false : propsIsShowTimeBar;
  }, [isShowMenuBar, activeNavKey, propsIsShowTimeBar]);
  var isShowFloatButtons = useMemo(function () {
    return isShowMenuBar && activeNavKey === 'food' ? false : propsIsShowFloatButtons;
  }, [isShowMenuBar, activeNavKey, propsIsShowFloatButtons]);

  // 根据bookingConfigParams?.sub_type 动态配置osKey
  // 多页面使用同一个组件时，需要根据不同的osKey来区分不同的页面
  var osKey = useMemo(function () {
    return "".concat(bookingConfigParams === null || bookingConfigParams === void 0 ? void 0 : bookingConfigParams.sub_type).concat(bookingConfigParams !== null && bookingConfigParams !== void 0 && bookingConfigParams.osKey ? "-".concat(bookingConfigParams === null || bookingConfigParams === void 0 ? void 0 : bookingConfigParams.osKey) : '');
  }, [bookingConfigParams]);
  var productDisplayConfig = useMemo(function () {
    if (isShowMenuBar && activeNavKey === 'food') {
      return 'sku-list-a5';
    }
    return propsProductDisplayConfig;
  }, [isShowMenuBar, activeNavKey, propsProductDisplayConfig]);
  var internalStoreRef = useRef(null);
  var storeRef = externalStoreRef !== null && externalStoreRef !== void 0 ? externalStoreRef : internalStoreRef;
  var context = useEngineContext();
  var _context$appHelper$ut = (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils,
    pisellos = _context$appHelper$ut.pisellos,
    pisellosAllModule = _context$appHelper$ut.pisellosAllModule,
    businessUtils = _context$appHelper$ut.businessUtils;
  var _ref = pisellosAllModule || {},
    BookingTicket = _ref.BookingTicket,
    ShopDiscountImpl = _ref.ShopDiscountImpl;
  locales.init(localeTexts, ((_context$appHelper$ut2 = context.appHelper.utils) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut2.getApp) === null || _context$appHelper$ut3 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut3.call(_context$appHelper$ut2)) === null || _context$appHelper$ut3 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut3.locales) === null || _context$appHelper$ut3 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut3.getLocale) === null || _context$appHelper$ut4 === void 0 ? void 0 : _context$appHelper$ut4.call(_context$appHelper$ut3)) || ((_context$appHelper$ut5 = context.appHelper.utils) === null || _context$appHelper$ut5 === void 0 || (_context$appHelper$ut5 = _context$appHelper$ut5.storage) === null || _context$appHelper$ut5 === void 0 ? void 0 : _context$appHelper$ut5.get('umi_locale')) || 'en');
  useLayoutEffect(function () {
    request.setRequest(context.appHelper.utils.request);
  }, []);
  useMemo(function () {
    var store = createStore(_objectSpread(_objectSpread({}, cloneDeep(ticketBookingInitialState)), {}, {
      bookingConfigParams: bookingConfigParams,
      productExtensionType: productExtensionType
    }, editModeInitialState || {}));
    storeRef.current = store;
  }, [bookingConfigParams, productExtensionType, editModeInitialState]);
  useMemo(function () {
    // 如果是isEditTicketMode模式没必要重新初始化，列表已经初始化过了其实
    // 退一步来说，应该先检查有没有对应模块，没有再注册
    var bookingTicketModule = pisellos === null || pisellos === void 0 ? void 0 : pisellos.getModule(getBookingTicketKey(osKey));
    if (BookingTicket && !bookingTicketModule) {
      var _businessUtils$getApp;
      pisellos === null || pisellos === void 0 || pisellos.registerModule(new BookingTicket(getBookingTicketKey(osKey)), {
        initialState: {},
        otherParams: {
          cacheId: getBookingTicketKey(osKey),
          platform: 'shop',
          isFranchisee: businessUtils === null || businessUtils === void 0 || (_businessUtils$getApp = businessUtils.getAppByModal) === null || _businessUtils$getApp === void 0 ? void 0 : _businessUtils$getApp.call(businessUtils, 'franchisee')
        }
      });
    }
    var shopDiscountModule = pisellos === null || pisellos === void 0 ? void 0 : pisellos.getModule(getShopDiscountKey(osKey));
    if (ShopDiscountImpl && !shopDiscountModule) {
      pisellos === null || pisellos === void 0 || pisellos.registerModule(new ShopDiscountImpl(getShopDiscountKey(osKey)), {
        otherParams: {
          platform: "shop",
          rules: {
            hooks: propsRulesHooks || rulesHooks
          }
        }
      });
    }
  }, [osKey]);
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-ticket-booking-wrapper"
  }, isShowMenuBar && /*#__PURE__*/React.createElement(MenuBar, {
    activeNavKey: activeNavKey,
    onNavChange: setActiveNavKey
  }), /*#__PURE__*/React.createElement(IsActiveProvider, {
    isActive: isActive
  }, /*#__PURE__*/React.createElement(OsKeyProvider, {
    osKey: osKey,
    businessCode: props === null || props === void 0 ? void 0 : props.businessCode
  }, /*#__PURE__*/React.createElement(Provider, {
    store: storeRef.current,
    key: activeNavKey
  }, /*#__PURE__*/React.createElement(TicketBooking, {
    onPrerenderComplete: onPrerenderComplete,
    productDisplayConfig: productDisplayConfig,
    isShowTimeBar: isShowTimeBar,
    isShowFloatButtons: isShowFloatButtons,
    secondaryScreenDisplay: secondaryScreenDisplay,
    bodyTopSlot: bodyTopSlot
  })))));
});