"use strict";

exports.__esModule = true;
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactDom = require("react-dom");

var _MapUtil = _interopRequireDefault(require("./../MapUtil"));

var _Page = _interopRequireDefault(require("./../Page"));

var _Header = _interopRequireDefault(require("./../Header"));

var _Container = _interopRequireDefault(require("./../Container"));

var _Notice = _interopRequireDefault(require("./../Notice"));

var _Bridge = _interopRequireDefault(require("./../Bridge"));

var _Wrapper = _interopRequireDefault(require("./Wrapper"));

var _Close = _interopRequireDefault(require("./Close"));

var _MapInstance = _interopRequireDefault(require("./MapInstance"));

var _instance = _interopRequireDefault(require("./../Context/instance.js"));

var _excluded = ["ak", "show", "portal", "caption", "prevCaption", "onHide", "center", "points", "circle", "polygon", "district", "header", "children"];

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 _getRequireWildcardCache(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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function MapView(_ref) {
  var ak = _ref.ak,
      _ref$show = _ref.show,
      show = _ref$show === void 0 ? true : _ref$show,
      portal = _ref.portal,
      caption = _ref.caption,
      prevCaption = _ref.prevCaption,
      onHide = _ref.onHide,
      _ref$center = _ref.center,
      center = _ref$center === void 0 ? '江苏省,南京市' : _ref$center,
      points = _ref.points,
      circle = _ref.circle,
      polygon = _ref.polygon,
      district = _ref.district,
      header = _ref.header,
      children = _ref.children,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var _useState = (0, _react.useState)(null),
      mapInstance = _useState[0],
      setMapInstance = _useState[1]; // context


  var context = (0, _react.useContext)(_instance["default"]) || {};

  var locale = context.locale || function (remark) {
    return remark || '';
  };

  var refWrapperEl = (0, _react.useRef)(null);

  var _useState2 = (0, _react.useState)(''),
      errMsg = _useState2[0],
      setErrMsg = _useState2[1];

  (0, _react.useEffect)(function () {
    if (!window.BMap && !ak) {
      setErrMsg(locale('请传入百度地图ak', 'hint_map_ak'));
      return;
    }

    _MapUtil["default"].load({
      ak: ak,
      success: function success() {
        initData();
      },
      fail: function fail() {
        setErrMsg(locale('地图库加载失败, 请稍后再试', 'hint_map_failed_load'));
      }
    }); // 移除组件时注销


    return function () {
      // 设置上一页标题
      handleTitle(prevCaption); // 移除实例化对象

      mapInstance.destroy();
    };
  }, []); // eslint-disable-line
  // 隐藏时, 移除标注
  // useEffect(() => {
  //   if (show === false) {
  //     mapInstance.destroy();
  //   }
  // }, [show])
  // 初始化地图

  function initData() {
    if (!window.BMap) {
      // 如果有高德地图, 则加上 || !window.AMap
      setErrMsg(locale('地图库加载失败, 请稍后再试', 'hint_map_failed_load'));
      return;
    }

    console.log('初始化地图' + center);

    if (!refWrapperEl.current) {
      setErrMsg(locale('地图容器不存在', 'hint_map_no_container'));
      return;
    }

    mapInstance = new _MapInstance["default"]();
    setMapInstance(mapInstance);
    mapInstance.initMap(refWrapperEl.current, center, function (result) {
      if (typeof result === 'string') {
        setErrMsg(result);
        return;
      }
    });
  } // 绘制标记点


  (0, _react.useEffect)(function () {
    if (points && points.length && show && mapInstance) {
      console.log('绘制标记');
      console.log(mapInstance);
      mapInstance.drawMarkers(points);
    }
  }, [points]); // eslint-disable-line
  // 绘制圆形

  (0, _react.useEffect)(function () {
    if (circle && circle.point && show) {
      console.log('绘制圆形');
      mapInstance.drawCircle(circle.point, circle.radius);
    }
  }, [circle]); // eslint-disable-line
  // 绘制多边形

  (0, _react.useEffect)(function () {
    if (polygon && show) {
      console.log('绘制多边形');
      mapInstance.drawPolygon(polygon);
    }
  }, [polygon]); // eslint-disable-line
  // 绘制地区

  (0, _react.useEffect)(function () {
    if (district && show) {
      console.log('绘制区域');
      var districtName = [];

      for (var name in district) {
        districtName.push(district[name].name);
      }

      mapInstance.drawDistrict(districtName.join(','));
    }
  }, [district]); // eslint-disable-line
  // 显隐

  (0, _react.useEffect)(function () {
    if (show) {
      handleTitle(caption);
    } else {
      handleTitle(prevCaption);
    }
  }, [show]); // 中断绘制

  (0, _react.useEffect)(function () {
    if (show) {
      if (errMsg) {
        mapInstance.abort = true;
      } else {
        mapInstance.abort = false;
      }
    }
  }, [errMsg]); // 设置标题

  function handleTitle(title) {
    if (!title) return;

    if (typeof title === 'string') {
      _Bridge["default"].setTitle({
        title: title
      });
    } else if (typeof title === 'function') {
      title();
    }
  }

  var DOM = /*#__PURE__*/_react["default"].createElement(_Page["default"], _extends({}, others, {
    className: (show ? '' : 'hide') + ((others === null || others === void 0 ? void 0 : others.className) ? ' ' + others.className : '')
  }), header && /*#__PURE__*/_react["default"].createElement(_Header["default"], null, header), /*#__PURE__*/_react["default"].createElement(_Container["default"], null, /*#__PURE__*/_react["default"].createElement(_Wrapper["default"], {
    ref: refWrapperEl
  }), onHide && /*#__PURE__*/_react["default"].createElement(_Close["default"], {
    onClick: onHide
  }), children), errMsg && /*#__PURE__*/_react["default"].createElement(_Notice["default"], {
    caption: errMsg
  }));

  if (portal) {
    return (0, _reactDom.createPortal)(DOM, portal);
  }

  return DOM;
}

var _default = MapView;
exports["default"] = _default;
module.exports = exports.default;