(function ($) {
    "use strict";

    $(document).on('click', '.st-tranlation-agency-notice .button-primary', function (e) {

        if ('install-activate' === $(this).data('action') && !$(this).hasClass('init')) {
            var $self = $(this),
                $href = $self.attr('href');

            // Ensure the redirect URL is correct
            if ('true' === $self.data('freemius')) {
                $href = $href.replace('st-tranlation-agency', 'st-demo-importer');
            }

            $self.addClass('init');
            $self.html('Installing ST Demo Importer <span class="st-tranlation-agency-dot-flashing"></span>');

            var stDemoImporterData = {
                'action': 'sttranlationagency_install_activate_st_demo_importer',
                'nonce': st_tranlation_agency_localize.st_demo_importer_nonce
            };

            // Send AJAX request to install and activate ST Demo Importer
            $.post(st_tranlation_agency_localize.ajax_url, stDemoImporterData, function (response) {
                if (response.success) {
                    console.log('ST Demo Importer installed');

                    setTimeout(function () {
                        $self.html('Redirecting to ST Demo Importer <span class="st-tranlation-agency-dot-flashing"></span>');

                        setTimeout(function () {
                            window.location.href = $href; // Redirect after successful installation
                        }, 1000);
                    }, 500);
                } else {
                    $self.html('Installation Failed');
                    console.error('ST Demo Importer installation failed:', response);
                }
            }).fail(function () {
                $self.html('Installation Failed');
                console.error('AJAX request failed');
            });

            e.preventDefault();
        }
    });

})(jQuery);
