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/Detail.tsx
var Detail_exports = {};
__export(Detail_exports, {
  EventDetailContext: () => EventDetailContext,
  EventDetailProvider: () => EventDetailProvider,
  useEventDetail: () => useEventDetail
});
module.exports = __toCommonJS(Detail_exports);
var import_react = __toESM(require("react"));
var import_serve = require("./serve");
var import_ahooks = require("ahooks");
var import_index = require("./index");
var import_utils = require("../utils");
var import_useLoginUser = __toESM(require("../../../hooks/useLoginUser"));
var EventDetailContext = (0, import_react.createContext)(void 0);
var EventDetailProvider = ({
  children
}) => {
  const eventRef = (0, import_react.useRef)({});
  const detailRef = (0, import_react.useRef)({});
  const [detail, setDetail] = (0, import_react.useState)(null);
  const [open, setOpen] = (0, import_react.useState)(false);
  const [currentBookingOption, setCurrentBookingOption] = (0, import_react.useState)(null);
  const { selectEvent } = (0, import_index.useEvent)();
  const customer = (0, import_useLoginUser.default)();
  const currentBookingOptionRef = (0, import_react.useRef)(null);
  const _setDetail = (detailData) => {
    detailRef.current = { ...detailRef.current || {}, ...detailData || {} };
    setDetail({ ...detailRef.current });
  };
  const _setCurrentBookingOption = (id) => {
    currentBookingOptionRef.current = id;
    setCurrentBookingOption(id);
  };
  const onOpenEventDetail = (eventItem) => {
    var _a;
    setOpen(true);
    const introduction = {
      title: eventItem.title,
      price: eventItem.price,
      images: eventItem.image ? [eventItem.image] : []
    };
    eventRef.current = eventItem;
    _setDetail({ introduction });
    getEventDetailIntroduction.run(eventItem.id);
    getEventDetailResource.run({ event_item_id: eventItem.id });
    getEventDetailBookingOptions.run({
      ids: ((_a = eventItem._originData) == null ? void 0 : _a.relation_product_ids) || [],
      num: 100,
      skip: 1,
      customer_id: customer == null ? void 0 : customer.id,
      open_quotation: 1,
      is_eject: 1
    });
    getEventDetailSelectDate.run(eventItem.id, {
      days: 366
    });
  };
  const onCloseEventDetail = () => {
    setOpen(false);
    _setDetail(null);
    detailRef.current = null;
    setCurrentBookingOption(null);
    currentBookingOptionRef.current = null;
  };
  const onSelectEventDetail = () => {
    var _a, _b, _c;
    selectEvent({
      ...eventRef.current,
      eventDetail: {
        ...detailRef.current || {},
        bookingOptionItem: (_b = (_a = detail.bookingOptions) == null ? void 0 : _a.filter(
          (v) => v.value === currentBookingOptionRef.current
        )) == null ? void 0 : _b[0],
        productDetail: (_c = detailRef.current) == null ? void 0 : _c.productDetail
      }
    });
    onCloseEventDetail();
  };
  const getEventDetailIntroduction = (0, import_ahooks.useRequest)(import_serve.getEventDetailIntroductionApi, {
    manual: true,
    onSuccess: (res) => {
      const data = res.data;
      _setDetail({
        introduction: {
          ...detail.introduction || {},
          title: data.title,
          subtitle: data.subtitle,
          description: data.description,
          images: (0, import_utils.getIamgeFromMedia)(data.media),
          _originData: res.data
        }
      });
    }
  });
  const getEventDetailSelectDate = (0, import_ahooks.useRequest)(import_serve.getEventDetailSelectDateApi, {
    manual: true,
    onSuccess: (res) => {
      var _a, _b, _c, _d;
      const data = res.data;
      const dates = [...data.dates || []];
      const startDateInfo = (_b = (_a = detailRef.current) == null ? void 0 : _a.selectDate) == null ? void 0 : _b.startDateInfo;
      const endDateInfo = (_d = (_c = detailRef.current) == null ? void 0 : _c.selectDate) == null ? void 0 : _d.endDateInfo;
      const selectDate = {
        dates,
        _originData: {
          dates,
          default_data: data.default_data,
          shop_time: data.shop_time
        },
        startDateInfo,
        endDateInfo
      };
      if (!startDateInfo) {
        selectDate.startDateInfo = dates == null ? void 0 : dates[0];
      }
      if (!endDateInfo && (dates == null ? void 0 : dates.length)) {
        selectDate.endDateInfo = dates == null ? void 0 : dates[dates.length - 1];
      }
      _setDetail({ selectDate });
    }
  });
  const getEventDetailResource = (0, import_ahooks.useRequest)(import_serve.getEventDetailTeacherApi, {
    manual: true,
    onSuccess: (res) => {
      const data = res.data || [];
      const resource = [];
      data.forEach((item) => {
        var _a;
        resource.push({
          title: item.form_name,
          children: (_a = item == null ? void 0 : item.resource) == null ? void 0 : _a.map((v) => {
            var _a2;
            return {
              id: v.id,
              title: v.resource_name,
              image: (0, import_utils.getIamgeFromMedia)((_a2 = v == null ? void 0 : v.form_data) == null ? void 0 : _a2.media, "findOne"),
              _originData: v
            };
          })
        });
      });
      _setDetail({ resource });
    }
  });
  const getEventDetailBookingOptions = (0, import_ahooks.useRequest)(
    import_serve.getEventDetailBookingOptionsApi,
    {
      manual: true,
      onSuccess: (res) => {
        var _a;
        const list = ((_a = res.data) == null ? void 0 : _a.list) || [];
        let current = null;
        const bookingOptions = list.map((v) => {
          const option = {
            value: v.id,
            label: v.title,
            price: v.price,
            _originData: v
          };
          if (!current || Number((current == null ? void 0 : current.price) || 0) > Number(v.price || 0)) {
            current = { ...option };
          }
          return option;
        });
        _setDetail({ bookingOptions });
        selectEventDetailBookingOptions(current.value);
      }
    }
  );
  const getProductDetailRequest = (0, import_ahooks.useRequest)(import_serve.getProductDetailApi, {
    manual: true,
    loadingDelay: 200
  });
  const selectEventDetailBookingOptions = (id) => {
    var _a, _b, _c;
    const { introduction, bookingOptions } = detailRef.current || {};
    const option = (_a = bookingOptions == null ? void 0 : bookingOptions.filter((v) => v.value === id)) == null ? void 0 : _a[0];
    const setSelectedData = (data) => {
      var _a2;
      _setDetail({
        introduction: {
          ...((_a2 = detailRef == null ? void 0 : detailRef.current) == null ? void 0 : _a2.introduction) || {},
          price: data == null ? void 0 : data.price,
          origin_price: data == null ? void 0 : data.original_price
        },
        selectedProduct: data
        // 选中的活动关联的商品
      });
      _setCurrentBookingOption(id);
    };
    if (((_b = option == null ? void 0 : option._originData) == null ? void 0 : _b.is_eject) === 0) {
      getProductDetailRequest.runAsync(id, {
        open_deposit: 1,
        front_end_cache_id: ((_c = eventRef == null ? void 0 : eventRef.current) == null ? void 0 : _c.id) + "_" + id
      }).then((res) => {
        var _a2;
        const custom_deposit_data = (_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2.custom_deposit_data;
        const newOption = { ...option };
        if (custom_deposit_data && newOption._originData) {
          newOption._originData.custom_deposit_data = custom_deposit_data;
        }
        setSelectedData(newOption);
      }).catch(() => {
        setSelectedData(option);
      });
    } else {
      setSelectedData(option);
    }
  };
  const selectEventDetailProductDetail = (productDetail) => {
    _setDetail({ productDetail });
  };
  const checkBeforeSelectEventDetail = () => {
    var _a;
    const { selectedProduct, productDetail } = detailRef.current || {};
    console.log("selectedProduct", selectedProduct, productDetail);
    if (((_a = selectedProduct == null ? void 0 : selectedProduct._originData) == null ? void 0 : _a.is_eject) === 1 && !productDetail) {
      return false;
    }
    return true;
  };
  const contextValue = {
    detail,
    open,
    onOpenEventDetail,
    onCloseEventDetail,
    onSelectEventDetail,
    selectEventDetailBookingOptions,
    getEventDetailSelectDate,
    currentBookingOption,
    selectEventDetailProductDetail,
    checkBeforeSelectEventDetail,
    getProductDetailRequest
  };
  return /* @__PURE__ */ import_react.default.createElement(EventDetailContext.Provider, { value: contextValue }, children);
};
var useEventDetail = () => {
  const context = import_react.default.useContext(EventDetailContext);
  if (!context) {
    throw new Error("useEventDetail must be used within a EventDetailProvider");
  }
  return context;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  EventDetailContext,
  EventDetailProvider,
  useEventDetail
});
