// ---------------------------------------------------
// Dakidarts Numerology Core - Admin JS
// ---------------------------------------------------

document.addEventListener('DOMContentLoaded', function () {
    // -------------------------
    // Auto-dismiss notices after 5 seconds
    // -------------------------
    const notices = document.querySelectorAll('.dakidarts-notice');
    if (notices.length) {
        notices.forEach(notice => {
            setTimeout(() => {
                notice.classList.add('dakidarts-notice--fade-out');
                setTimeout(() => notice.remove(), 400);
            }, 5000);
        });
    }

    // -------------------------
    // Tab switching
    // -------------------------
    const tabs = document.querySelectorAll('.dakidarts-nav-tab');
    const tabContents = document.querySelectorAll('.dakidarts-tab-content');

    if (tabs.length) {
        tabs.forEach(tab => {
            tab.addEventListener('click', function (e) {
                e.preventDefault();

                // Remove active class
                tabs.forEach(t => t.classList.remove('nav-tab-active'));
                tabContents.forEach(c => c.style.display = 'none');

                // Activate current tab
                this.classList.add('nav-tab-active');
                const target = this.getAttribute('data-tab');
                const content = document.getElementById(target);
                if (content) content.style.display = 'block';
            });
        });

        // Activate first tab by default
        tabs[0].click();
    }

    // -------------------------
    // Save button feedback (do not disable submit)
    // -------------------------
    const apiKeyInput = document.getElementById('dakidarts_api_key');
    const saveButton = document.querySelector('.dakidarts-save-btn');

    if (apiKeyInput && saveButton) {
        saveButton.addEventListener('click', function () {
            saveButton.textContent = 'Saving...';
        });
    }

    // -------------------------
    // Optional: live shortcode preview
    // -------------------------
    const shortcodePreview = document.getElementById('dakidarts_shortcode_preview');
    const endpointSelect = document.querySelectorAll('.dakidarts-endpoint-toggle');

    if (shortcodePreview) {
        endpointSelect.forEach(input => {
            input.addEventListener('change', function () {
                const endpoint = this.getAttribute('data-endpoint');
                shortcodePreview.textContent = `[dakidarts_numerology endpoint="${endpoint}"]`;
            });
        });
    }

    // -------------------------
    // Route selection + language toggle
    // -------------------------
    const routeInputs = document.querySelectorAll('input[name="route"]');
    const langField = document.querySelector('[data-route-field="gateway"]');

    const toggleLangField = () => {
        if (!langField) return;
        const selected = document.querySelector('input[name="route"]:checked');
        const isGateway = selected && selected.value === 'gateway';
        langField.style.display = isGateway ? 'grid' : 'none';
    };

    if (routeInputs.length) {
        routeInputs.forEach(input => {
            input.addEventListener('change', toggleLangField);
        });
        toggleLangField();
    }
});
