var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/Sales/Event/index.tsx
var Event_exports = {};
__export(Event_exports, {
  EventContext: () => EventContext,
  EventProvider: () => EventProvider,
  default: () => Event_default,
  useEvent: () => useEvent
});
module.exports = __toCommonJS(Event_exports);
var import_react = __toESM(require("react"));
var import_SalesModel = require("../../../businessModel/SalesModel");
var import_serve = require("./serve");
var import_ahooks = require("ahooks");
var import_Cart = require("../Cart");
var defaultState = {
  currentEventItem: null,
  eventList: []
};
var EventContext = (0, import_react.createContext)(
  void 0
);
var EventProvider = ({ children }) => {
  const { data, setData, getCurrentData } = (0, import_SalesModel.useSales)();
  const { updateCurrentCartItem, currentCartItem, createCartItemFromEvent } = (0, import_Cart.useCart)();
  const eventData = (0, import_react.useMemo)(() => {
    return data.eventData || {};
  }, [data.eventData]);
  const setEventData = (newEventData) => {
    setData({
      eventData: {
        ...getCurrentData().eventData,
        ...newEventData
      }
    });
  };
  const getEventList = (0, import_ahooks.useRequest)(import_serve.getEventListApi, {
    manual: true,
    onSuccess: (res) => {
      var _a, _b;
      const eventList = [];
      if ((_b = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.list) == null ? void 0 : _b.length) {
        res.data.list.forEach((item) => {
          var _a2, _b2;
          if ((_a2 = item == null ? void 0 : item.relation_product_ids) == null ? void 0 : _a2.length) {
            eventList.push({
              id: item.id,
              title: item.title,
              price: item.price,
              image: item.cover || "",
              isMultiPrice: !!(((_b2 = item == null ? void 0 : item.relation_product_ids) == null ? void 0 : _b2.length) > 1),
              _originData: item
            });
          }
        });
      }
      setEventData({ eventList });
    }
  });
  const getEventListByTitle = (title) => {
    var _a;
    if (!title) {
      return eventData.eventList || [];
    }
    const newEventList = [];
    if ((_a = eventData == null ? void 0 : eventData.eventList) == null ? void 0 : _a.length) {
      eventData.eventList.forEach((item) => {
        var _a2;
        if ((_a2 = item.title) == null ? void 0 : _a2.includes(title || "")) {
          newEventList.push(item);
        }
      });
    }
    return newEventList;
  };
  const selectEvent = (eventItem) => {
    setEventData({
      currentEventItem: eventItem
    });
    createCartItemFromEvent(eventItem);
  };
  const contextValue = {
    eventData,
    getEventList,
    getEventListByTitle,
    selectEvent
  };
  return /* @__PURE__ */ import_react.default.createElement(EventContext.Provider, { value: contextValue }, children);
};
var useEvent = () => {
  const context = import_react.default.useContext(EventContext);
  if (!context) {
    throw new Error("useEvent must be used within a EventProvider");
  }
  return context;
};
var Event_default = {
  defaultState
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  EventContext,
  EventProvider,
  useEvent
});
