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); }
import React, { createContext, useMemo, useEffect } from 'react';
import { useSales } from "../../../businessModel/SalesModel";
import { getBoardSettingApi } from "./serve";
import { useRequest } from 'ahooks';
import step from "../Step";
import cart from "../Cart";
import checkout from "../Checkout";
import holder from "../Holder";
import event from "../Event";
import resource from "../Resource";
import summary from "../Summary";
import { useCustomer } from "../Customer";

// 定义上下文接口

// 默认步骤状态
var defaultState = {};

// 创建步骤上下文
export var InitContext = /*#__PURE__*/createContext(undefined);
export var InitProvider = function InitProvider(_ref) {
  var children = _ref.children;
  var _useSales = useSales(),
    data = _useSales.data,
    setData = _useSales.setData,
    hooks = _useSales.hooks,
    getCurrentData = _useSales.getCurrentData,
    getSessionStorage = _useSales.getSessionStorage;
  var _useCustomer = useCustomer(),
    appUtils = _useCustomer.appUtils;
  var useCustomInit = hooks.useCustomInit;
  var customInit = useCustomInit();
  var initData = useMemo(function () {
    return data.initData || {};
  }, [data]);
  var init = function init() {
    // 从sessionStorage中获取数据进行初始化赋值
    var storageData = getSessionStorage();
    setData(_objectSpread({
      initData: step.defaultState,
      stepData: step.defaultState,
      cartData: cart.defaultState,
      checkoutData: checkout.defaultState,
      holderData: holder.defaultState,
      eventData: event.defaultState,
      resourceData: resource.defaultState,
      summaryData: summary.defaultState
    }, storageData || {}));
  };
  var setInitData = function setInitData(newInitData) {
    setData({
      initData: _objectSpread(_objectSpread({}, data.initData), newInitData)
    });
  };

  /**
   * 返回第一步后需要重置部分数据
   */
  var resetInitPartData = function resetInitPartData() {
    var _getCurrentData$resou;
    setData(_objectSpread(_objectSpread({}, getCurrentData() || {}), {}, {
      cartData: cart.defaultState,
      checkoutData: checkout.defaultState,
      holderData: holder.defaultState,
      eventData: event.defaultState,
      summaryData: summary.defaultState,
      resourceData: {
        resourceList: (_getCurrentData$resou = getCurrentData().resourceData) === null || _getCurrentData$resou === void 0 ? void 0 : _getCurrentData$resou.resourceList,
        currentResourceItem: null
      }
    }));
  };

  // 初始化步骤数据
  useEffect(function () {
    init();
  }, []);
  var getBoardSetting = useRequest(getBoardSettingApi, {
    manual: true,
    onSuccess: function onSuccess(res) {
      var _res$data, _res$data2, _appUtils$getStore, _res$data3;
      setInitData({
        boardSetting: res === null || res === void 0 ? void 0 : res.data,
        form_id: res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 || (_res$data = _res$data.config) === null || _res$data === void 0 || (_res$data = _res$data.resource) === null || _res$data === void 0 || (_res$data = _res$data.booking_event_v2_resource) === null || _res$data === void 0 ? void 0 : _res$data.form_id,
        shop_id: res === null || res === void 0 || (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.shop_id,
        shop: (_appUtils$getStore = appUtils.getStore()) === null || _appUtils$getStore === void 0 || (_appUtils$getStore = _appUtils$getStore.getState()) === null || _appUtils$getStore === void 0 || (_appUtils$getStore = _appUtils$getStore.core) === null || _appUtils$getStore === void 0 || (_appUtils$getStore = _appUtils$getStore.core) === null || _appUtils$getStore === void 0 ? void 0 : _appUtils$getStore.shop,
        scheduleDisplayType: res === null || res === void 0 || (_res$data3 = res.data) === null || _res$data3 === void 0 || (_res$data3 = _res$data3.config) === null || _res$data3 === void 0 || (_res$data3 = _res$data3.product_display_setting) === null || _res$data3 === void 0 || (_res$data3 = _res$data3.booking_event_v2_schedule_display_format) === null || _res$data3 === void 0 ? void 0 : _res$data3.value
      });
    }
  });
  var contextValue = {
    initData: initData,
    getBoardSetting: getBoardSetting,
    resetInitPartData: resetInitPartData
  };
  return /*#__PURE__*/React.createElement(InitContext.Provider, {
    value: contextValue
  }, children);
};

/**
 * 使用步骤管理的Hook
 * @throws {Error} 如果在StepProvider外部使用会抛出错误
 * @returns {StepContextType} 步骤管理上下文
 */
export var useInit = function useInit() {
  var context = React.useContext(InitContext);
  if (!context) {
    throw new Error('useInit must be used within a InitProvider');
  }
  return context;
};