"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.calcIntersection = exports.IntersectionQueryStyle = void 0;
var _ol = require("ol");
var _geom = require("ol/geom");
var _style = require("ol/style");
var _turf = require("@turf/turf");
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 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; }
var getCoordinates = function getCoordinates(feature) {
  var _coordinates;
  var coordinates = feature.getGeometry().getCoordinates();
  if (((_coordinates = coordinates) === null || _coordinates === void 0 ? void 0 : _coordinates.length) === 1) {
    coordinates = coordinates[0];
  }
  return coordinates;
};
var getLine = function getLine(v) {
  var coordinates = getCoordinates(v);
  return (0, _turf.lineString)(coordinates);
};
/**
 * 计算交叉点
 * @param features
 * @returns {*[]}
 */
var calcIntersection = exports.calcIntersection = function calcIntersection(features) {
  var result = [];
  var P = {};
  if (features !== null && features !== void 0 && features.length) {
    features.map(function (v) {
      var start_line = getLine(v);
      features.map(function (v2) {
        var end_line = getLine(v2);

        // 使用 turf.booleanCrosses 判断是否相交
        var intersection = (0, _turf.lineIntersect)(start_line, end_line);

        // 如果存在交叉点，那么将这两条线添加到对象中
        if (intersection.features.length > 0) {
          var geo = intersection.features[0].geometry.coordinates.join(',');
          P[geo] = P[geo] || [];
          P[geo].push(v, v2);
        }
      });
    });
  }
  var _loop = function _loop() {
    var _list = [];
    var list = P[i].filter(function (item) {
      return !_list.includes(item.values_.code) && _list.push(item.values_.code);
    });
    var point = new _ol.Feature({
      geometry: new _geom.Point(i.split(','))
    });
    point.setProperties({
      list: list,
      code: i
    });
    result.push({
      point: point,
      list: list
    });
  };
  for (var i in P) {
    _loop();
  }
  return result;
};

/**
 * 获取交叉点样式
 * @param feature
 * @returns {*}
 * @constructor
 */
var IntersectionPointStyle = function IntersectionPointStyle(feature) {
  return new _style.Style({
    image: new _style.Circle({
      radius: 7,
      fill: new _style.Fill({
        color: '#ff4d4f'
      }),
      stroke: new _style.Stroke({
        color: '#FFFFFF',
        width: 2
      })
    })
  });
};

/**
 * 获取角度
 * @param feature
 * @param direction
 * @returns {number}
 */
var getRotation = function getRotation(feature) {
  var direction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  var _getCoordinates = getCoordinates(feature),
    _getCoordinates2 = _slicedToArray(_getCoordinates, 2),
    start = _getCoordinates2[0],
    end = _getCoordinates2[1];
  var dx = direction ? start[0] - end[0] : end[0] - start[0];
  var dy = direction ? start[1] - end[1] : end[1] - start[1];
  var rotation = Math.atan2(dy, dx);
  return -rotation + Math.PI;
};

/**
 * 获取文本样式
 * @param text
 * @param style
 * @returns {*}
 * @constructor
 */
var TextStyle = function TextStyle(_ref) {
  var text = _ref.text,
    style = _ref.style;
  return new _style.Style({
    text: new _style.Text(_objectSpread({
      text: String(text),
      font: '14px Calibri,sans-serif',
      fill: new _style.Fill({
        color: '#000'
      }),
      overflow: 'line',
      stroke: new _style.Stroke({
        color: '#fff',
        width: 2
      }),
      textAlign: 'center',
      textBaseline: 'middle'
    }, style))
  });
};

/**
 * 获取管线样式
 * @param feature
 * @returns {[*,*,*,*]}
 * @constructor
 */
var IntersectionLineStyle = function IntersectionLineStyle(feature) {
  var _feature$values_, _feature$values_2;
  var code = (_feature$values_ = feature.values_) === null || _feature$values_ === void 0 ? void 0 : _feature$values_.code;
  var _ref2 = ((_feature$values_2 = feature.values_) === null || _feature$values_2 === void 0 ? void 0 : _feature$values_2.data_json) && JSON.parse(feature.values_.data_json) || {},
    flowDirectionId = _ref2.flowDirectionId,
    ds = _ref2.ds;
  ds = ds || Number(ds) === 0 ? "DN".concat(ds, "mm") : '';

  // 获取角度
  var direction = Number(flowDirectionId) === 0;
  var rotation = getRotation(feature, direction);

  // 获取流向
  var flowDirectionStyle = TextStyle({
    feature: feature,
    text: '➤',
    style: {
      rotation: rotation,
      fill: new _style.Fill({
        color: '#ff4d4f'
      })
    }
  });

  // 获取编码
  var lineCodeStyle = TextStyle({
    feature: feature,
    text: code,
    direction: direction,
    style: {
      rotation: rotation,
      textBaseline: 'top',
      offsetY: 4
    }
  });

  // 获取管径
  var dsStyle = TextStyle({
    feature: feature,
    text: ds,
    direction: direction,
    style: {
      rotation: rotation,
      textBaseline: 'bottom',
      offsetY: -4
    }
  });
  return [new _style.Style({
    stroke: new _style.Stroke({
      color: '#ff4d4f',
      width: 4
    })
  }), flowDirectionStyle, lineCodeStyle, dsStyle];
};
var IntersectionQueryStyle = exports.IntersectionQueryStyle = function IntersectionQueryStyle(feature) {
  var styles = [];
  if (feature.getGeometry() instanceof _geom.Point) {
    styles.push(IntersectionPointStyle(feature));
  } else {
    styles.push.apply(styles, _toConsumableArray(IntersectionLineStyle(feature)));
  }
  return styles;
};
//# sourceMappingURL=IntersectionQuery.js.map
