jQuery(document).ready(function ($) {
    // Attach click event to the dismiss button
    $(document).on('click', '.notice[data-notice="get-start"] button.notice-dismiss', function () {
        // Dismiss the notice via AJAX
        $.ajax({
            type: 'POST',
            url: ajaxurl,
            data: {
                action: 'jewelry_market_dismissed_notice',
            },
            success: function () {
                // Remove the notice on success
                $('.notice[data-notice="get-start"]').remove();
            }
        });
    });
});

// Plugin – AI Content Writer plugin activation
document.addEventListener('DOMContentLoaded', function () {
    const jewelry_market_button = document.getElementById('install-activate-button');
    const jewelry_market_config = window.installPluginData || window.pluginInstallerData;

    if (!jewelry_market_button || !jewelry_market_config) return;

    jewelry_market_button.addEventListener('click', function (e) {
        e.preventDefault();

        const jewelry_market_redirectUrl = jewelry_market_button.getAttribute('data-redirect') || jewelry_market_config.redirectUrl;

        jewelry_market_button.textContent = 'Setting up plugins & demo...';

        const jewelry_market_installData = new FormData();
        jewelry_market_installData.append('action', 'install_and_activate_wordclever_plugin');
        jewelry_market_installData.append('nonce', jewelry_market_config.nonce);

        fetch(jewelry_market_config.ajaxurl, {
            method: 'POST',
            body: jewelry_market_installData,
        })
        .then(res => res.json())
        .then(res => {
            if (res.success) {
                window.location.href = jewelry_market_redirectUrl;
            } else {
                alert('Activation error: ' + (res.data?.message || 'Unknown error'));
                jewelry_market_button.textContent = 'Try Again';
            }
        })
        .catch(error => {
            alert('Request failed: ' + error.message);
            jewelry_market_button.textContent = 'Try Again';
        });
    });
});