"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.addPosition = addPosition;
exports.removePosition = removePosition;
exports.default = tableConfFrom;

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var POSITION_KEY = "_position";

var DEFAULT_TABLE_CONF = {
  cellEdit: {
    mode: "click",
    blurToSave: true
  },
  options: {},
  handleConfirmDeleteRow: function handleConfirmDeleteRow(next) {
    return next();
  }
};

function addPosition(data) {
  return data.map(function (el, i) {
    return Object.assign({}, el, _defineProperty({}, POSITION_KEY, i));
  });
}

function removePosition(data) {
  if (Array.isArray(data)) {
    return data.map(function (el) {
      return removePosition(el);
    });
  } else {
    var dataCopy = Object.assign({}, data);
    delete dataCopy[POSITION_KEY];
    return dataCopy;
  }
}

function tableConfFrom(_ref) {
  var _ref$table = _ref.table,
      table = _ref$table === undefined ? {} : _ref$table;
  var formData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  var afterSaveCell = arguments[2];
  var afterDeleteRow = arguments[3];

  if (table.keyField === undefined || table.keyField === POSITION_KEY) {
    table.keyField = POSITION_KEY;
    formData = addPosition(formData);
  }

  var tableConf = Object.assign({
    data: formData
  }, DEFAULT_TABLE_CONF, table);

  tableConf.cellEdit.afterSaveCell = afterSaveCell;
  tableConf.options.afterDeleteRow = afterDeleteRow;

  return tableConf;
}