/**
 * Social share: Web Share API + copy link (clipboard).
 */
(function () {
	'use strict';

	var L = typeof wppsSocialShare === 'object' && wppsSocialShare ? wppsSocialShare : {};

	function copyFallback(text) {
		var ta = document.createElement('textarea');
		ta.value = text;
		ta.setAttribute('readonly', '');
		ta.style.position = 'fixed';
		ta.style.left = '-9999px';
		document.body.appendChild(ta);
		ta.select();
		var ok = false;
		try {
			ok = document.execCommand('copy');
		} catch (e) {
			ok = false;
		}
		document.body.removeChild(ta);
		return ok;
	}

	function announce(root, message) {
		var el = root.querySelector('.wpps-social-share__status');
		if (el) {
			el.textContent = message;
			setTimeout(function () {
				el.textContent = '';
			}, 2500);
		}
	}

	function setCopyButtonState(btn, labelText) {
		var span = btn.querySelector('.wpps-share-link__text--copy');
		if (span && labelText) {
			span.textContent = labelText;
		}
	}

	function initWebShare() {
		document.querySelectorAll('.wpps-web-share-trigger').forEach(function (btn) {
			if (navigator.share) {
				btn.addEventListener('click', function (e) {
					e.preventDefault();
					var title = btn.getAttribute('data-share-title') || '';
					var text = btn.getAttribute('data-share-text') || '';
					var url = btn.getAttribute('data-share-url') || window.location.href;
					navigator.share({ title: title, text: text, url: url }).catch(function (err) {
						if (err.name !== 'AbortError') {
							// eslint-disable-next-line no-console
							console.warn('Web Share failed:', err);
						}
					});
				});
			} else {
				btn.classList.add('wpps-web-share-unsupported');
			}
		});
	}

	function initCopyLink() {
		document.querySelectorAll('.wpps-copy-link-trigger').forEach(function (btn) {
			btn.addEventListener('click', function (e) {
				e.preventDefault();
				var url = btn.getAttribute('data-copy-url') || window.location.href;
				var root = btn.closest('.wpps-social-share');
				var defaultLabel = L.copyLabel || 'Copy link';
				var copiedLabel = L.copiedLabel || 'Link copied';
				var failedLabel = L.copyFailed || 'Could not copy';

				var done = function (ok) {
					if (ok && root) {
						announce(root, copiedLabel);
					} else if (root) {
						announce(root, failedLabel);
					}
				};

				if (navigator.clipboard && navigator.clipboard.writeText) {
					navigator.clipboard.writeText(url).then(function () {
						setCopyButtonState(btn, copiedLabel);
						done(true);
						setTimeout(function () {
							setCopyButtonState(btn, defaultLabel);
						}, 2000);
					}).catch(function () {
						var ok = copyFallback(url);
						if (ok) {
							setCopyButtonState(btn, copiedLabel);
							done(true);
							setTimeout(function () {
								setCopyButtonState(btn, defaultLabel);
							}, 2000);
						} else {
							done(false);
						}
					});
				} else {
					var ok = copyFallback(url);
					if (ok) {
						setCopyButtonState(btn, copiedLabel);
						done(true);
						setTimeout(function () {
							setCopyButtonState(btn, defaultLabel);
						}, 2000);
					} else {
						done(false);
					}
				}
			});
		});
	}

	var overflowEscapeBound = false;

	function bindOverflowEscapeOnce() {
		if (overflowEscapeBound) return;
		overflowEscapeBound = true;
		document.addEventListener('keydown', function (e) {
			if (e.key !== 'Escape') return;
			document.querySelectorAll('.wpps-social-share__overflow').forEach(function (wrap) {
				if (wrap.hasAttribute('hidden')) return;
				var root = wrap.closest('.wpps-social-share--has-overflow');
				var openBtn = root && root.querySelector('.wpps-share-overflow-open');
				wrap.setAttribute('hidden', '');
				if (openBtn) {
					openBtn.setAttribute('aria-expanded', 'false');
					openBtn.focus();
				}
			});
		});
	}

	function initFloatingOverflow() {
		bindOverflowEscapeOnce();
		document.querySelectorAll('.wpps-social-share--has-overflow').forEach(function (root) {
			if (root.getAttribute('data-wpps-overflow-init') === '1') return;
			root.setAttribute('data-wpps-overflow-init', '1');
			var wrap = root.querySelector('.wpps-social-share__overflow');
			var openBtn = root.querySelector('.wpps-share-overflow-open');
			if (!wrap || !openBtn) return;
			var closeEls = wrap.querySelectorAll('.wpps-share-overflow-close');

			function closePanel() {
				wrap.setAttribute('hidden', '');
				openBtn.setAttribute('aria-expanded', 'false');
				openBtn.focus();
			}

			openBtn.addEventListener('click', function (e) {
				e.preventDefault();
				wrap.removeAttribute('hidden');
				openBtn.setAttribute('aria-expanded', 'true');
				var closer = wrap.querySelector('.wpps-social-share__overflow-close');
				if (closer) closer.focus();
			});
			closeEls.forEach(function (el) {
				el.addEventListener('click', function (e) {
					e.preventDefault();
					closePanel();
				});
			});
		});
	}

	function init() {
		initWebShare();
		initCopyLink();
		initFloatingOverflow();
	}

	if (document.readyState === 'loading') {
		document.addEventListener('DOMContentLoaded', init);
	} else {
		init();
	}
})();
