/**
 * Freefactu Extensions Page Script
 *
 * Handles extension activation/deactivation via AJAX.
 *
 * @package Freefactu
 * @since 1.3.0
 */

(function ($) {
    'use strict';

    $(document).ready(function () {
        // Toggle extension activation
        $('.freefactu-extension-btn[data-action]').on('click', function (e) {
            e.preventDefault();

            var $button = $(this);
            var $card = $button.closest('.freefactu-extension-card');
            var extensionId = $button.data('extension-id');
            var action = $button.data('action');

            $button.prop('disabled', true).addClass('is-loading');

            $.ajax({
                url: ajaxurl,
                type: 'POST',
                data: {
                    action: 'freefactu_toggle_extension',
                    extension_id: extensionId,
                    toggle_action: action,
                    nonce: freefactuExtensions.nonce
                },
                success: function (response) {
                    if (response.success) {
                        // Reload the page to reflect changes
                        location.reload();
                    } else {
                        alert(response.data.message || freefactuExtensions.i18n.errorToggle);
                        $button.prop('disabled', false).removeClass('is-loading');
                    }
                },
                error: function () {
                    alert(freefactuExtensions.i18n.errorConnection);
                    $button.prop('disabled', false).removeClass('is-loading');
                }
            });
        });
    });
})(jQuery);
