import React, { useImperativeHandle, useEffect, useRef, forwardRef, useState } from 'react';
import { findNodeHandle, DeviceEventEmitter, NativeEventEmitter, Platform } from 'react-native';
import { CTPEventEmitter } from './MapViewManager';
import { startOdp, stopOdp, uploadLogs } from './SendCommandOdp';
import { OdpViewManager } from './OdpViewManager';
const OdpView = (props, parentRef) => {
  const nodeRef = useRef(null);
  const [onOdpReadyValue, setOnOdpReadyValue] = useState(null);
  const [onKeyValidationValue, setOnKeyValidationValue] = useState(null);
  const [onNavigationUpdateValue, setOnNavigationUpdateValue] = useState(null);
  const [onFacilityUpdateValue, setOnFacilityUpdateValue] = useState(null);
  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]);
  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]);
  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]);
  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]);
  useImperativeHandle(parentRef, () => {
    const viewId = findNodeHandle(nodeRef.current);
    const listeners = {
      startOdp: (key, host) => {
        startOdp(viewId, key, host);
      },
      stopOdp: () => {
        stopOdp(viewId);
      },
      uploadLogs: () => {
        uploadLogs(viewId);
      }
    };
    return listeners;
  });
  useEffect(() => {
    var _eventEmitter, _eventEmitter2, _eventEmitter3, _eventEmitter4;
    console.log('Initializing Event Emitter');
    let eventEmitter = null;
    if (Platform.OS == 'android') {
      eventEmitter = DeviceEventEmitter;
    } else if (Platform.OS == 'ios') {
      console.log(CTPEventEmitter);
      eventEmitter = new NativeEventEmitter(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;
      console.log('Removing Event Emitters');
      (_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.createElement(React.Fragment, null, OdpViewManager ? /*#__PURE__*/React.createElement(OdpViewManager, {
    ref: nodeRef
  }) : null);
};
const odpViewFwRef = /*#__PURE__*/forwardRef(OdpView);
export default odpViewFwRef;
//# sourceMappingURL=OdpView.js.map