"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.transformRgb = exports.setHighlightLayer = exports.getLineTotalLength = exports.getField = exports.getExtend = exports.getCoordinates = exports.getCoordinate = exports.getCoorStr = void 0;
var _Polygon = require("ol/geom/Polygon");
/**
 * @description: 框选传参处理
 * @param {*} geometry feature几何数据
 * @param {*} drawType 工具选中的key
 * @param {Array} flatCoordinates 当前范围
 * @return {*}
 */
var getCoorStr = exports.getCoorStr = function getCoorStr(geometry, drawType, flatCoordinates) {
  var _coordinates;
  if (!drawType) return {
    coorStr: '',
    cqFilter: ''
  };
  var coorStr = '';
  var coordinates = (geometry === null || geometry === void 0 ? void 0 : geometry.getCoordinates()) || (geometry === null || geometry === void 0 ? void 0 : geometry.flatCoordinates);

  // 圈选处理
  if (drawType === 'Circle') {
    var polygonGeometry = new _Polygon.fromCircle(geometry);
    coordinates = polygonGeometry === null || polygonGeometry === void 0 ? void 0 : polygonGeometry.getCoordinates();
  }

  // 当前范围
  if (drawType === 'CurrentView') {
    coordinates = flatCoordinates;
  }
  (_coordinates = coordinates) === null || _coordinates === void 0 || (_coordinates = _coordinates[0]) === null || _coordinates === void 0 || _coordinates.forEach(function (v, i) {
    if (i == 0) {
      coorStr += "".concat(v[0], " ").concat(v[1]);
    } else {
      coorStr += ", ".concat(v[0], " ").concat(v[1]);
    }
  });
  var cqFilter = coorStr ? "INTERSECTS(geo_location, POLYGON((".concat(coorStr, ")))") : '';
  return {
    coorStr: coorStr,
    cqFilter: cqFilter
  };
};

/**
 * @description: 设置高亮
 * @param {*} map 地图
 * @param {*} features
 * @param {*} isFit 是否移到中心
 * @return {*}
 */
var setHighlightLayer = exports.setHighlightLayer = function setHighlightLayer(map, features) {
  var isFit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  var layer = map === null || map === void 0 ? void 0 : map.getLayerByKey('Highlight');
  var source = layer === null || layer === void 0 ? void 0 : layer.getSource();
  if (source) {
    source.clear();
    source.addFeatures(features);
    isFit && (map === null || map === void 0 ? void 0 : map.fitView(features));
  }
};

/**
 * @description: 获取codes集合
 * @param {Array} data 数据
 * @param {*} field
 * @param {*} filterFn 过滤函数
 * @return {*}
 */
var getField = exports.getField = function getField() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var field = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'id';
  var filterFn = arguments.length > 2 ? arguments[2] : undefined;
  var keys = [];
  data.map(function (item) {
    var filter = typeof filterFn === 'function' ? filterFn(item) : true;
    if (item[field] && filter) keys.push(item[field]);
  });
  return keys;
};

/**
 * @description: 获取管线总长度
 * @param {*} field 管长字段
 * @param {*} length 保留小数长度
 * @return {*}
 */
var getLineTotalLength = exports.getLineTotalLength = function getLineTotalLength() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var field = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'length';
  var length = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2;
  var totalLength = 0;
  data === null || data === void 0 || data.forEach(function (item) {
    if (item[field]) {
      totalLength += Number(item[field]);
    }
  });
  return totalLength.toFixed(length);
};

/**
 * @description: 获取几何经纬度数据
 * @param {*} feature
 * @param {*} type 绘制类型
 * @return {*}
 */
var getCoordinates = exports.getCoordinates = function getCoordinates(feature, type) {
  var _feature$getGeometry;
  if (!(feature !== null && feature !== void 0 && feature.getGeometry)) return null;
  var coordinates = feature === null || feature === void 0 || (_feature$getGeometry = feature.getGeometry()) === null || _feature$getGeometry === void 0 ? void 0 : _feature$getGeometry.getCoordinates();
  if (type === 'Circle') {
    var polygonGeometry = new _Polygon.fromCircle(feature === null || feature === void 0 ? void 0 : feature.getGeometry());
    coordinates = polygonGeometry === null || polygonGeometry === void 0 ? void 0 : polygonGeometry.getCoordinates();
  }
  return coordinates;
};

/**
 * @description: 视野范围经纬度
 * @param {*} map
 * @return {*}
 */
var getExtend = exports.getExtend = function getExtend(map) {
  var extend = map.getView().calculateExtent(map.getSize());
  // 视野范围经纬度组成面-闭环
  extend = [[[extend[0], extend[1]],
  // 左下
  [extend[0], extend[3]],
  // 左上
  [extend[2], extend[3]],
  // 右上
  [extend[2], extend[1]],
  // 右下
  [extend[0], extend[1]] // 左下
  ]];
  return extend;
};
var getCoordinate = exports.getCoordinate = function getCoordinate(feature) {
  var _feature$getGeometry2;
  var coordinates = feature.getGeometry().getCoordinates();
  var type = feature === null || feature === void 0 || (_feature$getGeometry2 = feature.getGeometry()) === null || _feature$getGeometry2 === void 0 ? void 0 : _feature$getGeometry2.getType();
  if (['MultiLineString'].includes(type)) {
    coordinates = coordinates[0];
  }
  return coordinates;
};
var transformRgb = exports.transformRgb = function transformRgb(color) {
  var n = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  // 16进制颜色值的正则
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  // 把颜色值变成小写
  var newColor = color.toLowerCase();
  if (reg.test(newColor)) {
    // 如果只有三位的值，需变成六位，如：#fff => #ffffff
    if (newColor.length === 4) {
      var colorNew = '#';
      for (var i = 1; i < 4; i += 1) {
        colorNew += newColor.slice(i, i + 1).concat(newColor.slice(i, i + 1));
      }
      newColor = colorNew;
    }
    // 处理六位的颜色值，转为RGB
    var colorChange = [];
    for (var _i = 1; _i < 7; _i += 2) {
      colorChange.push(parseInt("0x".concat(newColor.slice(_i, _i + 2))));
    }
    var _rgb = "rgba(".concat(colorChange.join(','), ",").concat(n, ")");
    return _rgb;
  } else {
    return newColor;
  }
};
//# sourceMappingURL=utils.js.map
