(function () {
	'use strict';

	var copied = (window.wppsImageSizesPanel && window.wppsImageSizesPanel.copied) || 'Copied!';

	function copyUrl(url) {
		if (navigator.clipboard && navigator.clipboard.writeText) {
			return navigator.clipboard.writeText(url);
		}
		var input = document.createElement('input');
		input.value = url;
		input.style.cssText = 'position:fixed;left:-9999px';
		document.body.appendChild(input);
		input.select();
		try {
			document.execCommand('copy');
			return Promise.resolve();
		} catch (err) {
			return Promise.reject(err);
		} finally {
			input.remove();
		}
	}

	document.addEventListener('click', function (e) {
		var btn = e.target.closest('.wpps-image-sizes-panel .wpps-isp-copy-btn');
		var url;
		if (btn && (url = btn.getAttribute('data-url'))) {
			copyUrl(url).then(function () {
				var label = btn.textContent;
				btn.textContent = copied;
				btn.disabled = true;
				btn.setAttribute('aria-label', copied);
				setTimeout(function () {
					btn.textContent = label;
					btn.disabled = false;
					btn.removeAttribute('aria-label');
				}, 1500);
			});
			return;
		}
		var input = e.target.closest('.wpps-image-sizes-panel .wpps-isp-url-input');
		if (input) {
			input.select();
		}
	});
})();
