"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _index = _interopRequireDefault(require("../map/index"));
var _Search = _interopRequireDefault(require("./Search"));
var _PoiList = _interopRequireDefault(require("./PoiList"));
var _FeatureLayer = _interopRequireDefault(require("../feature-layer/FeatureLayer"));
var _location2 = _interopRequireDefault(require("../assets/location.png"));
var _search = require("../_util/search");
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 _getRequireWildcardCache(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 && Object.prototype.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; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var _default = exports["default"] = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _props$height = props.height,
    height = _props$height === void 0 ? '100%' : _props$height,
    _props$width = props.width,
    width = _props$width === void 0 ? '100%' : _props$width,
    _props$tk = props.tk,
    tk = _props$tk === void 0 ? '' : _props$tk,
    defaultValue = props.defaultValue,
    isChina = props.isChina;
  var _useState = (0, _react.useState)({}),
    _useState2 = _slicedToArray(_useState, 2),
    poi = _useState2[0],
    setPoi = _useState2[1];
  var mapRef = (0, _react.useRef)();
  var time = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      map: mapRef.current,
      getData: getData
    };
  });
  var getData = function getData() {
    var map = mapRef.current;
    var selectLayer = map.getLayerByKey('SELECT_LOCATION');
    var source = selectLayer.getSource();
    if (source) {
      var features = source.getFeatures();
      if (features !== null && features !== void 0 && features.length) {
        return features[0].values_;
      }
    }
  };
  var createdMap = function createdMap(map) {
    var layer = new _FeatureLayer["default"]({
      key: 'LOCATION',
      style: {
        mode: 'Simple',
        render: {
          type: 'Marker',
          style: {
            image: _location2["default"],
            scale: 1
          }
        }
      },
      labelField: 'name'
    });
    map.addLayer(layer);
    var selectLayer = new _FeatureLayer["default"]({
      key: 'SELECT_LOCATION',
      zIndex: 9,
      style: {
        mode: 'Simple',
        render: {
          type: 'Marker',
          style: {
            image: _location2["default"],
            scale: 1.3
          }
        }
      },
      labelField: 'name',
      template: function template(feature) {
        var _feature$values_ = feature.values_,
          address = _feature$values_.address,
          business = _feature$values_.business;
        return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", null, "\u5730\u5740\uFF1A", address), (business === null || business === void 0 ? void 0 : business.tel) && /*#__PURE__*/_react["default"].createElement("div", null, "\u7535\u8BDD\uFF1A", business.tel));
      }
    });
    map.addLayer(selectLayer);
    if (defaultValue !== null && defaultValue !== void 0 && defaultValue.geometryInfo) {
      selectLayer.loadData(defaultValue);
      map.fitView(selectLayer, {
        padding: [0, 0, 0, 0]
      });
    }
    mapRef.current = map;
  };
  var onMapClick = function onMapClick(event) {
    var map = mapRef.current;
    var selectLayer = map.getLayerByKey('SELECT_LOCATION');
    var source = selectLayer.getSource();
    source.clear();
    var feature = map.getFeaturesAtPixel(event.pixel)[0];
    if (feature) {
      var newFeature = feature.clone();
      selectLayer.getSource().addFeature(newFeature);
      map.showTemplate(newFeature);
    } else {
      if (time.current) {
        clearTimeout(time.current);
      }
      var _location = event.coordinate;
      selectLayer.loadData([{
        geometryInfo: {
          type: 'point',
          lngLats: _location.join(','),
          coordType: 'wgs84'
        }
      }]);
      map.getView().animate({
        center: _location,
        duration: 100
      });
      time.current = setTimeout(function () {
        (0, _search.regeoSearch)({
          location: _location,
          extensions: 'all'
        }).then(function (res) {
          if (res !== null && res !== void 0 && res.address) {
            selectLayer.loadData(res);
            var _feature = selectLayer.getFeatureByFieldValue('address', res.address);
            map.showTemplate(_feature);
          }
        });
      }, 500);
    }
  };
  var onListClick = function onListClick(item) {
    if (item.geometryInfo) {
      var map = mapRef.current;
      var selectLayer = map.getLayerByKey('SELECT_LOCATION');
      selectLayer.loadData(item);
      var feature = selectLayer.getFeatureByFieldValue('address', item.address);
      map.showTemplate(feature);
      map.getView().animate({
        center: item.geometryInfo.lngLats.split(',').map(function (v) {
          return Number(v);
        }),
        duration: 100
      });
    }
  };
  return /*#__PURE__*/_react["default"].createElement(_index["default"], {
    createdMap: createdMap,
    width: width,
    height: height,
    tk: tk,
    onClick: onMapClick
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "map-search-poi-search"
  }, /*#__PURE__*/_react["default"].createElement(_Search["default"], {
    isChina: isChina,
    onConfirm: setPoi
  })), (poi === null || poi === void 0 ? void 0 : poi.address) && /*#__PURE__*/_react["default"].createElement(_PoiList["default"], {
    poi: poi,
    onClick: onListClick
  }), props.children);
});
//# sourceMappingURL=index.js.map
