"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.tool = exports.addPointToArray = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _component = _interopRequireDefault(require("./component"));

var _utils = require("../../utils");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

var addPointToArray = function addPointToArray(point, arr) {
  arr = arr || [];

  if (arr.length === 0) {
    return {
      points: [point],
      closed: false
    };
  } else if (arr.length === 1) {
    if ((0, _utils.equalPoints)(point, arr[0])) {
      return {
        points: arr,
        closed: false
      };
    } else {
      return {
        points: [].concat((0, _toConsumableArray2["default"])(arr), [point]),
        closed: false
      };
    }
  } else if (arr.length >= 2) {
    var closed = (0, _utils.equalPoints)(point, arr[0]);

    if (closed) {
      return {
        points: arr,
        closed: closed
      };
    } else {
      var hasPoint = !!arr.find(function (p) {
        return (0, _utils.equalPoints)(p, point);
      });

      if (hasPoint) {
        return {
          points: arr,
          closed: false
        };
      } else {
        return {
          points: [].concat((0, _toConsumableArray2["default"])(arr), [point]),
          closed: false
        };
      }
    }
  }
};

exports.addPointToArray = addPointToArray;

var tool = function tool() {
  return {
    type: 'polygon',
    Component: _component["default"],
    complete: function complete(data, mark) {
      return _objectSpread(_objectSpread({}, mark), {}, {
        building: false,
        closed: true
      });
    },
    addPoint: function addPoint(point, mark) {
      if (!mark) {
        return {
          type: 'polygon',
          points: [point],
          closed: false,
          building: true
        };
      } else {
        var _addPointToArray = addPointToArray(point, mark.points),
            closed = _addPointToArray.closed,
            points = _addPointToArray.points;

        return _objectSpread(_objectSpread({}, mark), {}, {
          closed: closed,
          points: points,
          building: !closed
        });
      }
    }
  };
};

exports.tool = tool;
//# sourceMappingURL=index.js.map