'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _MapUtil = require('./../MapUtil');

var _MapUtil2 = _interopRequireDefault(_MapUtil);

var _Page = require('./../Page');

var _Page2 = _interopRequireDefault(_Page);

var _Header = require('./../Header');

var _Header2 = _interopRequireDefault(_Header);

var _Container = require('./../Container');

var _Container2 = _interopRequireDefault(_Container);

var _Notice = require('./../Notice');

var _Notice2 = _interopRequireDefault(_Notice);

var _Bridge = require('./../Bridge');

var _Bridge2 = _interopRequireDefault(_Bridge);

var _Wrapper = require('./Wrapper');

var _Wrapper2 = _interopRequireDefault(_Wrapper);

var _Close = require('./Close');

var _Close2 = _interopRequireDefault(_Close);

var _helper = require('./helper');

var _helper2 = _interopRequireDefault(_helper);

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

var _instance2 = _interopRequireDefault(_instance);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

  // context
  var context = (0, _react.useContext)(_instance2.default) || {};
  var locale = context.locale || function (key) {
    return key || '';
  };

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

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

  (0, _react.useEffect)(function () {
    if (!window.BMap && !ak) {
      setErrMsg(locale('请传入百度地图ak', 'hint_map_ak'));
      return;
    }
    _MapUtil2.default.load({
      ak: ak,
      success: function success() {
        initData();
      },
      fail: function fail() {
        setErrMsg(locale('地图库加载失败, 请稍后再试', 'hint_map_failed_load'));
      }
    });
    // 移除组件时注销
    return function () {
      _helper2.default.destroy();
    };
  }, []); // eslint-disable-line

  // 隐藏时, 移除标注
  // useEffect(() => {
  //   if (show === false) {
  //     helper.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;
    }

    _helper2.default.initMap(refWrapperEl.current, center, function (result) {
      if (typeof result === 'string') {
        setErrMsg(result);
        return;
      }
    });
  }

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

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

  // 绘制多边形
  (0, _react.useEffect)(function () {
    if (polygon && show) {
      console.log('绘制多边形');
      _helper2.default.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);
      }
      _helper2.default.drawDistrict(districtName.join(','));
    }
  }, [district]); // eslint-disable-line

  // 标题
  (0, _react.useEffect)(function () {
    if (caption && show) {
      _Bridge2.default.setTitle(caption);
    }
  }, [caption]);

  // 中断绘制
  (0, _react.useEffect)(function () {
    if (show) {
      if (errMsg) {
        _helper2.default.abort = true;
      } else {
        _helper2.default.abort = false;
      }
    }
  }, [errMsg]);

  var DOM = _react2.default.createElement(
    _Page2.default,
    { className: show ? '' : 'hide' },
    header && _react2.default.createElement(
      _Header2.default,
      null,
      header
    ),
    _react2.default.createElement(
      _Container2.default,
      null,
      _react2.default.createElement(_Wrapper2.default, { ref: refWrapperEl }),
      onHide && _react2.default.createElement(_Close2.default, { onClick: onHide }),
      children
    ),
    errMsg && _react2.default.createElement(_Notice2.default, { caption: errMsg })
  );
  if (portal) {
    return (0, _reactDom.createPortal)(DOM, portal);
  }
  return DOM;
}
exports.default = MapView;
module.exports = exports['default'];