'use strict';

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

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

/**
 * Encode / Escape && add data-abe attributs
 * @param  {String} block
 * @return {String} escaped string
 */
function encodeAbeTagAsComment(block) {
  var matchAbe = block.match(/>\s*\{\{abe .*\}\}/g);
  if (matchAbe) {
    for (var i = 0; i < matchAbe.length; i++) {
      var getattr = _.cmsData.regex.getAttr(matchAbe[i], 'key').replace('.', '[0]-');
      block = block.replace(matchAbe[i], ' data-abe-' + _.cmsData.regex.validDataAbe(getattr) + '="' + getattr + '" >');
    }
  }
  matchAbe = block.match(/( [A-Za-z0-9\-\_]+="*{{.*?}})/g);
  if (matchAbe) {
    for (i = 0; i < matchAbe.length; i++) {
      if (typeof matchAbe !== 'undefined' && matchAbe !== null) {
        getattr = _.cmsData.regex.getAttr(matchAbe[i], 'key').replace('.', '[0]-');
        var matchattr = matchAbe[i].split('=')[0].trim();
        block = block.replace(matchAbe[i], ' data-abe-attr-' + _.cmsData.regex.validDataAbe(getattr) + '="' + matchattr + '"' + ' data-abe-' + _.cmsData.regex.validDataAbe(getattr) + '="' + getattr + '" ' + matchAbe[i]).replace(/\{\{\abe.*?}\}/, '');
      }
    }
  }
  return escape(block);
}