'use strict';

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

var _ = require('../../');

/**
* Get All attributes from a Abe tag
* @return {Object} parsed attributes
*/
function getAll(str, json) {
  str = _.abeExtend.hooks.instance.trigger('beforeAbeAttributes', str, json);

  //This regex analyzes all attributes of a Abe tag 
  var re = /\b([a-z][a-z0-9\-]*)\s*=\s*("([^"]+)"|'([^']+)'|(\S+))/ig;

  var attrs = {
    autocomplete: null,
    desc: '',
    hint: '',
    display: null,
    editable: true,
    key: '',
    'max-length': null,
    'min-length': 0,
    order: 0,
    prefill: false,
    'prefill-quantity': null,
    reload: false,
    required: false,
    source: null,
    tab: 'default',
    type: 'text',
    value: '',
    file: '',
    visible: true
  };

  for (var match; match = re.exec(str);) {
    attrs[match[1]] = match[3] || match[4] || match[5];
  }

  attrs.sourceString = attrs.source;
  attrs.source = attrs.source ? json != null && json['abe_source'] != null ? json['abe_source'][attrs.key] : null : null;
  attrs.editable = attrs.editable && attrs.editable !== 'false' ? true : false;

  attrs = _.abeExtend.hooks.instance.trigger('afterAbeAttributes', attrs, str, json);

  return attrs;
}

function sanitizeSourceAttribute(obj, jsonPage) {
  if (obj.sourceString != null && obj.sourceString.indexOf('{{') > -1) {
    var matches = obj.sourceString.match(/({{[a-zA-Z._]+}})/g);
    if (matches !== null) {
      Array.prototype.forEach.call(matches, function (match) {
        var val = match.replace('{{', '');
        val = val.replace('}}', '');

        try {
          val = eval('jsonPage.' + val);
        } catch (e) {
          val = '';
        }
        obj.sourceString = obj.sourceString.replace(match, val);
      });
    }
  }

  return obj;
}