/**
 * Topping Sets Admin JavaScript
 *
 * @package Foodord
 */

document.addEventListener('DOMContentLoaded', function() {
	'use strict';

	const adminConfig = (typeof window.foodordToppingSetsAdmin === 'object' && window.foodordToppingSetsAdmin !== null)
		? window.foodordToppingSetsAdmin
		: {};
	const ajaxUrl = (typeof window.ajaxurl === 'string') ? window.ajaxurl : '';
	const derivedAdminUrl = ajaxUrl ? ajaxUrl.replace('admin-ajax.php', 'admin.php') : window.location.origin + '/wp-admin/admin.php';
	const adminBaseUrl = adminConfig.adminUrl || derivedAdminUrl;
	const toppingsPageSlug = adminConfig.toppingsPage || 'foodord-toppings';

	/**
	 * Topping Sets functionality
	 */
	const ToppingSets = {
		/**
		 * Initialize all functionality
		 */
		init: function() {
			this.bindEvents();
			this.initializeComponents();
		},

		/**
		 * Bind all event listeners
		 */
		bindEvents: function() {
			this.setupRemoveConfirmation();
			this.setupEditButtons();
			this.setupEmptyStateCTA();
		},

		/**
		 * Initialize UI components
		 */
		initializeComponents: function() {
			this.setupCategoryToggles();
		},

		/**
		 * Toggle extra category chips in rows when more than two are available.
		 */
		setupCategoryToggles: function() {
			const toggleButtons = document.querySelectorAll('.foodord-topping-set-row__more');
			if (!toggleButtons.length) {
				return;
			}

			toggleButtons.forEach(function(button) {
				button.addEventListener('click', function() {
					const container = button.closest('.foodord-topping-set-row__chips');
					if (!container) {
						return;
					}

					const isExpanded = container.classList.toggle('is-expanded');
					const moreLabel = button.dataset.moreLabel || '';
					const lessLabel = button.dataset.lessLabel || '';
					button.textContent = isExpanded && lessLabel ? lessLabel : moreLabel;
					button.setAttribute('aria-expanded', isExpanded ? 'true' : 'false');
				});
			});
		},

		/**
		 * Setup edit buttons to redirect to toppings page with edit parameters.
		 */
		setupEditButtons: function() {
			const editButtons = document.querySelectorAll('.foodord-edit-topping-set');
			if (!editButtons.length) {
				return;
			}

			editButtons.forEach(function(button) {
				button.addEventListener('click', function() {
					const setId = button.getAttribute('data-set-id');
					if (!setId) {
						return;
					}

					const editUrl = new URL(adminBaseUrl);
					editUrl.searchParams.set('page', toppingsPageSlug);
					editUrl.searchParams.set('action', 'edit');
					editUrl.searchParams.set('set_id', setId);

					window.location.href = editUrl.toString();
				});
			});
		},

		/**
		 * Setup empty state CTA button navigation.
		 */
		setupEmptyStateCTA: function() {
			const ctaButton = document.getElementById('foodord-create-topping-set');
			if (!ctaButton) {
				return;
			}

			ctaButton.addEventListener('click', function(event) {
				event.preventDefault();
				const createUrl = new URL(adminBaseUrl);
				createUrl.searchParams.set('page', toppingsPageSlug);
				window.location.href = createUrl.toString();
			});
		},

		/**
		 * Confirm deletion before following the delete link.
		 */
		setupRemoveConfirmation: function() {
			const deleteLinks = document.querySelectorAll('.foodord-delete-topping-set');
			if (!deleteLinks.length) {
				return;
			}

			deleteLinks.forEach(function(link) {
				link.addEventListener('click', function(event) {
					const message = link.getAttribute('data-confirm');
					if (message && !window.confirm(message)) {
						event.preventDefault();
						return false;
					}
				});
			});
		}
	};

	// Auto hide only success notices after saving (after 5 seconds)
	function autoHideNotices() {
		const successNotices = document.querySelectorAll('.notice.notice-success.is-dismissible');
		if (successNotices.length > 0) {
			successNotices.forEach(function(notice) {
				setTimeout(function() {
					notice.classList.add('notice-fade-out');
					setTimeout(function() {
						notice.remove();
					}, 500);
				}, 5000);
			});
		}
	}

	// Initialize when DOM is ready
	ToppingSets.init();

	// Hide notices after 1 second delay
	setTimeout(autoHideNotices, 1000);
});
