"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getBox = exports["default"] = exports.computeMinMax = void 0;
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _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 _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
/**
 * 兼容最小最大值
 * @param minCoordinate
 * @param maxCoordinate
 * @param padding
 * @returns {[*,*,*,*]}
 */
var computeMinMax = exports.computeMinMax = function computeMinMax(minCoordinate, maxCoordinate, padding) {
  if (padding) {
    if (Array.isArray(padding)) {
      if (padding.length === 2) {
        minCoordinate[0] = minCoordinate[0] - padding[0];
        maxCoordinate[0] = maxCoordinate[0] + padding[0];
        minCoordinate[1] = minCoordinate[1] - padding[1];
        maxCoordinate[1] = maxCoordinate[1] + padding[1];
      } else if (padding.length === 4) {
        maxCoordinate[0] = maxCoordinate[0] + padding[0];
        maxCoordinate[1] = maxCoordinate[1] + padding[1];
        minCoordinate[1] = minCoordinate[1] - padding[2];
        minCoordinate[0] = minCoordinate[0] - padding[3];
      }
    } else {
      maxCoordinate[0] = maxCoordinate[0] + padding;
      minCoordinate[0] = minCoordinate[0] - padding;
      maxCoordinate[1] = maxCoordinate[1] + padding;
      minCoordinate[1] = minCoordinate[1] - padding;
    }
  }
  return [minCoordinate[0], minCoordinate[1], maxCoordinate[0], maxCoordinate[1]];
};
var getPaddingByUnit = function getPaddingByUnit(padding, unit) {
  if (unit === 'px') {
    // 将像素值转换为度
    var pixelToDegreeConversionFactor = 1 / 111000; // 1 度大约等于 111000 像素
    padding = padding * pixelToDegreeConversionFactor;
  } else if (unit === 'm') {
    // 将 padding 值从米转换为度
    padding = padding / 111000; // 1 度大约等于 111000 米
  }
  return padding;
};
var transformPaddingByUnit = function transformPaddingByUnit(padding, unit) {
  if (padding) {
    if (Array.isArray(padding)) {
      padding = padding.map(function (item) {
        return getPaddingByUnit(item, unit);
      });
    } else {
      padding = getPaddingByUnit(padding, unit);
    }
  }
  return padding;
};

/**
 * 获取边界
 * @param coordinates
 * @param options
 * @returns {[*,*,*,*]}
 */
var getBox = exports.getBox = function getBox(coordinates, options) {
  // 找到最小的经度和纬度值
  var minCoordinate = [];
  var maxCoordinate = [];
  coordinates.forEach(function (c, index) {
    var i = index % 2;
    if (i === 0) {
      if (minCoordinate[0] === undefined || c < minCoordinate[0]) {
        minCoordinate[0] = c;
      }
      if (maxCoordinate[0] === undefined || c > maxCoordinate[0]) {
        maxCoordinate[0] = c;
      }
    } else if (i === 1) {
      if (minCoordinate[1] === undefined || c < minCoordinate[1]) {
        minCoordinate[1] = c;
      }
      if (maxCoordinate[1] === undefined || c > maxCoordinate[1]) {
        maxCoordinate[1] = c;
      }
    }
  });
  if (options) {
    var padding = options.padding,
      unit = options.unit;
    return computeMinMax(minCoordinate, maxCoordinate, transformPaddingByUnit(padding, unit));
  }
  return [minCoordinate[0], minCoordinate[1], maxCoordinate[0], maxCoordinate[1]];
};
var _default = exports["default"] = function _default(geoJson, options) {
  var coordinates = [];
  geoJson.features.map(function (feature) {
    var c = feature.geometry.coordinates.flat(Infinity);
    coordinates.push.apply(coordinates, _toConsumableArray(c));
  });
  return getBox(coordinates, options);
};
//# sourceMappingURL=getBBoxFromGeojson.js.map
