'use strict';

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

var _geojsonArea = require('@mapbox/geojson-area');

var _geojsonArea2 = _interopRequireDefault(_geojsonArea);

var _constants = require('../constants');

var _constants2 = _interopRequireDefault(_constants);

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

/**
 * @文件说明: 定义Feature要素排序函数
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-05-28 11:19:12
 */

var FEATURE_SORT_RANKS = {
  Point: 0,
  LineString: 1,
  Polygon: 2
};

function comparator(a, b) {
  var score = FEATURE_SORT_RANKS[a.geometry.type] - FEATURE_SORT_RANKS[b.geometry.type];
  if (score === 0 && a.geometry.type === _constants2['default'].geojsonTypes.POLYGON) {
    return a.area - b.area;
  }
  return score;
}

function sortFeatures(features) {
  return features.map(function (feature) {
    if (feature.geometry.type === _constants2['default'].geojsonTypes.POLYGON) {
      feature.area = _geojsonArea2['default'].geometry({
        type: _constants2['default'].geojsonTypes.FEATURE,
        property: {},
        geometry: feature.geometry
      });
    }
    return feature;
  }).sort(comparator).map(function (feature) {
    delete feature.area;
    return feature;
  });
}

exports['default'] = sortFeatures;