"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 _SendCommand = require("./SendCommand");
var _Compass = _interopRequireDefault(require("./Compass"));
var _PathSimulator = require("./PathSimulator");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 updateGeojsonTimestamps = geojson => {
  if (_reactNative.Platform.OS == 'android') {
    geojson.startTime = geojson.startTime * 1000;
    geojson.endTime = geojson.endTime * 1000;
  }
  return geojson;
};
const MapView = (props, parentRef) => {
  var _props$compassStyle, _props$compassStyle2, _props$compassStyle3, _props$compassStyle4, _props$onMapMessage10;
  const nodeRef = (0, _react.useRef)(null);
  const compassStyle = _reactNative.StyleSheet.create({
    compass: {
      height: props.style.visible ? (props === null || props === void 0 || (_props$compassStyle = props.compassStyle) === null || _props$compassStyle === void 0 ? void 0 : _props$compassStyle.size) ?? 0 : 0,
      width: props.style.visible ? (props === null || props === void 0 || (_props$compassStyle2 = props.compassStyle) === null || _props$compassStyle2 === void 0 ? void 0 : _props$compassStyle2.size) ?? 0 : 0,
      borderRadius: 5,
      alignItems: 'center',
      justifyContent: 'center',
      zIndex: 1,
      position: 'absolute',
      right: props === null || props === void 0 || (_props$compassStyle3 = props.compassStyle) === null || _props$compassStyle3 === void 0 ? void 0 : _props$compassStyle3.right,
      top: props === null || props === void 0 || (_props$compassStyle4 = props.compassStyle) === null || _props$compassStyle4 === void 0 ? void 0 : _props$compassStyle4.top,
      flex: 1,
      resizeMode: 'contain'
    }
  });
  const [compassTransform, setCompassTransform] = (0, _react.useState)({
    rotate: '0deg'
  });

  // CALLBACK DATA
  const [onNavigationPathGeneratedValue, setOnNavigationPathGeneratedValue] = (0, _react.useState)(null);
  const [onRerouteNavigationPathGeneratedValue, setOnRerouteNavigationPathGeneratedValue] = (0, _react.useState)(null);
  const [onDestinationSelectedValue, setOnDestinationSelectedValue] = (0, _react.useState)(null);
  const [onUserPositionUpdatedValue, setOnUserPositionUpdatedValue] = (0, _react.useState)(null);
  const [onSearchDestinationsGeneratedValue, setOnSearchDestinationsGeneratedValue] = (0, _react.useState)(null);
  const [onMapTappedGenericValue, setOnMapTappedGenericValue] = (0, _react.useState)(null);
  const [onGeofencesChangedValue, setOnGeofencesChangedValue] = (0, _react.useState)(null);
  const [onErrorValue, setOnErrorValue] = (0, _react.useState)(null);
  const [onVenueLoadedValue, setOnVenueLoadedValue] = (0, _react.useState)(null);

  // STATE VARIABLES
  // const [pathSimulator, _] = useState<PathSimulator>(new PathSimulator((loc: number[]) => {setUserPosition(findNodeHandle(nodeRef.current), loc[0]!, loc[1]!, 0, 5, null);}));

  const simRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    if (_reactNative.Platform.OS == 'android') {
      (0, _SendCommand.createFragment)((0, _reactNative.findNodeHandle)(nodeRef.current));
    }
    setTimeout(function () {
      (0, _SendCommand.setMapVenue)({
        viewId: (0, _reactNative.findNodeHandle)(nodeRef.current),
        credentials: props.credentials,
        mapOptions: props.mapOptions
      });
    }, 500);
  }, []);
  (0, _react.useEffect)(() => {
    if (props.style.visible) {
      (0, _SendCommand.centerMap)((0, _reactNative.findNodeHandle)(nodeRef.current), 0, 5, 100);
    }
  }, [props.style.visible]);
  (0, _react.useEffect)(() => {
    var _props$onMapMessage$o, _props$onMapMessage;
    if (onDestinationSelectedValue != null) (_props$onMapMessage$o = (_props$onMapMessage = props.onMapMessage).onDestinationSelected) === null || _props$onMapMessage$o === void 0 || _props$onMapMessage$o.call(_props$onMapMessage, onDestinationSelectedValue);
  }, [onDestinationSelectedValue]);
  (0, _react.useEffect)(() => {
    if (onNavigationPathGeneratedValue != null) {
      var _props$onMapMessage$o2, _props$onMapMessage2;
      let pathId, instructions, start, end, jibestreamPath, path;
      ({
        pathId,
        instructions,
        start,
        end,
        jibestreamPath,
        path
      } = onNavigationPathGeneratedValue);
      (_props$onMapMessage$o2 = (_props$onMapMessage2 = props.onMapMessage).onNavigationPathGenerated) === null || _props$onMapMessage$o2 === void 0 || _props$onMapMessage$o2.call(_props$onMapMessage2, pathId, instructions, start, end, jibestreamPath, updateGeojsonTimestamps(path));
    }
  }, [onNavigationPathGeneratedValue]);
  (0, _react.useEffect)(() => {
    if (onRerouteNavigationPathGeneratedValue != null) {
      var _props$onMapMessage$o3, _props$onMapMessage3;
      let pathId, instructions, start, end, jibestreamPath, path;
      ({
        pathId,
        instructions,
        start,
        end,
        jibestreamPath,
        path
      } = onRerouteNavigationPathGeneratedValue);
      (_props$onMapMessage$o3 = (_props$onMapMessage3 = props.onMapMessage).onRerouteNavigationPathGenerated) === null || _props$onMapMessage$o3 === void 0 || _props$onMapMessage$o3.call(_props$onMapMessage3, pathId, instructions, start, end, jibestreamPath, updateGeojsonTimestamps(path));
    }
  }, [onRerouteNavigationPathGeneratedValue]);
  (0, _react.useEffect)(() => {
    var _props$onMapMessage$o4, _props$onMapMessage4;
    if (onUserPositionUpdatedValue != null) (_props$onMapMessage$o4 = (_props$onMapMessage4 = props.onMapMessage).onUserPositionUpdated) === null || _props$onMapMessage$o4 === void 0 || _props$onMapMessage$o4.call(_props$onMapMessage4, onUserPositionUpdatedValue);
  }, [onUserPositionUpdatedValue]);
  (0, _react.useEffect)(() => {
    var _props$onMapMessage$o5, _props$onMapMessage5;
    if (onSearchDestinationsGeneratedValue != null) (_props$onMapMessage$o5 = (_props$onMapMessage5 = props.onMapMessage).onSearchDestinationsGenerated) === null || _props$onMapMessage$o5 === void 0 || _props$onMapMessage$o5.call(_props$onMapMessage5, onSearchDestinationsGeneratedValue);
  }, [onSearchDestinationsGeneratedValue]);
  (0, _react.useEffect)(() => {
    var _props$onMapMessage$o6, _props$onMapMessage6;
    if (onMapTappedGenericValue != null) (_props$onMapMessage$o6 = (_props$onMapMessage6 = props.onMapMessage).onMapTappedGeneric) === null || _props$onMapMessage$o6 === void 0 || _props$onMapMessage$o6.call(_props$onMapMessage6, onMapTappedGenericValue);
  }, [onMapTappedGenericValue]);
  (0, _react.useEffect)(() => {
    var _props$onMapMessage$o7, _props$onMapMessage7;
    if (onErrorValue != null) (_props$onMapMessage$o7 = (_props$onMapMessage7 = props.onMapMessage).onError) === null || _props$onMapMessage$o7 === void 0 || _props$onMapMessage$o7.call(_props$onMapMessage7, onErrorValue);
  }, [onErrorValue]);
  (0, _react.useEffect)(() => {
    var _props$onMapMessage$o8, _props$onMapMessage8;
    if (onVenueLoadedValue != null) (_props$onMapMessage$o8 = (_props$onMapMessage8 = props.onMapMessage).onVenueLoaded) === null || _props$onMapMessage$o8 === void 0 || _props$onMapMessage$o8.call(_props$onMapMessage8, onVenueLoadedValue);
  }, [onVenueLoadedValue]);
  (0, _react.useEffect)(() => {
    if (onGeofencesChangedValue != null) {
      var _props$onMapMessage$o9, _props$onMapMessage9;
      let entered, geofences;
      ({
        entered,
        geofences
      } = onGeofencesChangedValue);
      console.log('Geofences Changed: ', entered, geofences);
      (_props$onMapMessage$o9 = (_props$onMapMessage9 = props.onMapMessage).onGeofencesChanged) === null || _props$onMapMessage$o9 === void 0 || _props$onMapMessage$o9.call(_props$onMapMessage9, entered, geofences);
    }
  }, [onGeofencesChangedValue]);
  (0, _react.useImperativeHandle)(parentRef, () => {
    const viewId = (0, _reactNative.findNodeHandle)(nodeRef.current);
    const listeners = {
      setMapVenue: (credentials, mapOptions) => {
        setTimeout(function () {
          (0, _SendCommand.setMapVenue)({
            viewId,
            credentials,
            mapOptions
          });
        }, 500);
      },
      centerMapOnUser: function (orientation) {
        let zoom = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
        let animationDuration = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1000;
        (0, _SendCommand.centerMap)(viewId, orientation, zoom, animationDuration);
      },
      setFollowBlueDot: function (enabled) {
        let rotate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
        let interval = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 500;
        (0, _SendCommand.setFollowBlueDot)(viewId, enabled, rotate, interval);
      },
      setUserPosition: (lat, long, heading, confidence) => {
        (0, _SendCommand.setUserPosition)(viewId, lat, long, heading, confidence, null);
      },
      setUserPositionXY: (x, y, heading, confidence) => {
        (0, _SendCommand.setUserPosition)(viewId, x, y, heading, confidence, 'xy');
      },
      clearWayfindingPath: () => {
        (0, _SendCommand.clearWayfindingPath)(viewId);
      },
      wayfindToWaypoint: waypointId => {
        (0, _SendCommand.wayfindToWaypoint)(viewId, waypointId);
      },
      wayfindBetweenWaypoints: (originId, destinationId) => {
        (0, _SendCommand.wayfindBetweenWaypoints)(viewId, originId, destinationId);
      },
      drawNavigationPath: (pathId, _ref) => {
        let {
          shouldFocus,
          style,
          originIcon,
          destinationIcon
        } = _ref;
        (0, _SendCommand.drawNavigationPath)(viewId, pathId, shouldFocus ?? true, style ?? {}, originIcon, destinationIcon);
      },
      styleUnitsForDestination: (destinationId, style) => {
        (0, _SendCommand.styleUnitsForDestination)(viewId, destinationId, style);
      },
      addMapIcon: (coordinates, coordType, icon) => {
        (0, _SendCommand.addMapIcon)(viewId, coordinates, coordType, icon);
      },
      // Path Simulator
      startPathSimulation: (path, options) => {
        simRef.current.startPathSimulation(path, options);
      },
      controlSimulation: command => {
        return simRef.current.controlSimulation(command);
      },
      getSimulationStatus: () => {
        return simRef.current.getSimulationStatus();
      },
      getSimulationsPaths: () => {
        return simRef.current.getSimulationsPaths();
      }
    };
    return listeners;
  });
  (0, _react.useEffect)(() => {
    var _eventEmitter, _eventEmitter2, _eventEmitter3, _eventEmitter4, _eventEmitter5, _eventEmitter6, _eventEmitter7, _eventEmitter8, _eventEmitter9, _eventEmitter10;
    console.log('Initializing Event Emitter');
    let eventEmitter = null;
    if (_reactNative.Platform.OS == 'android') {
      eventEmitter = _reactNative.DeviceEventEmitter;
    } else if (_reactNative.Platform.OS == 'ios') {
      console.log(_MapViewManager.CTPEventEmitter);
      eventEmitter = new _reactNative.NativeEventEmitter(_MapViewManager.CTPEventEmitter);
    }
    (_eventEmitter = eventEmitter) === null || _eventEmitter === void 0 || _eventEmitter.addListener('DESTINATION_SELECTED', data => setOnDestinationSelectedValue(data));
    (_eventEmitter2 = eventEmitter) === null || _eventEmitter2 === void 0 || _eventEmitter2.addListener('NAVIGATION_PATH_GENERATED', data => setOnNavigationPathGeneratedValue(data));
    (_eventEmitter3 = eventEmitter) === null || _eventEmitter3 === void 0 || _eventEmitter3.addListener('REROUTE_NAVIGATION_PATH_GENERATED', data => setOnRerouteNavigationPathGeneratedValue(data));
    (_eventEmitter4 = eventEmitter) === null || _eventEmitter4 === void 0 || _eventEmitter4.addListener('USER_POSITION_UPDATED', userPosition => setOnUserPositionUpdatedValue(userPosition));
    (_eventEmitter5 = eventEmitter) === null || _eventEmitter5 === void 0 || _eventEmitter5.addListener('GENERATED_SEARCH_DESTINATIONS', destinations => setOnSearchDestinationsGeneratedValue(destinations));
    (_eventEmitter6 = eventEmitter) === null || _eventEmitter6 === void 0 || _eventEmitter6.addListener('MAP_TAPPED_GENERIC', event => setOnMapTappedGenericValue(event));
    (_eventEmitter7 = eventEmitter) === null || _eventEmitter7 === void 0 || _eventEmitter7.addListener('SCREEN_ROTATED', event => {
      const newTransform = {
        rotate: Math.round(event.degrees).toString() + 'deg'
      };
      setCompassTransform(newTransform);
    });
    (_eventEmitter8 = eventEmitter) === null || _eventEmitter8 === void 0 || _eventEmitter8.addListener('ERROR', msg => setOnErrorValue(msg));
    (_eventEmitter9 = eventEmitter) === null || _eventEmitter9 === void 0 || _eventEmitter9.addListener('VENUE_LOADED', venueId => setOnVenueLoadedValue(venueId));
    (_eventEmitter10 = eventEmitter) === null || _eventEmitter10 === void 0 || _eventEmitter10.addListener('GEOFENCES_CHANGED', data => setOnGeofencesChangedValue(data));
    return () => {
      var _eventEmitter11, _eventEmitter12, _eventEmitter13, _eventEmitter14, _eventEmitter15, _eventEmitter16, _eventEmitter17, _eventEmitter18, _eventEmitter19, _eventEmitter20;
      console.log('Removing Event Emitters');
      (_eventEmitter11 = eventEmitter) === null || _eventEmitter11 === void 0 || _eventEmitter11.removeAllListeners('DESTINATION_SELECTED');
      (_eventEmitter12 = eventEmitter) === null || _eventEmitter12 === void 0 || _eventEmitter12.removeAllListeners('NAVIGATION_PATH_GENERATED');
      (_eventEmitter13 = eventEmitter) === null || _eventEmitter13 === void 0 || _eventEmitter13.removeAllListeners('USER_POSITION_UPDATED');
      (_eventEmitter14 = eventEmitter) === null || _eventEmitter14 === void 0 || _eventEmitter14.removeAllListeners('GENERATED_SEARCH_DESTINATIONS');
      (_eventEmitter15 = eventEmitter) === null || _eventEmitter15 === void 0 || _eventEmitter15.removeAllListeners('MAP_TAPPED_GENERIC');
      (_eventEmitter16 = eventEmitter) === null || _eventEmitter16 === void 0 || _eventEmitter16.removeAllListeners('ERROR');
      (_eventEmitter17 = eventEmitter) === null || _eventEmitter17 === void 0 || _eventEmitter17.removeAllListeners('VENUE_LOADED');
      (_eventEmitter18 = eventEmitter) === null || _eventEmitter18 === void 0 || _eventEmitter18.removeAllListeners('SCREEN_ROTATED');
      (_eventEmitter19 = eventEmitter) === null || _eventEmitter19 === void 0 || _eventEmitter19.removeAllListeners('GEOFENCES_CHANGED');
      (_eventEmitter20 = eventEmitter) === null || _eventEmitter20 === void 0 || _eventEmitter20.removeAllListeners('REROUTE_NAVIGATION_PATH_GENERATED');
    };
  }, []);
  return /*#__PURE__*/_react.default.createElement(_reactNative.SafeAreaView, {
    style: {
      flex: 1,
      width: props.style.visible ? props.style.width : 0,
      height: props.style.visible ? props.style.height : 0
    }
  }, /*#__PURE__*/_react.default.createElement(_PathSimulator.PathSimulator, {
    ref: simRef,
    updateUserLocation: (pos, heading) => {
      (0, _SendCommand.setUserPosition)((0, _reactNative.findNodeHandle)(nodeRef.current), pos[0], pos[1], heading, 100, null);
    },
    onSimStatusChanged: (_props$onMapMessage10 = props.onMapMessage) === null || _props$onMapMessage10 === void 0 ? void 0 : _props$onMapMessage10.onSimulationStatusChanged
  }), /*#__PURE__*/_react.default.createElement(_Compass.default, {
    style: [compassStyle.compass, {
      transform: [compassTransform]
    }]
  }), _MapViewManager.MapViewManager ? /*#__PURE__*/_react.default.createElement(_MapViewManager.MapViewManager, {
    style: _reactNative.Platform.OS == 'android' ? {
      height: props.style.visible ? _reactNative.PixelRatio.getPixelSizeForLayoutSize(props.style.height) : 0,
      width: props.style.visible ? _reactNative.PixelRatio.getPixelSizeForLayoutSize(props.style.width) : 0
    } : {
      height: props.style.visible ? props.style.height / 1 : 0,
      width: props.style.visible ? props.style.width / 1 : 0
    },
    ref: nodeRef
  }) : null);
};
const mapViewFwRef = /*#__PURE__*/(0, _react.forwardRef)(MapView);
var _default = mapViewFwRef;
exports.default = _default;
//# sourceMappingURL=MapView.js.map