'use strict';

/**
 * @ngdoc filter
 * @name annotationAppApp.filter:replaceAngularTags
 * @function
 * @description
 * # replaceAngularTags
 * Filter in the annotationAppApp.
 */
angular.module('app')
	.filter('replaceAngularTags', function() {
		return function(input) {

			var _runReplacingByRegex = function(html, regexes, checkForLongerTags) {

				if (typeof checkForLongerTags === 'undefined') {
					checkForLongerTags = true;
				}

				// checkForLongerTags = typeof checkForLongerTags !== 'undefined' || true;

				regexes.forEach(function(regex) {
					var match, index, tmp, isMatchingLongerTag;

					while (match = regex[0].exec(html)) {
						index = match.index;

						tmp = html.slice(0, index);
						isMatchingLongerTag = tmp.match(/>#[^>#]+$/gi);
						if (checkForLongerTags && !isMatchingLongerTag) {
							html = html.replace(match[0], regex[1]);
						} else if (!checkForLongerTags) { // case needed to remove underlying...
							html = html.replace(regex[0], regex[1]);
						}
					}

				});
				return html;
			};



			var removeAnyAngularAddedTagByString = function(str) {
				var regexes = [
					[new RegExp('<span class="ng-scope">((\n|.)*?(?=<\/span>))<\/span>', 'gi'), '$1'], // if you change order, you are going to break the test and effectiveness
					[new RegExp('<br class="ng-scope">'), '<br>']
				];

				return _runReplacingByRegex(str, regexes, false);
			};

			

			return removeAnyAngularAddedTagByString(input);


		};
	});