import { Button, Icon } from '@rneui/themed';
import Mapbox from '@rnmapbox/maps';
import { StyleSheet, View } from 'react-native';
import { buttonSize } from '../config';
import { useContext } from 'react';
import { MapContext } from '../modules/MapContext';
import React from 'react';
import MSlot from './slots/MSlot';

/**
 * Location props
 * @category Props
 */

/**
 *用户定位
 * @example
 * ```
 * <Mapa.Location locateWhenInit={true} visible={true} style="right-bottom" />
 * ```
 *
 * @category Component
 */
const Location = props => {
  const {
    visible = true,
    locateWhenInit = false,
    onChange,
    style = 'right'
  } = props;
  const {
    map
  } = useContext(MapContext);
  // const containerStyle: StyleProp<ViewStyle> = props.style
  //     ? { ...(props.style as any), position: 'absolute' }
  //     : styles.container;

  const locate = async () => {
    const location = await map?.locationManager.getLastKnownLocation();
    if (location) {
      const center = [location.coords.longitude, location.coords.latitude];
      map.flyTo(center);
    }
  };
  if (visible) {
    // 启动位置监听
    map.locationManager.start();
    if (locateWhenInit) {
      map.on('loaded', async () => {
        await locate();
      });
    }
    if (onChange) {
      map.locationManager.on('locationChange', onChange);
    }
  }
  const onPress = async () => {
    await locate();
  };
  return /*#__PURE__*/React.createElement(MSlot, {
    style: style
  }, /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(Button, {
    type: "outline",
    style: styles.button,
    size: "md",
    onPress: onPress
  }, /*#__PURE__*/React.createElement(Icon, {
    name: "locate",
    type: "ionicon",
    size: buttonSize
  }))), /*#__PURE__*/React.createElement(Mapbox.LocationPuck, {
    visible: visible,
    puckBearing: 'heading',
    puckBearingEnabled: true
  }));
};
export default Location;
const styles = StyleSheet.create({
  container: {
    position: 'absolute',
    right: 5,
    bottom: 5
  },
  button: {
    backgroundColor: '#fff'
  }
});
//# sourceMappingURL=Location.js.map