'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports['default'] = render;

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

var _constants2 = _interopRequireDefault(_constants);

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

var _find = require('lodash/find');

var _find2 = _interopRequireDefault(_find);

var _hat = require('hat');

var _hat2 = _interopRequireDefault(_hat);

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

/**
 * @文件说明: 定义绘图数据的渲染刷新
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-05-26 17:24:30
 */

function render() {
  var store = this;
  var ctx = this.ctx;
  var iMapApi = this.iMapApi;

  // 判断绘制图层数据源是否不存在，则直接忽略
  var drawSource = iMapApi.getSource(_constants2['default'].sources.ID + '.' + this.ctx.uid);
  if (!drawSource) {
    return cleanup && cleanup();
  }

  var mode = ctx.events.currentModeName();
  // 清除记录
  var cleanup = function cleanup() {
    store.isModeChangeRender = false;
    store.clearChangedIds();
  };
  // 渲染Feature要素
  var renderFeature = function renderFeature(id) {
    var feature = store.get(id);
    feature = feature.updateInternalProperty('mode', mode);
    feature = feature.toGeoJSON();
    var currentFeature = null;
    ctx.events.currentModeRender(feature, function (geojson) {
      currentFeature = geojson;
    });
    return currentFeature;
  };

  var oldSourcesLength = store.sources.length;
  var featureIds = store.getAllIds();
  var changedIds = store.getChangedIds().filter(function (id) {
    var feature = store.get(id);
    return feature !== null && feature !== undefined;
  });
  var selectedIds = store.getSelectedIds().filter(function (id) {
    var feature = store.get(id);
    return feature !== null && feature !== undefined;
  });

  var newSources = [];
  var renderList = [];
  // 遍历绘图的Feature要素集合，对需更新的Feature要素进行重渲染
  (0, _methodsUtil.isNotEmptyArray)(featureIds) && featureIds.map(function (id) {
    var feature = null;
    // 判断是否为模式切换刷新渲染 | 是否在更新状态列表中 | 是否在选中状态列表中
    if (store.isModeChangeRender || changedIds.indexOf(id) !== -1 || selectedIds.indexOf(id) !== -1) {
      feature = renderFeature(id);
      renderList.push(feature);
    } else {
      feature = (0, _find2['default'])(store.sources, { id: id });
    }
    feature && newSources.push(feature);
  });

  // 判断是否执行有要素的选中状态变化
  if (store._emitSelectionChange) {
    ctx.api.fire(_constants2['default'].events.SELECTION_CHANGE, {
      mode: mode,
      features: store.getSelected().map(function (feature) {
        return feature.toGeoJSON();
      }),
      points: store.getSelectedCoordinates().map(function (coordinate) {
        return {
          id: (0, _hat2['default'])(),
          type: _constants2['default'].geojsonTypes.FEATURE,
          properties: {
            'draw:meta': _constants2['default'].meta.VERTEX,
            'draw:type': _constants2['default'].geojsonTypes.POINT,
            'draw:pid': coordinate.pid,
            'draw:path': coordinate.path
          },
          geometry: {
            type: _constants2['default'].geojsonTypes.POINT,
            coordinates: coordinate.coordinates
          }
        };
      })
    });
    store._emitSelectionChange = false;
  }

  // 判断是否有待删除的Feature要素
  if (store._deletedFeaturesToEmit && store._deletedFeaturesToEmit.length) {
    var emitFeatures = [];
    var emitFeatureIds = [];
    store._deletedFeaturesToEmit.map(function (feature) {
      emitFeatureIds.push(feature.id);
      emitFeatures.push(feature.toGeoJSON());
    });
    // 移除数据源的删除Feature要素
    store.sources = store.sources.filter(function (feature) {
      return emitFeatureIds.indexOf(feature.id) === -1;
    });
    store._deletedFeaturesToEmit = [];
    // 绘制要素删除时执行回调事件
    ctx.api.fire(_constants2['default'].events.DELETE, { mode: mode, features: emitFeatures });
  }

  // 判断是否有绘图数据源更新，则重新渲染图层数据
  if (!!(renderList && renderList.length) || !newSources.length && newSources.length !== oldSourcesLength.length) {
    store.sources = [].concat(newSources);
    // 设置绘图的数据源数据
    drawSource.setData({
      type: _constants2['default'].geojsonTypes.FEATURE_COLLECTION,
      features: store.sources
    });
    // 绘制要素数据时执行回调事件
    ctx.api.fire(_constants2['default'].events.RENDER, { mode: mode });
  }

  // 清理待更新队列缓存
  cleanup();
}