(function(){
	const removeExternalAdminNotices = () => {
		document.querySelectorAll('#wpbody-content .notice, #wpbody-content .updated, #wpbody-content .error').forEach((notice) => {
			if (!notice.classList.contains('vidvix-own-notice')) notice.remove();
		});
	};

	removeExternalAdminNotices();

	const wpBodyContent = document.getElementById('wpbody-content');
	if (wpBodyContent && window.MutationObserver) {
		new MutationObserver(removeExternalAdminNotices).observe(wpBodyContent, {
			childList: true,
			subtree: true
		});
	}

	const buildShortcode = (videoId) => videoId ? '[vidvix video_id="' + videoId + '"]' : '';
	const buildVideoId = (value) => {
		const cleanValue = value.trim();
		if (!cleanValue) return '';
		const path = cleanValue.split(/[?#]/)[0];
		const filename = path.split('/').filter(Boolean).pop() || '';
		const basename = filename.replace(/\.[^.]+$/, '') || 'video';
		return basename.toLowerCase().replace(/[^a-z0-9_-]+/g, '-').replace(/^-+|-+$/g, '') || 'video';
	};
	const syncRow = (row) => {
		const videoIdInput = row.querySelector('.vidvix-video-id-input');
		const shortcodeInput = row.querySelector('.vidvix-shortcode-preview');
		if (!videoIdInput || !shortcodeInput) return;
		shortcodeInput.value = buildShortcode(videoIdInput.value.trim());
	};
	const bindRow = (row) => {
		row.querySelectorAll('.vidvix-video-id-input').forEach(function(input){
			input.addEventListener('input', function(){
				syncRow(row);
			});
		});
		row.querySelectorAll('.vidvix-catalog-file-input').forEach(function(input){
			input.addEventListener('input', function(){
				const videoIdInput = row.querySelector('.vidvix-video-id-input');
				if (videoIdInput && !videoIdInput.value.trim()) {
					videoIdInput.value = buildVideoId(input.value);
					syncRow(row);
				}
			});
		});
		syncRow(row);
	};
	const tableBody = document.querySelector('[data-vidvix-catalog-body]');
	if (tableBody) {
		tableBody.querySelectorAll('.vidvix-catalog-row').forEach(bindRow);
	}
	document.addEventListener('click', function(event){
		const button = event.target.closest('.vidvix-remove-row');
		if (!button) return;
		const catalogIndex = button.getAttribute('data-catalog-index');
		if (catalogIndex !== null) {
			document.querySelectorAll('[data-catalog-index="' + catalogIndex + '"]').forEach(function(item){
				item.remove();
			});
		}
		button.disabled = true;
		const form = document.getElementById('vidvix-settings-form');
		if (form && form.requestSubmit) {
			form.requestSubmit();
			return;
		}
		if (form) form.submit();
	});
	document.addEventListener('click', function(event){
		const button = event.target.closest('.vidvix-copy-button');
		if (!button) return;
		const targetId = button.getAttribute('data-copy-target');
		let input = targetId ? document.getElementById(targetId) : null;
		if (!input) {
			const row = button.closest('.vidvix-catalog-row');
			input = row ? row.querySelector('.vidvix-shortcode-preview') : null;
		}
		if (!input) {
			const row = button.closest('.vidvix-shortcode-row');
			input = row ? row.querySelector('input') : null;
		}
		if (!input) return;
		input.select();
		input.setSelectionRange(0, input.value.length);
		const showCopiedState = function(){
			button.classList.add('is-copied');
			setTimeout(function(){ button.classList.remove('is-copied'); }, 450);
		};
		navigator.clipboard.writeText(input.value).then(function(){
			showCopiedState();
		}).catch(function(){
			document.execCommand('copy');
			showCopiedState();
		});
	});
})();
