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/plus/salesSdk/context/SalesSdkSummaryContext.tsx
var SalesSdkSummaryContext_exports = {};
__export(SalesSdkSummaryContext_exports, {
  SalesSdkSummaryContext: () => SalesSdkSummaryContext,
  SalesSdkSummaryProvider: () => SalesSdkSummaryProvider,
  SalesSdkSummaryRefreshContext: () => SalesSdkSummaryRefreshContext
});
module.exports = __toCommonJS(SalesSdkSummaryContext_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_SalesSdkContext = require("./SalesSdkContext");
var SalesSdkSummaryContext = (0, import_react.createContext)(null);
SalesSdkSummaryContext.displayName = "SalesSdkSummaryContext";
var SalesSdkSummaryRefreshContext = (0, import_react.createContext)(null);
SalesSdkSummaryRefreshContext.displayName = "SalesSdkSummaryRefreshContext";
var SalesSdkSummaryProvider = (0, import_react.forwardRef)(function SalesSdkSummaryProvider2({ children }, ref) {
  const sales = (0, import_react.useContext)(import_SalesSdkContext.SalesSdkContext);
  const bookingTicket = (sales == null ? void 0 : sales.bookingTicket) ?? null;
  const [summary, setSummary] = (0, import_react.useState)(null);
  const requestIdRef = (0, import_react.useRef)(0);
  const refreshSummary = (0, import_ahooks.useMemoizedFn)(async () => {
    const requestId = ++requestIdRef.current;
    if (!bookingTicket) {
      setSummary(null);
      return null;
    }
    const next = await bookingTicket.getSummary();
    if (requestId === requestIdRef.current) {
      setSummary(next ?? null);
    }
    return next ?? null;
  });
  (0, import_react.useImperativeHandle)(
    ref,
    () => ({
      refreshSummary
    }),
    [refreshSummary]
  );
  (0, import_react.useEffect)(() => {
    refreshSummary().catch((e) => {
      console.warn("[SalesSdk] SummaryProvider 初始拉取 summary 失败", e);
      setSummary(null);
    });
  }, [bookingTicket, refreshSummary]);
  (0, import_react.useEffect)(() => {
    if ((sales == null ? void 0 : sales.status) === "loading") {
      setSummary(null);
    }
  }, [sales == null ? void 0 : sales.status]);
  (0, import_react.useEffect)(() => {
    if (!bookingTicket) return;
    const off = bookingTicket.effectsOn("onSalesOrderLoaded", () => {
      refreshSummary().catch((e) => {
        console.warn("[SalesSdk] SummaryProvider onSalesOrderLoaded 刷新失败", e);
      });
    });
    return () => {
      try {
        off();
      } catch {
      }
    };
  }, [bookingTicket, refreshSummary]);
  (0, import_react.useEffect)(() => {
    if (!bookingTicket) return;
    const off = bookingTicket.effectsOn("onTempOrderReplaced", () => {
      refreshSummary().catch((e) => {
        console.warn("[SalesSdk] SummaryProvider onTempOrderReplaced 刷新失败", e);
      });
    });
    return () => {
      try {
        off();
      } catch {
      }
    };
  }, [bookingTicket, refreshSummary]);
  const tempOrder = (sales == null ? void 0 : sales.tempOrder) ?? null;
  const tempOrderSyncKey = (0, import_react.useMemo)(() => {
    var _a, _b, _c, _d;
    if (!tempOrder) return "";
    return [
      tempOrder.order_id ?? "",
      ((_a = tempOrder.products) == null ? void 0 : _a.length) ?? 0,
      ((_b = tempOrder.bookings) == null ? void 0 : _b.length) ?? 0,
      ((_c = tempOrder.summary) == null ? void 0 : _c.expect_amount) ?? "",
      ((_d = tempOrder.summary) == null ? void 0 : _d.total_amount) ?? ""
    ].join("|");
  }, [tempOrder]);
  (0, import_react.useEffect)(() => {
    if (!bookingTicket || !tempOrderSyncKey) return;
    refreshSummary().catch((e) => {
      console.warn("[SalesSdk] SummaryProvider tempOrder 变更后刷新失败", e);
    });
  }, [bookingTicket, tempOrderSyncKey, refreshSummary]);
  const value = (0, import_react.useMemo)(() => ({ summary }), [summary]);
  return /* @__PURE__ */ import_react.default.createElement(SalesSdkSummaryRefreshContext.Provider, { value: refreshSummary }, /* @__PURE__ */ import_react.default.createElement(SalesSdkSummaryContext.Provider, { value }, children));
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SalesSdkSummaryContext,
  SalesSdkSummaryProvider,
  SalesSdkSummaryRefreshContext
});
