import { useCallback, useEffect } from 'react';
import useEngineContext from "../../../hooks/useEngineContext";
export var useNativeScanner = function useNativeScanner(onChange) {
  var _context$appHelper, _interaction$utils3, _interaction$utils3$i;
  var context = useEngineContext();
  var _ref = ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {},
    interaction = _ref.interaction,
    isTerminal = _ref.isTerminal;
  useEffect(function () {
    var _interaction$utils, _interaction$utils$mo;
    // 获取客户端扫描结果
    interaction === null || interaction === void 0 || (_interaction$utils = interaction.utils) === null || _interaction$utils === void 0 || (_interaction$utils$mo = _interaction$utils.mountFunction) === null || _interaction$utils$mo === void 0 || _interaction$utils$mo.call(_interaction$utils, 'global', 'nativeScannerResult', function (strVal) {
      try {
        var _val = JSON.parse(strVal);
        onChange === null || onChange === void 0 || onChange(_val.value);
      } catch (err) {
        console.error(err);
      }
    });
  }, []);
  var activeNativeScanner = useCallback(function () {
    var _interaction$utils2, _interaction$utils2$p;
    interaction === null || interaction === void 0 || (_interaction$utils2 = interaction.utils) === null || _interaction$utils2 === void 0 || (_interaction$utils2$p = _interaction$utils2.postMessageToApp) === null || _interaction$utils2$p === void 0 || _interaction$utils2$p.call(_interaction$utils2, {
      module: 'global',
      key: 'active_native_scanner',
      data: {
        source: 'pisellInformationEntry'
      }
    });
  }, []);
  return {
    activeNativeScanner: activeNativeScanner,
    isTerminal: isTerminal && isTerminal() || (interaction === null || interaction === void 0 || (_interaction$utils3 = interaction.utils) === null || _interaction$utils3 === void 0 || (_interaction$utils3$i = _interaction$utils3.isAppEnv) === null || _interaction$utils3$i === void 0 ? void 0 : _interaction$utils3$i.call(_interaction$utils3))
  };
};