{% if REVISIONS %}
<script id="ppr-cmpout-tpl" type="text/x-jsrender">
<div class="post has-profile bg3 comparison" id="<<:cmpId>>" style="display:none;">
	<div class="inner">
		<dl class="postprofile">
			<dt class="has-avatar">
				<div class="ppr-diff-arrow">
					<i class="fa fa-arrow-up"></i>
				</div>
			</dt>
		</dl>
		<div class="postbody">
			<h3>{{ lang('PRIMEPOSTREVISIONS_COMPARISON') ~ lang('COLON') }} <a href="#<<:cmpId>>"><<:cmpSubj>></a></h3>
			<div class="content"><<:cmpText>></div>
		</div>
	</div>
</div>
</script>

<script id="ppr-cmpbtn-tpl" type="text/x-jsrender">
<li data-skip-responsive="true">
	<a class="button icon-button compare" data-cmp="<<:cmpId>>" title="{{ lang('PRIMEPOSTREVISIONS_COMPARE') }}">
		<i class="icon fa-balance-scale fa-fw"></i>
		<span class="sr-only">{{ lang('PRIMEPOSTREVISIONS_COMPARE') }}</span>
	</a>
</li>
</script>

{% INCLUDEJS '@primehalo_primepostrevisions/diff_match_patch.js' %}

<script>
/**
 * Prime Post Revisions, JavaScript for revision comparison.
 */

// Compare 2 revisions
function cmpData($textA, $textB) {
	let dmp		= new diff_match_patch();
	let diffs	= dmp.diff_main($textA, $textB);
	let cmpText	= dmp.diff_prettyHtml(diffs).replace(/&lt;(img.*)&gt;/g, '<$1>');
	let matches = cmpText.match(/<ins [^>]+>(.*?)<\/ins>|<del [^>]+>(.*?)<\/del>/g)
	if (matches != null) {
		matches.map(function(val) {
			cmpText = cmpText.replace(val, val.replace(/&para;/g, '&crarr;'));
		});
	}
	return cmpText.replace(/&para;/g, '');
}

// Compare all revisions with the adjacent post
$postList = $('#revisions_form div.post');
for (let i = 0, len = $postList.length; i < len - 1; i++) {
	let $postA	= $postList.eq(i);
	let $postB	= $postList.eq(i + 1);
	let $subjA	= $('h3 a', $postA).text();
	let $subjB	= $('h3 a', $postB).text();
	let $textA	= $('textarea.ppr-post-bbcode', $postA).val();
	let $textB	= $('textarea.ppr-post-bbcode', $postB).val();
	let cmpSubj	= cmpData($subjB, $subjA);
	let cmpText	= cmpData($textB, $textA);
	let cmpId	= 'cmp-' + i + '-' + (i + 1);
	let cmpBtn	= $('#ppr-cmpbtn-tpl').html().replace(/<<:cmpId>>/g, cmpId);
	let cmpOut	= $('#ppr-cmpout-tpl').html().replace(/<<:cmpId>>/g, cmpId).replace(/<<:cmpSubj>>/g, cmpSubj).replace(/<<:cmpText>>/g, cmpText);
	$postA.after(cmpOut);
	$postB.find('.post-buttons').prepend(cmpBtn);
}

// Toggle Diff post on click of compare button
$('div.post').on('click', 'a.compare', function() {
	let cmpId = $(this).data('cmp');
	$('#' + cmpId).slideToggle();
});

// Enable or disable the action buttons depending on if any of the checkboxes are checked
function updatePrimePostRevActionBtn() {
	let checkedCnt = $('#revisions_form input[name="revision_list[]"]:checkbox:checked').length;
	let $btns = $('#revisions_form input[type="submit"]');
	if (checkedCnt > 0) {
		$btns.removeAttr('disabled').removeClass('disabled');
	} else {
		$btns.attr('disabled', 'disabled').addClass('disabled');
	}

	// Disable the delete button if only the current version of the post is selected
	if (checkedCnt == 1 && $('#revision_list_0').is(':checked')) {
		$btns.filter('[name="delete"]').attr('disabled', 'disabled').addClass('disabled');
	}

	// If a delete button exists and the current version of the post is selected then show the notice about not being able to delete the post
	if ($btns.filter('[name="delete"]').length && $('#revision_list_0').is(':checked')) {
		$('#ppr-post-delete-notice:hidden').show('fast');	// If it's hidden then show it
	} else {
		$('#ppr-post-delete-notice:visible').hide('fast');	// If it's visible then hide it
	}
}

// Watch each checkbox to update the select action button status when a change occurs
$('#revisions_form').on('change', 'input[name="revision_list[]"]', function() {
	updatePrimePostRevActionBtn();
});
updatePrimePostRevActionBtn();
</script>
{% endif %}
