'use strict';

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

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

var _constants2 = _interopRequireDefault(_constants);

var _map_event_to_bounding_box = require('./map_event_to_bounding_box');

var _map_event_to_bounding_box2 = _interopRequireDefault(_map_event_to_bounding_box);

var _sort_features = require('./sort_features');

var _sort_features2 = _interopRequireDefault(_sort_features);

var _string_set = require('./string_set');

var _string_set2 = _interopRequireDefault(_string_set);

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

/**
 * @文件说明: 根据鼠标坐标查找Feature要素
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-05-22 14:35:15
 */

var META_TYPES = [_constants2['default'].meta.FEATURE, _constants2['default'].meta.MIDPOINT, _constants2['default'].meta.VERTEX];

function featuresAtClick(event, bbox, ctx) {
  return featuresAt(event, bbox, ctx, ctx.options.clickBuffer);
}

function featuresAtTouch(event, bbox, ctx) {
  return featuresAt(event, bbox, ctx, ctx.options.touchBuffer);
}

function featuresAt(event, bbox, ctx, buffer) {
  if (ctx.iMapApi === null) return [];

  var box = event ? (0, _map_event_to_bounding_box2['default'])(event, buffer) : bbox;
  var features = ctx.iMapApi.queryRenderedFeatures('draw-layers-group.' + ctx.uid, box);
  features = features.filter(function (feature) {
    return META_TYPES.indexOf(feature.properties['draw:meta']) !== -1;
  });

  var featureIds = new _string_set2['default']();
  var uniqueFeatures = [];
  features.map(function (feature) {
    var featureId = feature.properties['draw:id'];
    if (featureIds.has(featureId)) return;
    featureIds.add(featureId);
    uniqueFeatures.push(feature);
  });

  return (0, _sort_features2['default'])(uniqueFeatures);
}

exports['default'] = {
  click: featuresAtClick,
  touch: featuresAtTouch
};