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]);

  /**
   * 解析宿主注入的「扫码焦点栈」入口，兼容两种注入位置：
   * appHelper.utils.acquireScanFocus 或 appHelper.utils.businessUtils.acquireScanFocus。
   */
  var getAcquireScanFocus = useCallback(function () {
    var _engineContext$appHel4, _utils$businessUtils;
    var utils = engineContext === null || engineContext === void 0 || (_engineContext$appHel4 = engineContext.appHelper) === null || _engineContext$appHel4 === void 0 ? void 0 : _engineContext$appHel4.utils;
    if (typeof (utils === null || utils === void 0 ? void 0 : utils.acquireScanFocus) === 'function') {
      return utils.acquireScanFocus;
    }
    if (typeof (utils === null || utils === void 0 || (_utils$businessUtils = utils.businessUtils) === null || _utils$businessUtils === void 0 ? void 0 : _utils$businessUtils.acquireScanFocus) === 'function') {
      return utils.businessUtils.acquireScanFocus;
    }
    return undefined;
  }, [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,
    getAcquireScanFocus: getAcquireScanFocus
  });
  return null;
};
SalesSdkGlobalScanSubscriber.displayName = 'SalesSdkGlobalScanSubscriber';
export default SalesSdkGlobalScanSubscriber;