"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"] = Search;
var _react = _interopRequireWildcard(require("react"));
var _Modal = _interopRequireDefault(require("../_component/Modal"));
var _ToolsSelect = _interopRequireDefault(require("../_component/ToolsSelect"));
var _request = _interopRequireDefault(require("../_mapUtil/request"));
var _config = _interopRequireDefault(require("../_mapUtil/config"));
var _utils = require("../_mapUtil/utils");
var _featureDraw = _interopRequireDefault(require("@vtx/ol-map/lib/feature-draw"));
var _service = _interopRequireDefault(require("../_mapUtil/service"));
var _useProps2 = _interopRequireDefault(require("@vtx/ol-map/lib/use-props"));
var _options = require("../_mapUtil/options");
var _index = require("@vtx/ol-map/lib/analysis-layer/index");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _index2 = _interopRequireDefault(require("../_mapUtil/GetData/index"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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 && {}.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(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(r) { if (Array.isArray(r)) return r; } /*
 * @description: 爆管关阀分析
 */
function Search(props) {
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map,
    layerRef = _useProps.layerRef;
  var _props$onClose = props.onClose,
    onClose = _props$onClose === void 0 ? function () {} : _props$onClose,
    _props$show = props.show,
    show = _props$show === void 0 ? false : _props$show,
    _props$setLoading = props.setLoading,
    setLoading = _props$setLoading === void 0 ? function () {} : _props$setLoading,
    _props$setVisible = props.setVisible,
    setVisible = _props$setVisible === void 0 ? function () {} : _props$setVisible,
    _props$setDataSource = props.setDataSource,
    setDataSource = _props$setDataSource === void 0 ? function () {} : _props$setDataSource,
    _props$setType = props.setType,
    setType = _props$setType === void 0 ? function () {} : _props$setType,
    _props$getData = props.getData,
    getData = _props$getData === void 0 ? function () {} : _props$getData,
    _props$setExpandData = props.setExpandData,
    setExpandData = _props$setExpandData === void 0 ? function () {} : _props$setExpandData,
    _props$setYjfxData = props.setYjfxData,
    setYjfxData = _props$setYjfxData === void 0 ? function () {} : _props$setYjfxData;
  var _useState = (0, _react.useState)('Point'),
    _useState2 = _slicedToArray(_useState, 2),
    drawType = _useState2[0],
    setDrawType = _useState2[1];
  var _useState3 = (0, _react.useState)('Point'),
    _useState4 = _slicedToArray(_useState3, 2),
    activeBtn = _useState4[0],
    setActiveBtn = _useState4[1]; // 框选选择
  var _useState5 = (0, _react.useState)(null),
    _useState6 = _slicedToArray(_useState5, 2),
    features = _useState6[0],
    setFeatures = _useState6[1];
  var btn = (0, _react.useRef)();
  var onDrawEnd = function onDrawEnd(feature) {
    setFeatures([feature]);
  };
  (0, _react.useEffect)(function () {
    if (show) {
      setVisible(true);
    } else {
      setVisible(false);
    }
  }, [show]);
  (0, _react.useEffect)(function () {
    btn.current = 'Point';
    map.drawActive = true;
    map.on('loadstart', function () {});
    if (show) {
      map.on('click', mapClick);
      return function () {
        map.un('click', mapClick);
        map.hideTip();
      };
    }
  }, [show, map]);
  (0, _react.useEffect)(function () {
    if (activeBtn) {
      if (activeBtn == 'Point') {
        map.showTip('请选择一个爆管点');
      }
      setExpandData([]);
      setDataSource([]);
      if (layerRef.current) {
        layerRef.current.clearFeatures();
      }
      setDrawType(activeBtn);
    } else {
      map.hideTip();
    }
  }, [activeBtn]);
  var mapClick = function mapClick(event) {
    if (btn.current !== 'Point') return;
    setLoading(true);
    setVisible(true);
    setDataSource([]);
    var layers = map.layers.getAllLayers().filter(function (_layer) {
      return _layer === null || _layer === void 0 ? void 0 : _layer.show;
    });
    if (layers !== null && layers !== void 0 && layers.length) {
      (0, _index.getWMSFeatureInfoByLayers)(event, map, layers).then(function (features) {
        if (features !== null && features !== void 0 && features.length) {
          try {
            map.hideTemplate();
          } catch (err) {}
          setType('gffx');
          setActiveBtn('');
          btn.current = '';
          getData(features[0].values_, features[0].key);
        } else {
          setLoading(false);
        }
      });
    } else {
      setLoading(false);
    }
  };
  var onToolsChange = function onToolsChange(key) {
    setActiveBtn(key);
    btn.current = key;
    map.drawActive = true;
  };
  (0, _react.useEffect)(function () {
    if (features !== null && features !== void 0 && features.length) {
      (0, _index2["default"])({
        feature: features[0],
        map: map,
        drawType: activeBtn,
        warningTitle: '请先勾选供水管线、供水管点图层！',
        highLayerFn: function highLayerFn(features) {
          var _layerRef$current;
          (_layerRef$current = layerRef.current) === null || _layerRef$current === void 0 || _layerRef$current.setHighlight(features);
        }
      }).then(function (res) {
        var _res$data;
        setVisible(true);
        setActiveBtn('');
        btn.current = '';
        map.drawActive = false;
        setType('yjfx');
        if (res !== null && res !== void 0 && (_res$data = res.data) !== null && _res$data !== void 0 && _res$data.length) {
          getYJZXData(res.data, res.features);
        }
      });
    }
  }, [features]);

  /**
   * @description: 获取压降中心数据
   * @param {*} data
   * @return {*}
   */
  var getYJZXData = function getYJZXData(data, features) {
    setYjfxData([]);
    var codes = _service["default"].getField(data, 'code');
    var url = "".concat(_config["default"].publicUrlZNFX, "/api/gisAnalyze/explosionRangeAnalysis");
    var params = {
      lineType: 2,
      // 1 污水 2 供水
      pointCodes: codes
    };
    _request["default"].post(url, {
      body: params
    }).then(function (res) {
      var _res$data2;
      setLoading(false);
      if ((res === null || res === void 0 ? void 0 : res.result) === 0 && res !== null && res !== void 0 && (_res$data2 = res.data) !== null && _res$data2 !== void 0 && _res$data2.length) {
        var _layerRef$current2;
        setYjfxData(res.data);
        var _codes = (0, _utils.getField)(res.data, 'pointCode') || [];
        var _features = features.filter(function (v) {
          var _v$values_;
          return _codes.includes(v === null || v === void 0 || (_v$values_ = v.values_) === null || _v$values_ === void 0 ? void 0 : _v$values_.code);
        });
        (_layerRef$current2 = layerRef.current) === null || _layerRef$current2 === void 0 || _layerRef$current2.setHighlight(_features);
      }
    })["catch"](function () {
      setLoading(false);
    });
  };
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_Modal["default"], {
    title: "\u7206\u7BA1\u5206\u6790",
    onClose: onClose,
    width: 500,
    visible: show
  }, /*#__PURE__*/_react["default"].createElement(_ToolsSelect["default"], {
    title: "",
    value: activeBtn,
    onChange: onToolsChange,
    options: _options.toolsSelectOptions
  }), drawType !== 'Point' && /*#__PURE__*/_react["default"].createElement(_featureDraw["default"], {
    drawType: drawType,
    onDrawEnd: onDrawEnd
  })));
}
Search.propTypes = {
  show: _propTypes["default"].bool,
  onClose: _propTypes["default"].func,
  setLoading: _propTypes["default"].func,
  setVisible: _propTypes["default"].func,
  setDataSource: _propTypes["default"].func,
  setType: _propTypes["default"].func,
  getData: _propTypes["default"].func,
  moveKey: _propTypes["default"].any,
  setExpandData: _propTypes["default"].func,
  setYjfxData: _propTypes["default"].func
};
//# sourceMappingURL=Search.js.map
