"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _MapViewManager = require("./MapViewManager");
var _SendCommandOdp = require("./SendCommandOdp");
var _OdpViewManager = require("./OdpViewManager");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const OdpView = (props, parentRef) => {
  const nodeRef = (0, _react.useRef)(null);
  const [onOdpReadyValue, setOnOdpReadyValue] = (0, _react.useState)(null);
  const [onKeyValidationValue, setOnKeyValidationValue] = (0, _react.useState)(null);
  const [onNavigationUpdateValue, setOnNavigationUpdateValue] = (0, _react.useState)(null);
  const [onFacilityUpdateValue, setOnFacilityUpdateValue] = (0, _react.useState)(null);
  (0, _react.useEffect)(() => {
    var _props$onOdpEvent$onR, _props$onOdpEvent;
    if (onOdpReadyValue != null) (_props$onOdpEvent$onR = (_props$onOdpEvent = props.onOdpEvent).onReady) === null || _props$onOdpEvent$onR === void 0 || _props$onOdpEvent$onR.call(_props$onOdpEvent, onOdpReadyValue);
  }, [onOdpReadyValue]);
  (0, _react.useEffect)(() => {
    var _props$onOdpEvent$onK, _props$onOdpEvent2;
    if (onKeyValidationValue != null) (_props$onOdpEvent$onK = (_props$onOdpEvent2 = props.onOdpEvent).onKeyValidation) === null || _props$onOdpEvent$onK === void 0 || _props$onOdpEvent$onK.call(_props$onOdpEvent2, onKeyValidationValue);
  }, [onKeyValidationValue]);
  (0, _react.useEffect)(() => {
    var _props$onOdpEvent$onN, _props$onOdpEvent3;
    if (onNavigationUpdateValue != null) (_props$onOdpEvent$onN = (_props$onOdpEvent3 = props.onOdpEvent).onNavigationUpdate) === null || _props$onOdpEvent$onN === void 0 || _props$onOdpEvent$onN.call(_props$onOdpEvent3, onNavigationUpdateValue);
  }, [onNavigationUpdateValue]);
  (0, _react.useEffect)(() => {
    var _props$onOdpEvent$onF, _props$onOdpEvent4;
    if (onFacilityUpdateValue != null) (_props$onOdpEvent$onF = (_props$onOdpEvent4 = props.onOdpEvent).onFacilityUpdate) === null || _props$onOdpEvent$onF === void 0 || _props$onOdpEvent$onF.call(_props$onOdpEvent4, onFacilityUpdateValue);
  }, [onFacilityUpdateValue]);
  (0, _react.useImperativeHandle)(parentRef, () => {
    const viewId = (0, _reactNative.findNodeHandle)(nodeRef.current);
    const listeners = {
      startOdp: function (key, host) {
        let interval_ms = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1000;
        (0, _SendCommandOdp.startOdp)(viewId, key, host, interval_ms);
      },
      stopOdp: () => {
        (0, _SendCommandOdp.stopOdp)(viewId);
      },
      uploadLogs: () => {
        (0, _SendCommandOdp.uploadLogs)(viewId);
      }
    };
    return listeners;
  });
  (0, _react.useEffect)(() => {
    var _eventEmitter, _eventEmitter2, _eventEmitter3, _eventEmitter4;
    let eventEmitter = null;
    if (_reactNative.Platform.OS == 'android') {
      eventEmitter = _reactNative.DeviceEventEmitter;
    } else if (_reactNative.Platform.OS == 'ios') {
      eventEmitter = new _reactNative.NativeEventEmitter(_MapViewManager.CTPEventEmitter);
    }
    (_eventEmitter = eventEmitter) === null || _eventEmitter === void 0 || _eventEmitter.addListener('ODP_READY', event => setOnOdpReadyValue(event));
    (_eventEmitter2 = eventEmitter) === null || _eventEmitter2 === void 0 || _eventEmitter2.addListener('ODP_KEY_VALIDATION', event => setOnKeyValidationValue(event));
    (_eventEmitter3 = eventEmitter) === null || _eventEmitter3 === void 0 || _eventEmitter3.addListener('ODP_NAVIGATION_UPDATE', event => setOnNavigationUpdateValue(event));
    (_eventEmitter4 = eventEmitter) === null || _eventEmitter4 === void 0 || _eventEmitter4.addListener('ODP_FACILITY_UPDATE', event => setOnFacilityUpdateValue(event));
    return () => {
      var _eventEmitter5, _eventEmitter6, _eventEmitter7, _eventEmitter8;
      (_eventEmitter5 = eventEmitter) === null || _eventEmitter5 === void 0 || _eventEmitter5.removeAllListeners('ODP_READY');
      (_eventEmitter6 = eventEmitter) === null || _eventEmitter6 === void 0 || _eventEmitter6.removeAllListeners('ODP_KEY_VALIDATION');
      (_eventEmitter7 = eventEmitter) === null || _eventEmitter7 === void 0 || _eventEmitter7.removeAllListeners('ODP_NAVIGATION_UPDATE');
      (_eventEmitter8 = eventEmitter) === null || _eventEmitter8 === void 0 || _eventEmitter8.removeAllListeners('ODP_FACILITY_UPDATE');
    };
  }, []);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, _OdpViewManager.OdpViewManager ? /*#__PURE__*/_react.default.createElement(_OdpViewManager.OdpViewManager, {
    ref: nodeRef
  }) : null);
};
const odpViewFwRef = /*#__PURE__*/(0, _react.forwardRef)(OdpView);
var _default = odpViewFwRef;
exports.default = _default;
//# sourceMappingURL=OdpView.js.map