import { StyleSheet, View } from 'react-native';
import React, { useContext, useState } from 'react';
import { BottomSheet, Button, Text } from '@rneui/themed';
import { AMapGeoRequest, AMapWeatherRequest, MapContext } from '../../mapa';
import Detail from './Detail';
import MSlot from '../slots/MSlot';
/**
 * Weather props
 *
 * @category Props
 */

/**
  @category Component
 */
const Weather = props => {
  const {
    style
  } = props;
  const {
    map
  } = useContext(MapContext);
  const {
    akey,
    request = new AMapWeatherRequest({
      key: akey
    }),
    detailEle: DettailEle = Detail
  } = props;
  const geoCodeRequest = new AMapGeoRequest({
    key: akey
  });
  const [realWeather, setRealWeather] = useState([]);
  const [forecastWeather, setForecastWeather] = useState([]);
  const [detailVisible, setDetailVisible] = useState(false);
  const slotRef = React.useRef(null);
  const onRequest = async ({
    type = 0
  }) => {
    const center = await map.getCenter();
    const geoCodeData = await geoCodeRequest.decodeAddress({
      key: akey,
      location: center
    });
    const result = await request.weather({
      key: akey,
      extensions: type ? 'all' : 'base',
      city: geoCodeData.regeocode.addressComponent.adcode
    });
    if (type) {
      setForecastWeather(result.forecasts || []);
    } else {
      console.log('weather', result.lives);
      setRealWeather(result.lives || []);
    }
    slotRef.current?.refresh();
  };
  map.on('loaded', () => {
    // 查询实时天气
    onRequest({
      type: 0
    });
    // 查询天气预报
    onRequest({
      type: 1
    });
  });
  const onOpen = async () => {
    if (!forecastWeather.length) {
      onRequest({
        type: 1
      });
    }
    setDetailVisible(true);
  };
  const onClose = async () => {
    setDetailVisible(false);
  };
  return /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(MSlot, {
    ref: slotRef,
    style: style
  }, /*#__PURE__*/React.createElement(Button, {
    type: "outline",
    style: styles.button,
    size: "md",
    onPress: onOpen
  }, /*#__PURE__*/React.createElement(Text, null, (realWeather[0]?.temperature || '?') + '℃'))), /*#__PURE__*/React.createElement(BottomSheet, {
    modalProps: {},
    isVisible: detailVisible
  }, /*#__PURE__*/React.createElement(DettailEle, {
    data: {
      lives: realWeather,
      forecasts: forecastWeather
    },
    onClose: onClose
  })));
};
export default Weather;
const styles = StyleSheet.create({
  container: {
    position: 'absolute',
    right: 5,
    bottom: 20
  },
  header: {
    backgroundColor: '#fff'
  },
  button: {
    backgroundColor: '#fff'
  }
});
//# sourceMappingURL=index.js.map