'use strict';

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

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _hat = require('hat');

var _hat2 = _interopRequireDefault(_hat);

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-27 18:23:13
 */

var Feature = function Feature(ctx, geojson) {
  this.ctx = ctx;
  this.properties = geojson.properties || {};
  this.coordinates = geojson.geometry.coordinates;
  this.id = geojson.id || (0, _hat2['default'])();
  this.type = geojson.geometry.type;
};

Feature.prototype.changed = function () {
  this.ctx.store.featureChanged(this.id);
};

Feature.prototype.incomingCoords = function (coords) {
  this.setCoordinates(coords);
};

Feature.prototype.setCoordinates = function (coords) {
  this.coordinates = coords;
  this.changed();
};

Feature.prototype.getCoordinates = function () {
  return JSON.parse(JSON.stringify(this.coordinates));
};

Feature.prototype.setProperty = function (property, value) {
  this.properties[property] = value;
};

Feature.prototype.toGeoJSON = function () {
  return JSON.parse(JSON.stringify({
    id: this.id,
    type: _constants2['default'].geojsonTypes.FEATURE,
    properties: this.properties,
    geometry: {
      coordinates: this.getCoordinates(),
      type: this.type
    }
  }));
};

Feature.prototype.internal = function (mode) {
  var properties = (0, _extends3['default'])({
    'draw:id': this.id,
    'draw:mode': mode,
    'draw:meta': _constants2['default'].meta.FEATURE,
    'draw:type': this.type,
    'draw:active': _constants2['default'].activeStates.INACTIVE
  }, this.properties);
  this.properties = properties;

  return this;
};

Feature.prototype.updateInternalProperty = function (key, value) {
  this.properties['draw:' + key] = value;
  return this;
};

exports['default'] = Feature;