"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");
var _Instructions = require("./Instructions");
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 MapView = (props, parentRef) => {
  var _props$compassStyle, _props$compassStyle2, _props$compassStyle3, _props$compassStyle4, _props$onMapMessage11;
  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 [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);
  }, []);

  //   useEffect(() => {
  //     if (props.style.visible) {
  //       centerMap(findNodeHandle(nodeRef.current), {
  //         rotation: 0,
  //         zoom: 2,
  //         animationDuration: 100,
  //       });
  //     }
  //   }, [props.style.visible]);

  (0, _react.useEffect)(() => {
    if (onNavigationPathGeneratedValue != null) {
      var _props$onMapMessage$o2, _props$onMapMessage2, _props$mapOptions, _props$credentials;
      let pathId, instructions, start, end, jibestreamPath;
      ({
        pathId,
        instructions,
        start,
        end,
        jibestreamPath
      } = onNavigationPathGeneratedValue);
      if (!jibestreamPath || !instructions) {
        var _props$onMapMessage$o, _props$onMapMessage;
        (_props$onMapMessage$o = (_props$onMapMessage = props.onMapMessage).onNavigationPathGenerated) === null || _props$onMapMessage$o === void 0 || _props$onMapMessage$o.call(_props$onMapMessage, pathId, instructions, start, end, jibestreamPath, undefined);
        return;
      }
      const processedInstructions = (0, _Instructions.preProcessPath)(jibestreamPath, instructions);
      (_props$onMapMessage$o2 = (_props$onMapMessage2 = props.onMapMessage).onNavigationPathGenerated) === null || _props$onMapMessage$o2 === void 0 || _props$onMapMessage$o2.call(_props$onMapMessage2, pathId, processedInstructions, start, end, jibestreamPath, (0, _Instructions.generateGeojsonPath)(processedInstructions, jibestreamPath, ((_props$mapOptions = props.mapOptions) === null || _props$mapOptions === void 0 || (_props$mapOptions = _props$mapOptions.navigation) === null || _props$mapOptions === void 0 ? void 0 : _props$mapOptions.directionsUnit) ?? 'FEET', ((_props$credentials = props.credentials) === null || _props$credentials === void 0 ? void 0 : _props$credentials.locale) ?? 'en-US'));
    }
  }, [onNavigationPathGeneratedValue]);
  (0, _react.useEffect)(() => {
    if (onRerouteNavigationPathGeneratedValue != null) {
      var _props$onMapMessage$o4, _props$onMapMessage4, _props$mapOptions2, _props$credentials2;
      let pathId, instructions, start, end, jibestreamPath;
      ({
        pathId,
        instructions,
        start,
        end,
        jibestreamPath
      } = onRerouteNavigationPathGeneratedValue);
      if (!jibestreamPath || !instructions) {
        var _props$onMapMessage$o3, _props$onMapMessage3;
        (_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, undefined);
        return;
      }
      const processedInstructions = (0, _Instructions.preProcessPath)(jibestreamPath, instructions);
      (_props$onMapMessage$o4 = (_props$onMapMessage4 = props.onMapMessage).onRerouteNavigationPathGenerated) === null || _props$onMapMessage$o4 === void 0 || _props$onMapMessage$o4.call(_props$onMapMessage4, pathId, processedInstructions, start, end, jibestreamPath, (0, _Instructions.generateGeojsonPath)(processedInstructions, jibestreamPath, ((_props$mapOptions2 = props.mapOptions) === null || _props$mapOptions2 === void 0 || (_props$mapOptions2 = _props$mapOptions2.navigation) === null || _props$mapOptions2 === void 0 ? void 0 : _props$mapOptions2.directionsUnit) ?? 'FEET', ((_props$credentials2 = props.credentials) === null || _props$credentials2 === void 0 ? void 0 : _props$credentials2.locale) ?? 'en-US'));
    }
  }, [onRerouteNavigationPathGeneratedValue]);
  (0, _react.useEffect)(() => {
    var _props$onMapMessage$o5, _props$onMapMessage5;
    if (onUserPositionUpdatedValue != null) (_props$onMapMessage$o5 = (_props$onMapMessage5 = props.onMapMessage).onUserPositionUpdated) === null || _props$onMapMessage$o5 === void 0 || _props$onMapMessage$o5.call(_props$onMapMessage5, onUserPositionUpdatedValue);
  }, [onUserPositionUpdatedValue]);
  (0, _react.useEffect)(() => {
    var _props$onMapMessage$o6, _props$onMapMessage6;
    if (onSearchDestinationsGeneratedValue != null) (_props$onMapMessage$o6 = (_props$onMapMessage6 = props.onMapMessage).onSearchDestinationsGenerated) === null || _props$onMapMessage$o6 === void 0 || _props$onMapMessage$o6.call(_props$onMapMessage6, onSearchDestinationsGeneratedValue);
  }, [onSearchDestinationsGeneratedValue]);
  (0, _react.useEffect)(() => {
    var _props$onMapMessage$o7, _props$onMapMessage7;
    if (onMapTappedGenericValue != null) (_props$onMapMessage$o7 = (_props$onMapMessage7 = props.onMapMessage).onMapTappedGeneric) === null || _props$onMapMessage$o7 === void 0 || _props$onMapMessage$o7.call(_props$onMapMessage7, onMapTappedGenericValue);
  }, [onMapTappedGenericValue]);
  (0, _react.useEffect)(() => {
    var _props$onMapMessage$o8, _props$onMapMessage8;
    if (onErrorValue != null) (_props$onMapMessage$o8 = (_props$onMapMessage8 = props.onMapMessage).onError) === null || _props$onMapMessage$o8 === void 0 || _props$onMapMessage$o8.call(_props$onMapMessage8, onErrorValue);
  }, [onErrorValue]);
  (0, _react.useEffect)(() => {
    var _props$onMapMessage$o9, _props$onMapMessage9;
    if (onVenueLoadedValue != null) (_props$onMapMessage$o9 = (_props$onMapMessage9 = props.onMapMessage).onVenueLoaded) === null || _props$onMapMessage$o9 === void 0 || _props$onMapMessage$o9.call(_props$onMapMessage9, onVenueLoadedValue);
  }, [onVenueLoadedValue]);
  (0, _react.useEffect)(() => {
    if (onGeofencesChangedValue != null) {
      var _props$onMapMessage$o10, _props$onMapMessage10;
      let entered, geofences;
      ({
        entered,
        geofences
      } = onGeofencesChangedValue);
      (_props$onMapMessage$o10 = (_props$onMapMessage10 = props.onMapMessage).onGeofencesChanged) === null || _props$onMapMessage$o10 === void 0 || _props$onMapMessage$o10.call(_props$onMapMessage10, 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: options => {
        (0, _SendCommand.centerMap)(viewId, {
          ...options,
          animationDuration: options.animationDuration ?? 1000
        });
      },
      centerMapOnPoint: options => {
        (0, _SendCommand.centerMapOnPoint)(viewId, {
          ...options,
          animationDuration: options.animationDuration ?? 1000,
          format: options.format ?? 'LAT_LONG'
        });
      },
      setMapViewBounds: options => {
        (0, _SendCommand.setMapViewBounds)(viewId, {
          ...options,
          animationDuration: options.animationDuration ?? 1000,
          rotation: options.rotation ?? 0,
          format: options.format ?? 'LAT_LONG'
        });
      },
      setMapViewToPath: options => {
        (0, _SendCommand.setMapViewToPath)(viewId, {
          ...options,
          margin: options.margin ?? 0.2,
          animationDuration: options.animationDuration ?? 1000,
          rotation: options.rotation ?? 0
        });
      },
      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);
      },
      startPathSimulation: (path, options) => {
        simRef.current.startPathSimulation(path, options);
      },
      controlSimulation: command => {
        return simRef.current.controlSimulation(command);
      },
      getSimulationStatus: () => {
        return simRef.current.getSimulationStatus();
      }
    };
    return listeners;
  });
  (0, _react.useEffect)(() => {
    var _eventEmitter, _eventEmitter2, _eventEmitter3, _eventEmitter4, _eventEmitter5, _eventEmitter6, _eventEmitter7, _eventEmitter8, _eventEmitter9;
    console.info('Registering Event Listeners');
    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('NAVIGATION_PATH_GENERATED', data => setOnNavigationPathGeneratedValue(data));
    (_eventEmitter2 = eventEmitter) === null || _eventEmitter2 === void 0 || _eventEmitter2.addListener('REROUTE_NAVIGATION_PATH_GENERATED', data => setOnRerouteNavigationPathGeneratedValue(data));
    (_eventEmitter3 = eventEmitter) === null || _eventEmitter3 === void 0 || _eventEmitter3.addListener('USER_POSITION_UPDATED', userPosition => setOnUserPositionUpdatedValue(userPosition));
    (_eventEmitter4 = eventEmitter) === null || _eventEmitter4 === void 0 || _eventEmitter4.addListener('GENERATED_SEARCH_DESTINATIONS', destinations => setOnSearchDestinationsGeneratedValue(destinations));
    (_eventEmitter5 = eventEmitter) === null || _eventEmitter5 === void 0 || _eventEmitter5.addListener('MAP_TAPPED_GENERIC', event => setOnMapTappedGenericValue(event));
    (_eventEmitter6 = eventEmitter) === null || _eventEmitter6 === void 0 || _eventEmitter6.addListener('SCREEN_ROTATED', event => {
      const newTransform = {
        rotate: Math.round(event.degrees).toString() + 'deg'
      };
      setCompassTransform(newTransform);
    });
    (_eventEmitter7 = eventEmitter) === null || _eventEmitter7 === void 0 || _eventEmitter7.addListener('ERROR', msg => setOnErrorValue(msg));
    (_eventEmitter8 = eventEmitter) === null || _eventEmitter8 === void 0 || _eventEmitter8.addListener('VENUE_LOADED', venueId => setOnVenueLoadedValue(venueId));
    (_eventEmitter9 = eventEmitter) === null || _eventEmitter9 === void 0 || _eventEmitter9.addListener('GEOFENCES_CHANGED', data => setOnGeofencesChangedValue(data));
    return () => {
      var _eventEmitter10, _eventEmitter11, _eventEmitter12, _eventEmitter13, _eventEmitter14, _eventEmitter15, _eventEmitter16, _eventEmitter17, _eventEmitter18;
      (_eventEmitter10 = eventEmitter) === null || _eventEmitter10 === void 0 || _eventEmitter10.removeAllListeners('NAVIGATION_PATH_GENERATED');
      (_eventEmitter11 = eventEmitter) === null || _eventEmitter11 === void 0 || _eventEmitter11.removeAllListeners('USER_POSITION_UPDATED');
      (_eventEmitter12 = eventEmitter) === null || _eventEmitter12 === void 0 || _eventEmitter12.removeAllListeners('GENERATED_SEARCH_DESTINATIONS');
      (_eventEmitter13 = eventEmitter) === null || _eventEmitter13 === void 0 || _eventEmitter13.removeAllListeners('MAP_TAPPED_GENERIC');
      (_eventEmitter14 = eventEmitter) === null || _eventEmitter14 === void 0 || _eventEmitter14.removeAllListeners('ERROR');
      (_eventEmitter15 = eventEmitter) === null || _eventEmitter15 === void 0 || _eventEmitter15.removeAllListeners('VENUE_LOADED');
      (_eventEmitter16 = eventEmitter) === null || _eventEmitter16 === void 0 || _eventEmitter16.removeAllListeners('SCREEN_ROTATED');
      (_eventEmitter17 = eventEmitter) === null || _eventEmitter17 === void 0 || _eventEmitter17.removeAllListeners('GEOFENCES_CHANGED');
      (_eventEmitter18 = eventEmitter) === null || _eventEmitter18 === void 0 || _eventEmitter18.removeAllListeners('REROUTE_NAVIGATION_PATH_GENERATED');
    };
  }, []);
  return /*#__PURE__*/_react.default.createElement(_reactNative.SafeAreaView, {
    style: {
      width: props.style.width,
      height: props.style.height,
      top: props.style.visible ? 0 : -(2 * _reactNative.Dimensions.get('window').height + props.style.height + 5),
      left: props.style.visible ? 0 : -(2 * _reactNative.Dimensions.get('window').width + props.style.width + 5),
      position: 'absolute',
      overflow: 'hidden'
    }
  }, /*#__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$onMapMessage11 = props.onMapMessage) === null || _props$onMapMessage11 === void 0 ? void 0 : _props$onMapMessage11.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: _reactNative.PixelRatio.getPixelSizeForLayoutSize(props.style.height),
      width: _reactNative.PixelRatio.getPixelSizeForLayoutSize(props.style.width)
    } : {
      height: props.style.height,
      width: props.style.width
    },
    ref: nodeRef
  }) : null);
};
const mapViewFwRef = /*#__PURE__*/(0, _react.forwardRef)(MapView);
var _default = mapViewFwRef;
exports.default = _default;
//# sourceMappingURL=MapView.js.map