(function () {
	'use strict';

	function onReady(callback) {
		if (document.readyState === 'loading') {
			document.addEventListener('DOMContentLoaded', callback, { once: true });
			return;
		}
		callback();
	}

	onReady(function () {
		var page = document.querySelector('.subin-email-log-page');
		if (!page) {
			return;
		}

		var detail = page.querySelector('[data-subin-event-detail]');
		var close = page.querySelector('[data-subin-event-detail-close]');
		var selectedRow = page.querySelector('.subin-log-row.is-selected');
		if (detail && window.location.search.indexOf('event_id=') !== -1) {
			window.requestAnimationFrame(function () {
				detail.focus({ preventScroll: true });
				(selectedRow || detail).scrollIntoView({
					behavior: window.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'auto' : 'smooth',
					block: 'start'
				});
			});
		}

		document.addEventListener('keydown', function (event) {
			if (event.key === 'Escape' && detail && close) {
				event.preventDefault();
				window.location.assign(close.href);
			}
		});

		var popovers = Array.prototype.slice.call(page.querySelectorAll('.subin-log-popover'));
		var exportPopover = page.querySelector('[data-subin-export-popover]');
		if (exportPopover && window.location.hash === '#subin-email-log-exports') {
			exportPopover.open = true;
		}
		popovers.forEach(function (popover) {
			popover.addEventListener('toggle', function () {
				if (!popover.open) {
					return;
				}
				popovers.forEach(function (other) {
					if (other !== popover) {
						other.open = false;
					}
				});
			});
		});

		document.addEventListener('click', function (event) {
			popovers.forEach(function (popover) {
				if (popover.open && !popover.contains(event.target)) {
					popover.open = false;
				}
			});
		});

		var form = page.querySelector('#subin-email-log-form');
		var origin = page.querySelector('#subin-email-log-origin');
		if (form && origin) {
			form.addEventListener('change', function () {
				if (!form.value) {
					return;
				}
				var provider = form.value.split('|')[0];
				var matching = Array.prototype.some.call(origin.options, function (option) {
					return option.value === 'provider:' + provider;
				});
				if (matching) {
					origin.value = 'provider:' + provider;
				}
			});
			origin.addEventListener('change', function () {
				if (!form.value) {
					return;
				}
				var formProvider = form.value.split('|')[0];
				if (origin.value !== 'provider:' + formProvider) {
					form.value = '';
				}
			});
		}

		var period = page.querySelector('#subin-email-log-period');
		var dateFrom = page.querySelector('#subin-email-log-from');
		var dateTo = page.querySelector('#subin-email-log-to');
		if (period && dateFrom && dateTo) {
			period.addEventListener('change', function () {
				dateFrom.value = '';
				dateTo.value = '';
			});
			[dateFrom, dateTo].forEach(function (field) {
				field.addEventListener('change', function () {
					if (field.value) {
						period.value = '';
					}
				});
			});
		}
	});
}());
