"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
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(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 _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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var _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);
});
exports["default"] = _default;
//# sourceMappingURL=index.js.map
