"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.CloseBtn = CloseBtn;
exports["default"] = _default;
var _react = _interopRequireWildcard(require("react"));
var _antd = require("antd");
var _Select = _interopRequireDefault(require("./Select"));
var _icons = require("@ant-design/icons");
var _toolsItem = _interopRequireDefault(require("../tools-item"));
var _useProps2 = _interopRequireDefault(require("../../_util/useProps"));
var _HighlightLayer = _interopRequireDefault(require("../../highlight-layer/HighlightLayer"));
var _classnames = _interopRequireDefault(require("classnames"));
var _excluded = ["onSelect"],
  _excluded2 = ["options", "placeholder", "onClear"];
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(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; }
function _objectDestructuringEmpty(obj) { if (obj == null) throw new TypeError("Cannot destructure " + obj); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
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; }
var _Cesium = Cesium,
  Cartesian3 = _Cesium.Cartesian3,
  Math = _Cesium.Math;
;
/**
 * @description: 关闭按钮
 * @return {JSX.Element}
 */
function CloseBtn(_ref) {
  var style = _ref.style,
    _onClick = _ref.onClick;
  return /*#__PURE__*/_react["default"].createElement(_icons.CloseOutlined, {
    className: (0, _classnames["default"])('cs-close-btn'),
    onClick: function onClick() {
      _onClick && _onClick();
    },
    style: style
  });
}
var MySelect = function MySelect(props) {
  var onSelect = props.onSelect,
    otherProps = _objectWithoutProperties(props, _excluded);
  return /*#__PURE__*/_react["default"].createElement(_Select["default"], _extends({
    options: [{
      value: 'address',
      label: 'POI'
    }, {
      value: 'layer',
      label: '图层名称'
    }],
    style: {
      width: '120px'
    },
    size: "big",
    onSelect: onSelect
  }, otherProps));
};
var Content = function Content(props) {
  var options = props.options,
    placeholder = props.placeholder,
    onClear = props.onClear,
    otherProps = _objectWithoutProperties(props, _excluded2);
  (0, _react.useEffect)(function () {
    return function () {
      onClear && onClear();
    };
  }, []);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-autoComplete')
  }, /*#__PURE__*/_react["default"].createElement(_antd.AutoComplete, _extends({
    placeholder: placeholder || '请输入',
    options: options,
    style: {
      width: 160
    },
    allowClear: false,
    dropdownClassName: (0, _classnames["default"])('cs-search-wrap'),
    autoFocus: true
  }, otherProps)), /*#__PURE__*/_react["default"].createElement(CloseBtn, {
    style: {
      marginLeft: '10px'
    },
    onClick: onClear
  }));
};
function _default(props) {
  _objectDestructuringEmpty(props);
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map;
  var placeholder = props.placeholder,
    _props$defaultType = props.defaultType,
    defaultType = _props$defaultType === void 0 ? 'layer' : _props$defaultType;
  var time = (0, _react.useRef)(null);
  var _useState = (0, _react.useState)([]),
    _useState2 = _slicedToArray(_useState, 2),
    options = _useState2[0],
    setOptions = _useState2[1];
  var _useState3 = (0, _react.useState)(''),
    _useState4 = _slicedToArray(_useState3, 2),
    value = _useState4[0],
    setValue = _useState4[1];
  var _useState5 = (0, _react.useState)(defaultType),
    _useState6 = _slicedToArray(_useState5, 2),
    type = _useState6[0],
    setType = _useState6[1];
  var itemRef = (0, _react.useRef)();
  var highlightLayer = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    highlightLayer.current = new _HighlightLayer["default"]({
      map: map
    });
    return function () {
      onClear();
    };
  }, []);

  // 地址查询
  var onSearch = function onSearch(searchText) {
    if (time.current) {
      clearTimeout(time.current);
    }
    if (searchText && map) {
      time.current = setTimeout(function () {
        map.getMapFeatureInfoByText && map.getMapFeatureInfoByText({
          type: type,
          searchText: searchText
        }).then(function (res) {
          if (res !== null && res !== void 0 && res.length) {
            setOptions(res);
          }
        });
      }, 500);
    } else {
      setOptions([]);
    }
  };
  var onChange = function onChange(_value) {
    setValue(_value);
  };
  var onSelect = function onSelect(_value, e) {
    if (!map) return;
    if (type === 'address') {
      if (e.lngLat) {
        var _e$lngLat = _slicedToArray(e.lngLat, 2),
          longitude = _e$lngLat[0],
          latitude = _e$lngLat[1];
        map.camera.flyTo({
          destination: Cartesian3.fromDegrees(longitude, latitude, 200000.0),
          orientation: {
            pitch: Math.toRadians(-88.0),
            roll: 0.0
          },
          duration: 2
        });
      }
    } else if (type === 'layer') {
      if (e.feature) {
        map.showTemplate(e.feature);
      }
    }
    setValue(e.label);
  };
  var onClear = function onClear() {
    var _itemRef$current;
    ((_itemRef$current = itemRef.current) === null || _itemRef$current === void 0 ? void 0 : _itemRef$current.onVisibleChange) && itemRef.current.onVisibleChange(false);
    setValue('');
    setOptions([]);
  };
  var content = function content() {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])('cs-content')
    }, /*#__PURE__*/_react["default"].createElement(MySelect, {
      defaultValue: defaultType,
      onSelect: function onSelect(e) {
        setType(e);
        setValue('');
        setOptions([]);
      }
    }), /*#__PURE__*/_react["default"].createElement(Content, {
      placeholder: placeholder,
      value: value,
      options: options,
      onSearch: onSearch,
      onChange: onChange,
      onSelect: onSelect,
      onClear: onClear
    }));
  };
  return /*#__PURE__*/_react["default"].createElement(_toolsItem["default"], {
    content: content,
    ref: itemRef,
    destroyTooltipOnHide: true,
    hideCloseBtn: true
  }, /*#__PURE__*/_react["default"].createElement(_icons.SearchOutlined, null));
}
//# sourceMappingURL=index.js.map
