'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _assign = require('lodash/assign');

var _assign2 = _interopRequireDefault(_assign);

var _methodsUtil = require('../../_util/methods-util');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

/**
 * @文件说明: 构建Map地图的“要素”管理函数方法
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-05-26 11:06:52
 */

var feature = {
  /**
   * 获取FeatureCollection要素集合GeoJSON对象
   * @param {FeatureCollection} features 要素集合对象
   */
  getFeatureCollection: function getFeatureCollection(features) {
    var turf = this.exports.turf;

    if (turf) {
      if ((0, _methodsUtil.isNotEmptyArray)(features)) {
        return turf.featureCollection(features);
      } else if (features && features.type) {
        switch (features.type) {
          case 'FeatureCollection':
            return features;
          case 'Feature':
            return turf.featureCollection([features]);
        }
      }
    }
    return turf.featureCollection([]);
  },


  /**
   * 获取Feature要素的几何矩形范围
   * @param {FeatureCollection} features 要素集合对象
   */
  getFeaturesToBounds: function getFeaturesToBounds(features) {
    var turf = this.exports.turf;
    // 计算集合的BBOX范围

    var featuresCollection = this.getFeatureCollection(features);
    var bounds = featuresCollection && turf.bbox(featuresCollection);
    return bounds ? [[bounds[0], bounds[1]], [bounds[2], bounds[3]]] : null;
  },


  /**
   * 根据Feature要素集合缩放到合适层级
   * @param {FeatureCollection} features 要素集合对象
   */
  boundsToFeatures: function boundsToFeatures(features) {
    var _this = this;

    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    // 合并缩放参数
    var defaultPadding = {
      top: 100,
      bottom: 100,
      left: 50,
      right: 50
    };
    options.padding = (0, _assign2['default'])({}, defaultPadding, options.padding || {});
    // 获取范围
    var bounds = this.getFeaturesToBounds(features);
    return new Promise(function (resolve, reject) {
      if (bounds) {
        _this.boundsTo(bounds, 500, options).then(function () {
          resolve();
        })['catch'](function () {
          reject();
        });
      } else {
        resolve();
      }
    });
  },


  /**
   * 获取Feature要素的绝对中心点
   * @param {FeatureCollection} features 要素集合对象
   */
  getFeaturesToCenter: function getFeaturesToCenter(features) {
    var turf = this.exports.turf;

    var featureCollection = this.getFeatureCollection(features);
    return featureCollection && featureCollection.features.length ? turf.center(featureCollection) : null;
  },


  /**
   * 获取Feature要素的质心点
   * @param {FeatureCollection} features 要素集合对象
   */
  getFeaturesToCentroid: function getFeaturesToCentroid(features) {
    var turf = this.exports.turf;

    var featureCollection = this.getFeatureCollection(features);
    return featureCollection && featureCollection.features.length ? turf.centroid(featureCollection) : null;
  },


  /**
   * 生成Point Feature要素对象
   * @param {String} id Feature要素的唯一Id
   * @param {Coordinates} coordinates Feature要素的坐标点
   * @param {Object} properties Feature要素的属性对象
   */
  createPointFeature: function createPointFeature(id, coordinates) {
    var properties = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    var turf = this.exports.turf;

    if (turf && id) {
      return turf.point(coordinates, properties, { id: id });
    }
    return null;
  },


  /**
   * 生成MultiPoint Feature要素对象
   * @param {String} id Feature要素的唯一Id
   * @param {Coordinates} coordinates Feature要素的坐标点
   * @param {Object} properties Feature要素的属性对象
   */
  createMultiPointFeature: function createMultiPointFeature(id, coordinates) {
    var properties = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    var turf = this.exports.turf;

    if (turf && id) {
      return turf.multiPoint(coordinates, properties, { id: id });
    }
    return null;
  },


  /**
   * 生成Line Feature要素对象
   * @param {String} id Feature要素的唯一Id
   * @param {Coordinates} coordinates Feature要素的坐标点
   * @param {Object} properties Feature要素的属性对象
   */
  createLineFeature: function createLineFeature(id, coordinates) {
    var properties = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    var turf = this.exports.turf;

    if (turf && id) {
      return turf.lineString(coordinates, properties, { id: id });
    }
    return null;
  },


  /**
   * 生成MultiLineString Feature要素对象
   * @param {String} id Feature要素的唯一Id
   * @param {Coordinates} coordinates Feature要素的坐标点
   * @param {Object} properties Feature要素的属性对象
   */
  createMultiLineFeature: function createMultiLineFeature(id, coordinates) {
    var properties = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    var turf = this.exports.turf;

    if (turf && id) {
      return turf.multiLineString(coordinates, properties, { id: id });
    }
    return null;
  },


  /**
   * 生成Polygon Feature要素对象
   * @param {String} id Feature要素的唯一Id
   * @param {Coordinates} coordinates Feature要素的坐标点
   * @param {Object} properties Feature要素的属性对象
   */
  createPolygonFeature: function createPolygonFeature(id, coordinates) {
    var properties = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    var turf = this.exports.turf;

    if (turf && id) {
      return turf.polygon(coordinates, properties, { id: id });
    }
    return null;
  },


  /**
   * 生成MultiPolygon Feature要素对象
   * @param {String} id Feature要素的唯一Id
   * @param {Coordinates} coordinates Feature要素的坐标点
   * @param {Object} properties Feature要素的属性对象
   */
  createMultiPolygonFeature: function createMultiPolygonFeature(id, coordinates) {
    var properties = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    var turf = this.exports.turf;

    if (turf && id) {
      return turf.multiPolygon(coordinates, properties, { id: id });
    }
    return null;
  },


  /**
   * 生成圆形Circle Feature要素对象
   * @param {String} id Feature要素的唯一Id
   * @param {Coordinates} center 圆形的中心点
   * @param {Number} radius 圆形的半径，单位：KM(千米)
   * @param {Object} properties Feature要素的属性对象
   * @param {Object} options 圆形参数选项；<steps>形成圆形坐标的个数，默认99，既闭合为100个点
   */
  createCircleFeature: function createCircleFeature(id, center) {
    var radius = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0.5;
    var properties = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
    var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
    var turf = this.exports.turf;

    var opts = (0, _assign2['default'])({}, { steps: 99 }, options);
    // 转换中心点位为WGS84
    var wgs84Center = this.toWGS84(center);
    // 获取半径对应的圆形
    var circleFeature = turf.circle(wgs84Center, radius, {
      units: 'kilometers',
      steps: opts.steps
    });
    var coordinates = turf.getCoords(circleFeature);
    // 反转回对应的投影坐标
    coordinates = this.fromWGS84(coordinates);
    // 生成Feature对象
    return coordinates ? this.createPolygonFeature(id, coordinates, properties) : null;
  },


  /**
   * 生成矩形Rectangle Feature要素对象
   * @param {String} id Feature要素的唯一Id
   * @param {Coordinates} point 矩形的起始点
   * @param {Number} width 矩形的宽度，单位：KM(千米)
   * @param {Number} height 矩形的高度，单位：KM(千米)
   * @param {Object} properties Feature要素的属性对象
   */
  createRectangleFeature: function createRectangleFeature(id, point, width, height) {
    var properties = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
    var turf = this.exports.turf;
    // 转换中心点位为WGS84

    var wgs84Point = this.toWGS84(point);
    // 计算对角线距离的点位
    var widthFeature = turf.destination(wgs84Point, Math.abs(width), 90, { units: 'kilometers' });
    var widthPoint = turf.getCoord(widthFeature);
    var heightFeature = turf.destination(widthPoint, Math.abs(height), 180, { units: 'kilometers' });
    var heightPoint = turf.getCoord(heightFeature);
    // 生成矩形的坐标点
    var coordinates = [[[point[0], point[1]], [widthPoint[0], widthPoint[1]], [heightPoint[0], heightPoint[1]], [point[0], heightPoint[1]], [point[0], point[1]]]];
    // 反转回对应的投影坐标
    coordinates = this.fromWGS84(coordinates);
    // 生成Feature对象
    return coordinates ? this.createPolygonFeature(id, coordinates, properties) : null;
  }
};

exports['default'] = feature;