"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 _Select = _interopRequireDefault(require("../_component/Select"));
var _ToolsSelect = _interopRequireDefault(require("../_component/ToolsSelect"));
var _request = _interopRequireDefault(require("../_mapUtil/request"));
var _config = _interopRequireDefault(require("../_mapUtil/config"));
var _useProps2 = _interopRequireDefault(require("@vtx/ol-map/lib/use-props"));
var _index = require("@vtx/ol-map/lib/analysis-layer/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 _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
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,
    analysisLayer = _useProps.analysisLayer,
    code = _useProps.code,
    traceDirectionOption = _useProps.traceDirectionOption,
    onClose = _useProps.onClose,
    setTraceDirectionOption = _useProps.setTraceDirectionOption,
    setLoading = _useProps.setLoading,
    setTableData = _useProps.setTableData,
    setVisible = _useProps.setVisible,
    setCode = _useProps.setCode,
    options = _useProps.options,
    highLayer = _useProps.highLayer;
  var btn = (0, _react.useRef)();
  var _useState = (0, _react.useState)(''),
    _useState2 = _slicedToArray(_useState, 2),
    btnKey = _useState2[0],
    setBtnKey = _useState2[1];
  (0, _react.useEffect)(function () {
    if (code) getData();
  }, [traceDirectionOption, code]);
  (0, _react.useEffect)(function () {
    // 地图点击事件
    map.on('click', mapClick);
    return function () {
      highLayer.current && highLayer.current.destroy();
      // 地图点击事件
      map.un('click', mapClick);
    };
  }, [map]);
  var mapClick = function mapClick(event) {
    if (!btn.current) return;
    setLoading(true);
    analysisLayer.current.filter = '';
    var layers = map.getVisibleLayersByKeys(map.layers.getShowKeys());
    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) {
          setVisible(true);
          getCode(features[0]);
          highLayer.current.addHighlightFeature([features[0]]);
        } else {
          setLoading(false);
        }
      });
    } else {
      setLoading(false);
    }
  };
  var getCode = function getCode(feature) {
    var _feature$getGeometry;
    var type = feature === null || feature === void 0 || (_feature$getGeometry = feature.getGeometry()) === null || _feature$getGeometry === void 0 ? void 0 : _feature$getGeometry.getType();
    var code = '';
    // code:管线起点或终点
    if (['MultiLineString', 'LineString'].includes(type)) {
      var _feature$get;
      code = (_feature$get = feature.get('code')) === null || _feature$get === void 0 || (_feature$get = _feature$get.split('_')) === null || _feature$get === void 0 ? void 0 : _feature$get[0];
    } else if (['Point'].includes(type)) {
      code = feature.get('code');
    }
    setCode(code);
  };
  var getData = function getData() {
    _request["default"].get("".concat(_config["default"].publicUrlZNFX, "/api/gisAnalyze/getPipeLineTrack"), {
      body: {
        type: traceDirectionOption.map(function (v) {
          return v.value;
        }).join(','),
        lineCode: code
      }
    }).then(function (res) {
      setLoading(false);
      var data = [];
      if ((res === null || res === void 0 ? void 0 : res.result) === 0 && res !== null && res !== void 0 && res.data) {
        var _res$data, _res$data2;
        // 下游管线
        var downstreamLine = ((_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.downstreamLine) || [];
        downstreamLine.map(function (v) {
          if (v.waterType === 'WS') {
            v.key = 'WSGW';
          } else if (v.waterType === 'YS') {
            v.key = 'YSGW';
          } else {
            v.key = 'YWHL';
          }
        });
        // 上游管线
        var upstreamLine = ((_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.upstreamLine) || [];
        upstreamLine.map(function (v) {
          if (v.waterType === 'WS') {
            v.key = 'WSGW';
          } else if (v.waterType === 'YS') {
            v.key = 'YSGW';
          } else {
            v.key = 'YWHL';
          }
        });
        data = [].concat(_toConsumableArray(downstreamLine), _toConsumableArray(upstreamLine));
        analysisLayer.current.loadData(_toConsumableArray(data));
        setTableData({
          downstreamLine: downstreamLine,
          upstreamLine: upstreamLine
        });
      } else {
        setTableData({
          downstreamLine: [],
          upstreamLine: []
        });
      }
    });
  };
  var onCloseFn = function onCloseFn() {
    setTableData({
      downstreamLine: [],
      upstreamLine: []
    });
    highLayer.current && highLayer.current.clearFeatures();
    analysisLayer.current.filter = '';
    onClose && onClose();
  };
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_Modal["default"], {
    title: "\u8FFD\u8E2A\u5206\u6790",
    onClose: onCloseFn,
    width: 300
  }, /*#__PURE__*/_react["default"].createElement(_Select["default"], {
    mode: "multiple",
    defaultValue: [1, 2],
    options: options,
    title: "\u8FFD\u8E2A\u65B9\u5411",
    onChange: function onChange(e, option) {
      setTraceDirectionOption(option);
    },
    width: 200,
    layout: "horizontal"
  }), /*#__PURE__*/_react["default"].createElement(_ToolsSelect["default"], {
    layout: "horizontal",
    title: "\u5730\u56FE\u9009\u62E9",
    value: btnKey,
    onChange: function onChange(e) {
      if (e === btnKey) {
        setBtnKey('');
        btn.current = '';
        map.drawActive = false;
      } else {
        // 地图提示
        map.showTip('请单击选择管线或管点');
        setBtnKey(e);
        btn.current = e;
        map.drawActive = true;
      }
    },
    options: [{
      label: '点选',
      key: 'select',
      isActive: false,
      icon: 'icon-dianxuan'
    }]
  })));
}
//# sourceMappingURL=Search.js.map
