"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 _button = _interopRequireDefault(require("antd/lib/button"));
var _message = _interopRequireDefault(require("antd/lib/message"));
var _Modal = _interopRequireDefault(require("../_component/Modal"));
var _FormItem = require("../_component/FormItem");
var _request = _interopRequireDefault(require("../_mapUtil/request"));
var _config = _interopRequireDefault(require("../_mapUtil/config"));
var _MyIcon = _interopRequireDefault(require("../_component/MyIcon"));
var _RequiredLabel = _interopRequireDefault(require("../_component/RequiredLabel"));
var _style2 = require("../_mapUtil/style");
var _index = require("@vtx/ol-map/lib/analysis-layer/index");
var _ConnectivityAnalysis = _interopRequireDefault(require("./ConnectivityAnalysis"));
var _featureDraw = _interopRequireDefault(require("@vtx/ol-map/lib/feature-draw"));
var _useProps2 = _interopRequireDefault(require("@vtx/ol-map/lib/use-props"));
var _style3 = require("ol/style");
var _PublicService = _interopRequireDefault(require("../_mapUtil/PublicService"));
var _service = _interopRequireDefault(require("../_mapUtil/service"));
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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; } /**
 * @returns 连通性分析（供水、污水）
 */
var tenantId = _PublicService["default"].getQueryString('tenantId') || _PublicService["default"].getSystemInfo('tenantId');
function Search(props) {
  var _startPointFeature$va3, _startPointFeature$va4, _endPointFeature$valu3, _endPointFeature$valu4;
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map,
    highLayers = _useProps.highLayers,
    paramsName = _useProps.paramsName,
    _useProps$setTableDat = _useProps.setTableData,
    setTableData = _useProps$setTableDat === void 0 ? function () {} : _useProps$setTableDat,
    _useProps$setVisible = _useProps.setVisible,
    setVisible = _useProps$setVisible === void 0 ? function () {} : _useProps$setVisible,
    _useProps$onClose = _useProps.onClose,
    onClose = _useProps$onClose === void 0 ? function () {} : _useProps$onClose,
    options = _useProps.options,
    _useProps$setActiveKe = _useProps.setActiveKey,
    setActiveKey = _useProps$setActiveKe === void 0 ? function () {} : _useProps$setActiveKe;
  var endPointFeatureRef = (0, _react.useRef)();
  var _useState = (0, _react.useState)('Point'),
    _useState2 = _slicedToArray(_useState, 2),
    drawTypeStart = _useState2[0],
    setDrawTypeStart = _useState2[1];
  var _useState3 = (0, _react.useState)(''),
    _useState4 = _slicedToArray(_useState3, 2),
    drawTypeEnd = _useState4[0],
    setDrawTypeEnd = _useState4[1];
  var _useState5 = (0, _react.useState)(null),
    _useState6 = _slicedToArray(_useState5, 2),
    startPointFeature = _useState6[0],
    setStartPointFeature = _useState6[1];
  var _useState7 = (0, _react.useState)(null),
    _useState8 = _slicedToArray(_useState7, 2),
    endPointFeature = _useState8[0],
    setEndPointFeature = _useState8[1];

  // 样式
  var _style = function style(feature) {
    return [(0, _style2.commonStyle)(), (0, _style2.flowDirectionStyle)(feature)];
  };
  // 起点
  var onDrawStart = function onDrawStart(feature) {
    _service["default"].getFeature(feature, map, '管点').then(function (feature) {
      // 有起点 并且没有终点
      if (feature && !endPointFeatureRef.current) {
        getSelect('endPoint');
      }
      setStartPointFeature(feature);
    });
  };
  // 终点
  var onDrawEnd = function onDrawEnd(feature) {
    _service["default"].getFeature(feature, map, '管点').then(function (feature) {
      setEndPointFeature(feature);
      endPointFeatureRef.current = feature;
    });
  };

  // 选择起终点
  var getSelect = function getSelect(type) {
    if (type === 'startPoint') {
      setDrawTypeStart('Point');
      setDrawTypeEnd(null);
      setStartPointFeature(null);
    } else {
      setDrawTypeEnd('Point');
      setEndPointFeature(null);
      setDrawTypeStart(null);
    }
    map.showTip("\u8BF7\u9009\u62E9\u4E00\u4E2A".concat(type === 'startPoint' ? '起点' : '终点'));
  };

  // 计算
  var handleView = function handleView() {
    var _startPointFeature$va, _startPointFeature$va2, _endPointFeature$valu, _endPointFeature$valu2;
    var startPoint = (startPointFeature === null || startPointFeature === void 0 || (_startPointFeature$va = startPointFeature.values_) === null || _startPointFeature$va === void 0 ? void 0 : _startPointFeature$va.code) || (startPointFeature === null || startPointFeature === void 0 || (_startPointFeature$va2 = startPointFeature.values_) === null || _startPointFeature$va2 === void 0 ? void 0 : _startPointFeature$va2.CODE);
    var endPoint = (endPointFeature === null || endPointFeature === void 0 || (_endPointFeature$valu = endPointFeature.values_) === null || _endPointFeature$valu === void 0 ? void 0 : _endPointFeature$valu.code) || (endPointFeature === null || endPointFeature === void 0 || (_endPointFeature$valu2 = endPointFeature.values_) === null || _endPointFeature$valu2 === void 0 ? void 0 : _endPointFeature$valu2.CODE);
    for (var key in highLayers) {
      highLayers[key] && highLayers[key].clearFeatures();
    }
    setTableData([]);
    if (startPoint && endPoint) {
      if (startPoint === endPoint) {
        return _message["default"].info('请选择不同的管点', 2);
      } else {
        if ((startPointFeature === null || startPointFeature === void 0 ? void 0 : startPointFeature.key) !== (endPointFeature === null || endPointFeature === void 0 ? void 0 : endPointFeature.key)) {
          return _message["default"].info('请选择相同类型的管点分析！', 2);
        }
        var activeKey = _service["default"].getActiveKeyfromPointToLine(options, startPointFeature === null || startPointFeature === void 0 ? void 0 : startPointFeature.key);
        var lineType = _service["default"].getLineType(options, activeKey);
        var params = {
          lineType: lineType,
          pointStartCode: startPoint,
          pointEndCode: endPoint
        };
        setActiveKey(activeKey);
        var lineSelectType;
        if (['YSJ'].includes(startPointFeature === null || startPointFeature === void 0 ? void 0 : startPointFeature.key)) {
          lineSelectType = 1;
        } else if (['WSJ'].includes(startPointFeature === null || startPointFeature === void 0 ? void 0 : startPointFeature.key)) {
          lineSelectType = 2;
        }
        params.lineSelectType = lineSelectType;
        var first = true;
        getData({
          params: params,
          lineType: lineType,
          startPoint: startPoint,
          endPoint: endPoint,
          first: first,
          lineSelectType: lineSelectType
        });
      }
    } else {
      return _message["default"].info('请选择起终点', 2);
    }
  };
  var getData = function getData(_ref) {
    var params = _ref.params,
      lineType = _ref.lineType,
      startPoint = _ref.startPoint,
      endPoint = _ref.endPoint,
      first = _ref.first,
      lineSelectType = _ref.lineSelectType;
    setVisible(true);
    _request["default"].post("".concat(_config["default"].publicUrlZNFX, "/api/gisAnalyze/concatAnalysis"), {
      body: _objectSpread({}, params)
    }).then(function (res) {
      if (String(res === null || res === void 0 ? void 0 : res.result) === '0') {
        // lineType： 1-排水 2-供水
        if (res !== null && res !== void 0 && res.data) {
          var connectRes = [];
          connectRes = res.data;
          getHighLayers(connectRes);
          setTableData({
            pointEndCode: endPoint,
            pointStartCode: startPoint,
            tableData: connectRes,
            result: '连通'
          });
        } else {
          if (first) {
            var _params = {
              lineType: lineType,
              pointStartCode: endPoint,
              pointEndCode: startPoint,
              lineSelectType: lineSelectType
            };
            first = false;
            getData({
              params: _params,
              lineType: lineType,
              startPoint: startPoint,
              endPoint: endPoint,
              first: first
            });
          } else {
            _message["default"].info('当前暂无连通！', 2);
            setTableData({
              pointEndCode: endPoint,
              pointStartCode: startPoint,
              tableData: [],
              result: '不连通'
            });
          }
        }
      } else {
        _message["default"].error((res === null || res === void 0 ? void 0 : res.err) || '', 2);
      }
    });
  };
  // 获取先关高亮图层数据
  var getHighLayers = function getHighLayers() {
    var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    // 销毁高亮图层
    for (var key in highLayers) {
      highLayers[key] && highLayers[key].destroy();
    }
    var layersData = {
      '管点-1': [],
      // 排水管点
      '管线-1': [],
      // 排水管线
      '管点-2': [],
      // 供水管点
      '管线-2': [],
      //供水管线
      '管点-3': [],
      // 原水管点
      '管线-3': [] // 原水管点
    };
    data.length && data.map(function (item) {
      if (item.category && item.lineType) {
        layersData["".concat(item.category, "-").concat(item.lineType)].push(item);
      }
    });
    var _loop = function _loop(_key) {
      var codes = '';
      if (layersData[_key] && layersData[_key].length) {
        layersData[_key].map(function (item, index) {
          codes += index === layersData[_key].length - 1 ? "'".concat(item.code, "'") : "'".concat(item.code, "',");
        });
      }
      var otherParams = (options === null || options === void 0 ? void 0 : options.find(function (v) {
        return v.key === paramsName[_key];
      })) || {};
      var field = (otherParams === null || otherParams === void 0 ? void 0 : otherParams.field) || 'code';
      var params = _objectSpread({
        map: map,
        filter: "".concat(field, " in (").concat(codes, ")"),
        otherParams: _key.includes('管线') ? {
          style: function style(feature) {
            return _style(feature);
          }
        } : {}
      }, otherParams);
      if (codes) {
        highLayers[paramsName[_key]] = new _ConnectivityAnalysis["default"](params);
        // highLayers[paramsName[key]].filter = `${field} in (${codes})`;
        highLayers[paramsName[_key]].setLayerData({
          viewparams: "tenantId:".concat(tenantId)
        }).then(function () {});
      }
    };
    for (var _key in layersData) {
      _loop(_key);
    }
  };
  var onCloseModal = function onCloseModal() {
    setVisible(false);
    setDrawTypeStart(null);
    setDrawTypeEnd(null);
    setStartPointFeature(null);
    setEndPointFeature(null);
    onClose && onClose();
    for (var key in highLayers) {
      highLayers[key] && highLayers[key].clearFeatures();
    }
  };
  var classnames = function classnames(name) {
    if (name) {
      return "ol-map-analysis-connectivity-analysis-".concat(name);
    }
    return "ol-map-analysis-connectivity-analysis";
  };
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_Modal["default"], {
    title: "\u8FDE\u901A\u6027\u5206\u6790",
    onClose: onCloseModal,
    width: 450
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: classnames()
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: classnames('row')
  }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_RequiredLabel["default"], {
    label: "\u8D77\u70B9",
    type: "field"
  }), (0, _FormItem.useRenderInput)({
    itemProps: {
      placeholder: '请选择起点',
      value: (startPointFeature === null || startPointFeature === void 0 || (_startPointFeature$va3 = startPointFeature.values_) === null || _startPointFeature$va3 === void 0 ? void 0 : _startPointFeature$va3.code) || (startPointFeature === null || startPointFeature === void 0 || (_startPointFeature$va4 = startPointFeature.values_) === null || _startPointFeature$va4 === void 0 ? void 0 : _startPointFeature$va4.CODE),
      addonAfter: /*#__PURE__*/_react["default"].createElement(_MyIcon["default"], {
        type: "icon-public-zuobiaoshiqu",
        onClick: function onClick() {
          return getSelect('startPoint');
        }
        // className="dotIcon"
        ,
        className: classnames('dotIcon')
      })
    }
  })), /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      width: 12
    }
  }), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_RequiredLabel["default"], {
    label: "\u7EC8\u70B9",
    type: "field"
  }), (0, _FormItem.useRenderInput)({
    itemProps: {
      placeholder: '请选择终点',
      value: (endPointFeature === null || endPointFeature === void 0 || (_endPointFeature$valu3 = endPointFeature.values_) === null || _endPointFeature$valu3 === void 0 ? void 0 : _endPointFeature$valu3.code) || (endPointFeature === null || endPointFeature === void 0 || (_endPointFeature$valu4 = endPointFeature.values_) === null || _endPointFeature$valu4 === void 0 ? void 0 : _endPointFeature$valu4.CODE),
      addonAfter: /*#__PURE__*/_react["default"].createElement(_MyIcon["default"], {
        type: "icon-public-zuobiaoshiqu",
        onClick: function onClick() {
          return getSelect('endPoint');
        },
        className: classnames('dotIcon')
      })
    }
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: classnames('row')
  }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    onClick: handleView,
    type: "primary"
  }, "\u8BA1\u7B97")))), /*#__PURE__*/_react["default"].createElement(_featureDraw["default"], {
    feature: startPointFeature,
    drawType: drawTypeStart,
    onDrawEnd: onDrawStart,
    style: function style() {
      return [new _style3.Style({
        image: new _style3.Circle({
          radius: 18,
          fill: new _style3.Fill({
            color: '#ff4d4f'
          })
        })
      }), (0, _style2.TextStyle)({
        text: '起点'
      })];
    }
  }), /*#__PURE__*/_react["default"].createElement(_featureDraw["default"], {
    drawType: drawTypeEnd,
    onDrawEnd: onDrawEnd,
    feature: endPointFeature,
    style: function style() {
      return [new _style3.Style({
        image: new _style3.Circle({
          radius: 18,
          fill: new _style3.Fill({
            color: '#ff4d4f'
          })
        })
      }), (0, _style2.TextStyle)({
        text: '终点'
      })];
    }
  }));
}
var _default = exports["default"] = Search;
//# sourceMappingURL=Search.js.map
