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/SalesSdkProductContext.tsx
var SalesSdkProductContext_exports = {};
__export(SalesSdkProductContext_exports, {
  SalesSdkProductContext: () => SalesSdkProductContext,
  SalesSdkProductProvider: () => SalesSdkProductProvider
});
module.exports = __toCommonJS(SalesSdkProductContext_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_SalesSdkContext = require("./SalesSdkContext");
var SalesSdkProductContext = (0, import_react.createContext)(null);
SalesSdkProductContext.displayName = "SalesSdkProductContext";
var EMPTY_PRODUCTS = [];
var SalesSdkProductProvider = ({ children }) => {
  const sales = (0, import_react.useContext)(import_SalesSdkContext.SalesSdkContext);
  const bookingTicket = (sales == null ? void 0 : sales.bookingTicket) ?? null;
  const [products, setProducts] = (0, import_react.useState)(EMPTY_PRODUCTS);
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [error, setError] = (0, import_react.useState)(null);
  (0, import_react.useEffect)(() => {
    var _a;
    setProducts(((_a = bookingTicket == null ? void 0 : bookingTicket.getProductCatalog) == null ? void 0 : _a.call(bookingTicket).products) ?? EMPTY_PRODUCTS);
  }, [bookingTicket]);
  (0, import_react.useEffect)(() => {
    if (!bookingTicket) return;
    const off = bookingTicket.effectsOn("onProductsLoaded", (payload) => {
      var _a;
      const catalog = (_a = bookingTicket.getProductCatalog) == null ? void 0 : _a.call(bookingTicket);
      setProducts((catalog == null ? void 0 : catalog.products) ?? (Array.isArray(payload) ? payload : EMPTY_PRODUCTS));
    });
    return () => {
      try {
        off();
      } catch {
      }
    };
  }, [bookingTicket]);
  const load = (0, import_ahooks.useMemoizedFn)(
    async (params = {}, options) => {
      var _a;
      if (!bookingTicket) throw new Error("[SalesSdk] Products: bookingTicket 未初始化");
      setLoading(true);
      setError(null);
      try {
        const list = await bookingTicket.loadProducts(params, {
          subscriberId: options == null ? void 0 : options.subscriberId
        });
        setProducts(((_a = bookingTicket.getProductCatalog) == null ? void 0 : _a.call(bookingTicket).products) ?? list ?? EMPTY_PRODUCTS);
        return list || EMPTY_PRODUCTS;
      } catch (e) {
        const err = e instanceof Error ? e : new Error(String((e == null ? void 0 : e.message) ?? e));
        setError(err);
        throw err;
      } finally {
        setLoading(false);
      }
    }
  );
  const loadDetail = (0, import_ahooks.useMemoizedFn)(
    async (params) => {
      if (!bookingTicket) throw new Error("[SalesSdk] Products: bookingTicket 未初始化");
      return bookingTicket.loadProductDetail(params);
    }
  );
  const findByIds = (0, import_ahooks.useMemoizedFn)(async (ids) => {
    var _a;
    if (!bookingTicket) throw new Error("[SalesSdk] Products: bookingTicket 未初始化");
    const result = await bookingTicket.getProductByIds(ids);
    setProducts(((_a = bookingTicket.getProductCatalog) == null ? void 0 : _a.call(bookingTicket).products) ?? products);
    return result || EMPTY_PRODUCTS;
  });
  const scanGlobalListener = (0, import_ahooks.useMemoizedFn)((cb) => {
    if (!bookingTicket) return () => {
    };
    return bookingTicket.scanGlobalListener(cb);
  });
  const scanCustomerListener = (0, import_ahooks.useMemoizedFn)((cb) => {
    if (!bookingTicket) return () => {
    };
    return bookingTicket.scanCustomerListener(cb);
  });
  const scanUniversal = (0, import_ahooks.useMemoizedFn)(
    (cb, key) => {
      if (!bookingTicket) return () => {
      };
      return bookingTicket.scanUniversalListener(cb, key);
    }
  );
  const activateCamera = (0, import_ahooks.useMemoizedFn)((data) => {
    bookingTicket == null ? void 0 : bookingTicket.activateCamera(data);
  });
  const enableScan = (0, import_ahooks.useMemoizedFn)(() => {
    bookingTicket == null ? void 0 : bookingTicket.enableAllScanListeners();
  });
  const disableScan = (0, import_ahooks.useMemoizedFn)(() => {
    bookingTicket == null ? void 0 : bookingTicket.disableAllScanListeners();
  });
  const value = (0, import_react.useMemo)(
    () => ({
      products,
      loading,
      error,
      load,
      loadDetail,
      findByIds,
      scanGlobalListener,
      scanCustomerListener,
      scanUniversal,
      activateCamera,
      enableScan,
      disableScan
    }),
    [
      products,
      loading,
      error,
      load,
      loadDetail,
      findByIds,
      scanGlobalListener,
      scanCustomerListener,
      scanUniversal,
      activateCamera,
      enableScan,
      disableScan
    ]
  );
  return /* @__PURE__ */ import_react.default.createElement(SalesSdkProductContext.Provider, { value }, children);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SalesSdkProductContext,
  SalesSdkProductProvider
});
