/**
 * Voidek Auto Schema Generator — UX Enhancements JS
 *
 * Handles:
 *  1. Dismissal of the activation notice via AJAX.
 *  2. Copy-to-clipboard for the schema JSON preview.
 *  3. Toggle icon sync for the <details> element.
 */
(function ($) {
    'use strict';

    $(function () {

        /* ------------------------------------------------------------------
           1. Activation notice dismiss
           ------------------------------------------------------------------ */
        $(document).on('click', '#vserpb-activation-banner .notice-dismiss, #vserpb-activation-banner .vserpb-dismiss-btn', function () {
            var nonce = $(this).data('nonce') ||
                        $('#vserpb-activation-banner .vserpb-dismiss-btn').data('nonce') ||
                        (vserpb_ux && vserpb_ux.dismiss_nonce);

            if (!nonce) { return; }

            $.post(vserpb_ajax_object.ajax_url, {
                action: 'vserpb_dismiss_activation_notice',
                nonce:  nonce
            }).done(function () {
        // Reload after successful dismiss
        location.reload();
    });
            // The native WP .notice-dismiss handler hides the element already.
        });

        /* ------------------------------------------------------------------
           2. Copy JSON to clipboard
           ------------------------------------------------------------------ */
        $(document).on('click', '.vserpb-copy-schema', function () {
            var $btn    = $(this);
            var target  = $btn.data('target');
            var content = $('#' + target).text();

            if (!content) { return; }

            if (navigator.clipboard && window.isSecureContext) {
                navigator.clipboard.writeText(content).then(function () {
                    vserpb_flash_copied($btn);
                });
            } else {
                // Fallback for non-secure contexts.
                var $tmp = $('<textarea>').val(content).appendTo('body').select();
                document.execCommand('copy');
                $tmp.remove();
                vserpb_flash_copied($btn);
            }
        });

        function vserpb_flash_copied($btn) {
            var original = $btn.text();
            $btn.text(vserpb_ux.copied).addClass('copied');
            setTimeout(function () {
                $btn.text('📋 ' + vserpb_ux.copy).removeClass('copied');
            }, 2000);
        }

        /* ------------------------------------------------------------------
           3. Keep the toggle icon in sync with the <details> open state.
           ------------------------------------------------------------------ */
        $(document).on('toggle', '.vserpb-schema-details', function () {
            var $icon = $(this).find('.vserpb-toggle-icon');
            // CSS already handles rotation via :not([open]), but update ARIA.
            var isOpen = this.open;
            $icon.attr('aria-expanded', isOpen ? 'true' : 'false');
        });

    });

}(jQuery));
