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 } from 'react';
import { useSales } from "../../../businessModel/SalesModel";
import { getEventListApi } from "./serve";
import { useRequest } from 'ahooks';
import { useCart } from "../Cart";

// 定义步骤数据接口

// 定义上下文接口

// 默认步骤状态
var defaultState = {
  currentEventItem: null,
  eventList: []
};

// 创建步骤上下文
export var EventContext = /*#__PURE__*/createContext(undefined);
export var EventProvider = function EventProvider(_ref) {
  var children = _ref.children;
  var _useSales = useSales(),
    data = _useSales.data,
    setData = _useSales.setData,
    getCurrentData = _useSales.getCurrentData;
  var _useCart = useCart(),
    updateCurrentCartItem = _useCart.updateCurrentCartItem,
    currentCartItem = _useCart.currentCartItem,
    createCartItemFromEvent = _useCart.createCartItemFromEvent;
  var eventData = useMemo(function () {
    return data.eventData || {};
  }, [data.eventData]);
  var setEventData = function setEventData(newEventData) {
    setData({
      eventData: _objectSpread(_objectSpread({}, getCurrentData().eventData), newEventData)
    });
  };
  var getEventList = useRequest(getEventListApi, {
    manual: true,
    onSuccess: function onSuccess(res) {
      var _res$data;
      var eventList = [];
      if (res !== null && res !== void 0 && (_res$data = res.data) !== null && _res$data !== void 0 && (_res$data = _res$data.list) !== null && _res$data !== void 0 && _res$data.length) {
        res.data.list.forEach(function (item) {
          var _item$relation_produc;
          // 如果活动没有关联商品则不展示该活动
          if (item !== null && item !== void 0 && (_item$relation_produc = item.relation_product_ids) !== null && _item$relation_produc !== void 0 && _item$relation_produc.length) {
            var _item$relation_produc2;
            eventList.push({
              id: item.id,
              title: item.title,
              price: item.price,
              image: item.cover || '',
              isMultiPrice: !!((item === null || item === void 0 || (_item$relation_produc2 = item.relation_product_ids) === null || _item$relation_produc2 === void 0 ? void 0 : _item$relation_produc2.length) > 1),
              _originData: item
            });
          }
        });
      }
      setEventData({
        eventList: eventList
      });
    }
  });
  var getEventListByTitle = function getEventListByTitle(title) {
    var _eventData$eventList;
    if (!title) {
      return eventData.eventList || [];
    }
    var newEventList = [];
    if (eventData !== null && eventData !== void 0 && (_eventData$eventList = eventData.eventList) !== null && _eventData$eventList !== void 0 && _eventData$eventList.length) {
      eventData.eventList.forEach(function (item) {
        var _item$title;
        if ((_item$title = item.title) !== null && _item$title !== void 0 && _item$title.includes(title || '')) {
          newEventList.push(item);
        }
      });
    }
    return newEventList;
  };
  var selectEvent = function selectEvent(eventItem) {
    setEventData({
      currentEventItem: eventItem
    });
    // if (currentCartItem) {
    //   updateCurrentCartItem({ eventItem: eventItem });
    // } else {
    //   createCartItemFromEvent(eventItem);
    // }
    // 每次添加商品时都需要直接创建购物车，防止第一步创建的购物车数据和缓存数据冲突
    createCartItemFromEvent(eventItem);
  };
  var contextValue = {
    eventData: eventData,
    getEventList: getEventList,
    getEventListByTitle: getEventListByTitle,
    selectEvent: selectEvent
  };
  return /*#__PURE__*/React.createElement(EventContext.Provider, {
    value: contextValue
  }, children);
};

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