/**
 * Accessibility Widget premium tools — packed only in the Pro overlay.
 */
(function () {
	'use strict';

	var SVG_FILTERS = '<svg class="wpps-a11y-svg-filters" aria-hidden="true" focusable="false" width="0" height="0" style="position:absolute;width:0;height:0;overflow:hidden">'
		+ '<filter id="wpps-a11y-protanopia"><feColorMatrix type="matrix" values="0.567,0.433,0,0,0 0.558,0.442,0,0,0 0,0.242,0.758,0,0 0,0,0,1,0"/></filter>'
		+ '<filter id="wpps-a11y-deuteranopia"><feColorMatrix type="matrix" values="0.625,0.375,0,0,0 0.7,0.3,0,0,0 0,0.3,0.7,0,0 0,0,0,1,0"/></filter>'
		+ '<filter id="wpps-a11y-tritanopia"><feColorMatrix type="matrix" values="0.95,0.05,0,0,0 0,0.433,0.567,0,0 0,0.475,0.525,0,0 0,0,0,1,0"/></filter>'
		+ '</svg>';

	var linkNavIndex = -1;

	function isRendered(el) {
		var rects = el.getClientRects();
		return rects.length > 0 && rects[0].width > 0 && rects[0].height > 0;
	}

	function isPageElement(el) {
		return !el.closest('.wpps-a11y-widget') && isRendered(el);
	}

	window.wppsA11yPremium = {
		ensureSvgFilters: function () {
			if (!document.getElementById('wpps-a11y-protanopia')) {
				var holder = document.createElement('div');
				holder.innerHTML = SVG_FILTERS;
				var svg = holder.firstChild;
				if (svg) {
					document.documentElement.appendChild(svg);
				}
			}
		},
		navigateLinks: function (direction) {
			var links = Array.prototype.slice.call(document.querySelectorAll('a[href]')).filter(isPageElement);
			if (!links.length) {
				return;
			}
			linkNavIndex += direction;
			if (linkNavIndex < 0) {
				linkNavIndex = links.length - 1;
			}
			if (linkNavIndex >= links.length) {
				linkNavIndex = 0;
			}
			var link = links[linkNavIndex];
			link.scrollIntoView({ behavior: 'smooth', block: 'center' });
			link.focus({ preventScroll: true });
		},
		readSelectionAloud: function (announce, i18n) {
			i18n = i18n || {};
			if (!window.speechSynthesis || !window.SpeechSynthesisUtterance) {
				if (typeof announce === 'function') {
					announce(i18n.ttsUnsupported || 'Text-to-speech is not supported in this browser.');
				}
				return;
			}
			var text = '';
			var selection = window.getSelection();
			if (selection && !selection.isCollapsed) {
				text = (selection.toString() || '').trim();
			}
			if (!text) {
				if (window.speechSynthesis.speaking) {
					window.speechSynthesis.cancel();
					return;
				}
				if (typeof announce === 'function') {
					announce(i18n.selectTextTts || 'Select text on the page, then activate read aloud.');
				}
				return;
			}
			window.speechSynthesis.cancel();
			var utterance = new window.SpeechSynthesisUtterance(text);
			utterance.lang = document.documentElement.lang || 'en';
			window.speechSynthesis.speak(utterance);
		},
		applyProfile: function (profileKey, ctx) {
			ctx = ctx || {};
			var profiles = (window.wppsA11yWidget && window.wppsA11yWidget.profiles) || {};
			var profile = profiles[profileKey];
			if (!profile || !profile.settings || typeof ctx.resetAll !== 'function') {
				return;
			}
			ctx.resetAll();
			Object.keys(profile.settings).forEach(function (key) {
				ctx.prefs[key] = profile.settings[key];
			});
			ctx.prefs.activeProfile = profileKey;
			if (typeof ctx.sanitizePrefs === 'function') {
				ctx.prefs = ctx.sanitizePrefs(ctx.prefs);
			}
			if (typeof ctx.applyAll === 'function') {
				ctx.applyAll();
			}
			if (typeof ctx.savePrefs === 'function') {
				ctx.savePrefs();
			}
			if (typeof ctx.announce === 'function') {
				ctx.announce((ctx.i18n && ctx.i18n.profileApplied) || 'Profile applied.');
			}
			return ctx.prefs;
		}
	};
})();
