import { useCallback, useContext } from 'react';
import { locales } from '@pisell/utils';
import { useToast } from '@pisell/materials';
import useEngineContext from "../../../hooks/useEngineContext";
import { SalesSdkContext } from "../context/SalesSdkContext";
import { SalesSdkUIHostsContext } from "../context/SalesSdkUIHostsContext";
import { SalesSdkRootRefreshContext } from "../context/SalesSdkProvider";
import { useSalesSdkCart } from "../hooks/useSalesSdkCart";
import { createGlobalScanHostBridge } from "../utils/createGlobalScanHostBridge";
import { useSalesSdkGlobalScanListener } from "../hooks/useSalesSdkGlobalScanListener";
/**
 * 挂载在 SalesSdkCartProvider 内，订阅 pubsub 扫码并转发到 OS handleGlobalScanCode。
 * 无 UI 渲染。
 *
 * @example
 * ```tsx
 * <SalesSdkCartProvider>
 *   <SalesSdkGlobalScanSubscriber enabled={enableGlobalScanListener} isActive={isActive} />
 *   {children}
 * </SalesSdkCartProvider>
 * ```
 */
export var SalesSdkGlobalScanSubscriber = function SalesSdkGlobalScanSubscriber(_ref) {
  var _engineContext$appHel, _sales$bookingTicket, _sales$tempOrder2;
  var enabled = _ref.enabled,
    isActive = _ref.isActive,
    businessCode = _ref.businessCode;
  var sales = useContext(SalesSdkContext);
  var uiHosts = useContext(SalesSdkUIHostsContext);
  var refreshRootSnapshot = useContext(SalesSdkRootRefreshContext);
  var cart = useSalesSdkCart();
  var engineContext = useEngineContext();
  var Toast = useToast();
  var action = engineContext === null || engineContext === void 0 || (_engineContext$appHel = engineContext.appHelper) === null || _engineContext$appHel === void 0 || (_engineContext$appHel = _engineContext$appHel.utils) === null || _engineContext$appHel === void 0 ? void 0 : _engineContext$appHel.action;
  var bookingTicket = (_sales$bookingTicket = sales === null || sales === void 0 ? void 0 : sales.bookingTicket) !== null && _sales$bookingTicket !== void 0 ? _sales$bookingTicket : null;
  var getPubsub = useCallback(function () {
    var _engineContext$appHel2, _engineContext$appHel3;
    return engineContext === null || engineContext === void 0 || (_engineContext$appHel2 = engineContext.appHelper) === null || _engineContext$appHel2 === void 0 || (_engineContext$appHel2 = _engineContext$appHel2.utils) === null || _engineContext$appHel2 === void 0 || (_engineContext$appHel3 = _engineContext$appHel2.getApp) === null || _engineContext$appHel3 === void 0 || (_engineContext$appHel3 = _engineContext$appHel3.call(_engineContext$appHel2)) === null || _engineContext$appHel3 === void 0 ? void 0 : _engineContext$appHel3.pubsub;
  }, [engineContext]);
  var bridgeFactory = useCallback(function () {
    var _sales$tempOrder;
    if (!uiHosts || !refreshRootSnapshot) return undefined;
    var tempOrderCustomerId = Number(sales === null || sales === void 0 || (_sales$tempOrder = sales.tempOrder) === null || _sales$tempOrder === void 0 ? void 0 : _sales$tempOrder.customer_id);
    return createGlobalScanHostBridge({
      uiHosts: uiHosts,
      action: action,
      cart: cart,
      businessCode: businessCode,
      refreshRootSnapshot: refreshRootSnapshot,
      getCustomerId: function getCustomerId() {
        return Number.isFinite(tempOrderCustomerId) ? tempOrderCustomerId : undefined;
      },
      toast: Toast,
      getLocaleText: function getLocaleText(key) {
        return locales.getText(key);
      }
    });
  }, [uiHosts, action, cart, businessCode, refreshRootSnapshot, Toast, sales === null || sales === void 0 || (_sales$tempOrder2 = sales.tempOrder) === null || _sales$tempOrder2 === void 0 ? void 0 : _sales$tempOrder2.customer_id]);
  useSalesSdkGlobalScanListener({
    enabled: enabled,
    isActive: isActive,
    bookingTicket: bookingTicket,
    bridgeFactory: bridgeFactory,
    getPubsub: getPubsub
  });
  return null;
};
SalesSdkGlobalScanSubscriber.displayName = 'SalesSdkGlobalScanSubscriber';
export default SalesSdkGlobalScanSubscriber;