"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.transformLonLatToWGS84 = exports.transformGeometryInfoToWGS84 = exports.transformGeometry = exports.getDataByHttp = exports.createFeatureCollection = exports.createFeature = void 0;
var _http = _interopRequireDefault(require("../_util/http"));
var _mapTool = _interopRequireDefault(require("../_util/mapTool"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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 _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(arr) { if (Array.isArray(arr)) return arr; }
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; }
var transformLonLatToWGS84 = function transformLonLatToWGS84(lonLat, coordType) {
  var _lonLat = _toConsumableArray(lonLat).map(function (v) {
    return Number(v);
  });
  if (coordType === 'bmap') {
    var gcj = _mapTool["default"].bd09togcj02(lonLat[0], lonLat[1]);
    _lonLat = _mapTool["default"].gcj02towgs84(gcj[0], gcj[1]);
  } else if (coordType === 'amap') {
    _lonLat = _mapTool["default"].gcj02towgs84(lonLat[0], lonLat[1]);
  }
  return _lonLat;
};
exports.transformLonLatToWGS84 = transformLonLatToWGS84;
var transformGeometryInfoToWGS84 = function transformGeometryInfoToWGS84(lngLats, type, coordType) {
  var lngLatArr = [];
  if (lngLats) {
    if (type === 'point') {
      var _l = typeof lngLats === 'string' ? lngLats.split(',') : lngLats;
      lngLatArr = transformLonLatToWGS84(_l, coordType);
    } else if (['polygon', 'polyline'].includes(type)) {
      lngLatArr[0] = [];
      lngLats.split(';').map(function (res) {
        var _res$split = res.split(','),
          _res$split2 = _slicedToArray(_res$split, 2),
          lng = _res$split2[0],
          lat = _res$split2[1];
        lngLatArr[0].push(transformLonLatToWGS84([Number(lng), Number(lat)], coordType));
      });
    } else {
      if (Array.isArray(lngLats)) {
        lngLatArr = lngLats;
      }
    }
  }
  return lngLatArr;
};
exports.transformGeometryInfoToWGS84 = transformGeometryInfoToWGS84;
var transformGeometry = function transformGeometry(geometryInfo) {
  /**
   * geometry: {
   *  "type": "Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon | GeometryCollection",
   *  "coordinates": []
   * }
   */

  var type = geometryInfo.type,
    lngLats = geometryInfo.lngLats,
    coordType = geometryInfo.coordType;

  /**
   * geometryInfo: {
   *  coordType: "wgs84"
   *  lngLats: "119.02486914921874,25.63890915087647"
   *  type: "point"
   * }
   */

  var _type = type.replace(/^\S/, function (s) {
    return s.toUpperCase();
  });
  return {
    type: _type,
    coordinates: transformGeometryInfoToWGS84(lngLats, type, coordType)
  };
};
exports.transformGeometry = transformGeometry;
var createFeature = function createFeature(v) {
  return {
    type: 'Feature',
    geometry: transformGeometry(v.geometryInfo),
    properties: _objectSpread({}, v)
  };
};
exports.createFeature = createFeature;
var createFeatureCollection = function createFeatureCollection(data) {
  var featureCollection = {
    type: 'FeatureCollection',
    features: []
  };
  var features = [];
  data.map(function (v) {
    if (v.geometryInfo) {
      features.push(createFeature(v));
    }
  });
  featureCollection.features = features;
  return featureCollection;
};
exports.createFeatureCollection = createFeatureCollection;
var getDataByHttp = function getDataByHttp(url, params, method) {
  var request = _http["default"].get;
  if (method === 'POST' || method === 'post') {
    request = _http["default"].post;
  }
  return request(url, {
    body: params
  }).then(function (res) {
    var _res$data;
    if (url.indexOf('/geoserver/') !== -1) {
      return res;
    }
    if (res !== null && res !== void 0 && (_res$data = res.data) !== null && _res$data !== void 0 && _res$data.length) {
      return createFeatureCollection(res.data);
    }
  });
};
exports.getDataByHttp = getDataByHttp;
//# sourceMappingURL=createFeatureCollection.js.map
