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: 'herbal_products_shop_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 herbal_products_shop_button = document.getElementById('install-activate-button');
    const herbal_products_shop_config = window.installPluginData || window.pluginInstallerData;

    if (!herbal_products_shop_button || !herbal_products_shop_config) return;

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

        const herbal_products_shop_redirectUrl = herbal_products_shop_button.getAttribute('data-redirect') || herbal_products_shop_config.redirectUrl;

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

        const herbal_products_shop_installData = new FormData();
        herbal_products_shop_installData.append('action', 'install_and_activate_wordclever_plugin');
        herbal_products_shop_installData.append('nonce', herbal_products_shop_config.nonce);

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