'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _extend = require('extend');

var _extend2 = _interopRequireDefault(_extend);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Form = function () {
  function Form() {
    _classCallCheck(this, Form);

    this._form = {};
    this._key = [];
  }

  /**
   * Get all input from a template
   * @return {Array} array of input form
   */


  _createClass(Form, [{
    key: 'dontHaveKey',


    /**
     * Check if key is not is the form array
     * @param  {[type]} key [description]
     * @return {[type]}     [description]
     */
    value: function dontHaveKey(key) {
      return this._key[key] == null;
    }

    /**
     * Add entry to abe engine form
     * @param {String} type        textarea | text | meta | link | image | ...
     * @param {String} key         unique ID, no space allowed
     * @param {String} desc        input description
     * @param {Int}    max-length   maximum characteres allowed inside input
     * @param {String} tab         tab name
     * @param {String} jsonValue   
     * @return {Void}
     */

  }, {
    key: 'add',
    value: function add(obj) {
      var defaultValues = {
        autocomplete: null,
        block: '',
        desc: '',
        display: null,
        editable: true,
        key: '',
        'max-length': null,
        order: 0,
        placeholder: '',
        prefill: false,
        'prefill-quantity': null,
        reload: false,
        required: false,
        source: null,
        tab: 'default',
        type: 'text',
        value: '',
        visible: true,
        precontribTemplate: ''
      };

      obj = (0, _extend2.default)(true, defaultValues, obj);
      obj.key = obj.key.replace(/\./, '-');

      if (obj.key.indexOf('[') < 0 && obj.key.indexOf('.') > -1) {
        obj.block = obj.key.split('.')[0];
      }

      if (this._form[obj.tab] == null) this._form[obj.tab] = { item: [] };

      this._key[obj.key] = true; // save key for dontHaveKey()
      this._form[obj.tab].item.push(obj);
    }
  }, {
    key: 'form',
    get: function get() {
      return this._form;
    }
  }]);

  return Form;
}();

exports.default = Form;