"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _react = _interopRequireWildcard(require("react"));
var _themed = require("@rneui/themed");
var _mapa = require("../../mapa");
var _Detail = _interopRequireDefault(require("./Detail"));
var _MSlot = _interopRequireDefault(require("../slots/MSlot"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
 * Weather props
 *
 * @category Props
 */

/**
  @category Component
 */
const Weather = props => {
  const {
    style
  } = props;
  const {
    map
  } = (0, _react.useContext)(_mapa.MapContext);
  const {
    akey,
    request = new _mapa.AMapWeatherRequest({
      key: akey
    }),
    detailEle: DettailEle = _Detail.default
  } = props;
  const geoCodeRequest = new _mapa.AMapGeoRequest({
    key: akey
  });
  const [realWeather, setRealWeather] = (0, _react.useState)([]);
  const [forecastWeather, setForecastWeather] = (0, _react.useState)([]);
  const [detailVisible, setDetailVisible] = (0, _react.useState)(false);
  const slotRef = _react.default.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.default.createElement(_reactNative.View, null, /*#__PURE__*/_react.default.createElement(_MSlot.default, {
    ref: slotRef,
    style: style
  }, /*#__PURE__*/_react.default.createElement(_themed.Button, {
    type: "outline",
    style: styles.button,
    size: "md",
    onPress: onOpen
  }, /*#__PURE__*/_react.default.createElement(_themed.Text, null, (realWeather[0]?.temperature || '?') + '℃'))), /*#__PURE__*/_react.default.createElement(_themed.BottomSheet, {
    modalProps: {},
    isVisible: detailVisible
  }, /*#__PURE__*/_react.default.createElement(DettailEle, {
    data: {
      lives: realWeather,
      forecasts: forecastWeather
    },
    onClose: onClose
  })));
};
var _default = exports.default = Weather;
const styles = _reactNative.StyleSheet.create({
  container: {
    position: 'absolute',
    right: 5,
    bottom: 20
  },
  header: {
    backgroundColor: '#fff'
  },
  button: {
    backgroundColor: '#fff'
  }
});
//# sourceMappingURL=index.js.map