/**
 *---------------------------------------------------------------------------------------
 * @package      VP Conversion Tracking - Joomla! System Plugin
 * @subpackage   For VirtueMart 3+
 *---------------------------------------------------------------------------------------
 * @copyright    Copyright (C) 2012-2021 VirtuePlanet Services LLP. All rights reserved.
 * @license      GNU General Public License version 2 or later; see LICENSE.txt
 * @authors      Abhishek Das
 * @email        info@virtueplanet.com
 * @link         https://www.virtueplanet.com
 *---------------------------------------------------------------------------------------
*/

var VPCT = window.VPCT || {};

(function($) {
	$.fn.extend({
		insertSnippetAtCaret: function(myValue) {
			return this.each(function(i) {
				if (document.selection) {
					//For browsers like Internet Explorer
					this.focus();
					var sel = document.selection.createRange();
					sel.text = myValue;
					this.focus();
				} else if (this.selectionStart || this.selectionStart == '0') {
					//For browsers like Firefox and Webkit based
					var startPos = this.selectionStart;
					var endPos = this.selectionEnd;
					var scrollTop = this.scrollTop;
					this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
					this.focus();
					this.selectionStart = startPos + myValue.length;
					this.selectionEnd = startPos + myValue.length;
					this.scrollTop = scrollTop;
				} else {
					this.value += myValue;
					this.focus();
				}
			});
		}
	});
	
	$.fn.extend({
		insertItemsSnippetAtCaret: function(myValue) {
			return this.each(function(i) {
				if (document.selection) {
					//For browsers like Internet Explorer
					this.focus();
					var sel = document.selection.createRange();
					sel.text = myValue;
					this.focus();
				} else if (this.selectionStart || this.selectionStart == '0') {
					//For browsers like Firefox and Webkit based
					var startPos = this.selectionStart;
					var endPos = this.selectionEnd;
					var scrollTop = this.scrollTop;
					var previousText = this.value.substring(0, startPos);
					// If items loop opened but not closed then we are already in the loop
					if(previousText && previousText.indexOf('{{# items }}') >= 0 && previousText.indexOf('{{/ items }}') == -1) {
						myValue = myValue.replace('{{# items }}', '');
						myValue = myValue.replace('{{/ items }}', '');
						myValue = myValue.trim();
						if(previousText.indexOf('{{# items }}' + "\n") == -1) {
							myValue = "\n" + myValue;
						} 
					}
					this.value = previousText + myValue + this.value.substring(endPos, this.value.length);
					this.focus();
					this.selectionStart = startPos + myValue.length;
					this.selectionEnd = startPos + myValue.length;
					this.scrollTop = scrollTop;
				} else {
					this.value += myValue;
					this.focus();
				}
			});
		}
	});

	VPCT.addSnippet = function(button) {
		var $fieldset = $(button).closest('fieldset'),
			$textarea = $('textarea.code-snippet', $fieldset),
			$select = $('select.vpct-button-options', $fieldset),
			snippet = $select.val();
			
		if (!snippet || !$textarea.length) {
			return false;
		}
		
		if(snippet.indexOf('{{# items }}') >= 0) {console.log('ee')
			$textarea.insertItemsSnippetAtCaret(snippet);
		} else {
			$textarea.insertSnippetAtCaret(snippet);
		}
		
		return false;
	};
})(jQuery);