'use strict';

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

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

exports.removeDuplicate = removeDuplicate;

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

function recurseDeleteKey(currentLevel, arrayKeyAttr) {
  var currentArray = arrayKeyAttr.slice(0);

  if (arrayKeyAttr.length === 1) {
    delete currentLevel[arrayKeyAttr[0]];
  }

  Array.prototype.forEach.call(currentArray, function (key) {
    if (currentLevel[key] != null) {
      currentLevel = currentLevel[key];
      currentArray.shift();
      recurseDeleteKey(currentLevel, currentArray);
      if ((typeof currentLevel === 'undefined' ? 'undefined' : _typeof(currentLevel)) === 'object' && Object.prototype.toString.call(currentLevel) === '[object Array]') {
        Array.prototype.forEach.call(currentLevel, function (item) {
          recurseDeleteKey(item, currentArray);
        });
      } else {
        recurseDeleteKey(currentLevel, currentArray);
      }
    }
  });
}

function removeDuplicate(text, json) {
  var regAbe = /{{abe[\S\s].*?duplicate=['|"]([\S\s].*?['|"| ]}})/g;
  var matches = text.match(regAbe);
  if (matches != null) {

    Array.prototype.forEach.call(matches, function (match) {
      var keyAttr = _.cmsData.regex.getAttr(match, 'key');

      if (match != null) {
        var arrayKeyAttr = keyAttr.split('.');
        recurseDeleteKey(json, arrayKeyAttr);
      }
    });
  }

  return json;
}