/**
 * JavaScript for handling uninstall settings in the Mementor Text to Speech plugin.
 *
 * @package    Mementor_TTS
 * @subpackage Mementor_TTS/admin/js
 */

(function($) {
    'use strict';

    $(document).ready(function() {
        // Check if we're on the advanced settings page
        if ($('.mementor-tts-advanced-card').length === 0) {
            return;
        }

        // Find the delete data toggle
        const $deleteDataToggle = $('#mementor_tts_delete_data');
        const $deleteDataContainer = $deleteDataToggle.closest('.mementor-tts-toggle');

        // Create the uninstall options UI using localized strings
        const uninstallOptionsHTML = `
            <div class="mementor-tts-uninstall-options">
                <label class="mementor-tts-setting-label">${mementorTTSUninstall.uninstallBehavior}</label>
                
                <div class="mementor-tts-radio-group">
                    <div class="mementor-tts-radio-option">
                        <input type="radio" id="uninstall_mode_plugin" name="mementor_tts_uninstall_mode" value="plugin">
                        <label for="uninstall_mode_plugin">
                            <strong>${mementorTTSUninstall.keepEverything}</strong>
                            <span class="description">${mementorTTSUninstall.keepEverythingDesc}</span>
                        </label>
                    </div>
                    
                    <div class="mementor-tts-radio-option">
                        <input type="radio" id="uninstall_mode_plugin_settings" name="mementor_tts_uninstall_mode" value="plugin+settings">
                        <label for="uninstall_mode_plugin_settings">
                            <strong>${mementorTTSUninstall.removeSettings}</strong>
                            <span class="description">${mementorTTSUninstall.removeSettingsDesc}</span>
                        </label>
                    </div>
                    
                    <div class="mementor-tts-radio-option">
                        <input type="radio" id="uninstall_mode_plugin_settings_data" name="mementor_tts_uninstall_mode" value="plugin+settings+data">
                        <label for="uninstall_mode_plugin_settings_data">
                            <strong>${mementorTTSUninstall.completeRemoval}</strong>
                            <span class="description">${mementorTTSUninstall.completeRemovalDesc}</span>
                        </label>
                    </div>
                </div>
                <p class="description mementor-tts-warning">${mementorTTSUninstall.warningText}</p>
            </div>
        `;

        // Replace the toggle with the uninstall options
        $deleteDataContainer.replaceWith(uninstallOptionsHTML);

        // Set the default value based on the delete_data option
        if ($deleteDataToggle.is(':checked')) {
            $('#uninstall_mode_plugin_settings_data').prop('checked', true);
        } else {
            $('#uninstall_mode_plugin_settings').prop('checked', true);
        }

        // Add the uninstall mode to the form submission
        const originalFormSubmit = $('#mementor-tts-advanced-settings-form').submit;
        $('#mementor-tts-advanced-settings-form').submit(function(e) {
            // Add a hidden field for backward compatibility
            const uninstallMode = $('input[name="mementor_tts_uninstall_mode"]:checked').val();
            const deleteData = uninstallMode === 'plugin+settings+data' ? '1' : '0';
            
            $(this).append(`<input type="hidden" name="mementor_tts_delete_data" value="${deleteData}">`);
            
            // Add the uninstall mode
            $(this).append(`<input type="hidden" name="mementor_tts_uninstall_settings[uninstall_mode]" value="${uninstallMode}">`);
            
            // Continue with the original submit
            return originalFormSubmit.apply(this, arguments);
        });

        // Update the AJAX form data
        $(document).on('mementor_tts_before_ajax_submit', function(e, formData) {
            const uninstallMode = $('input[name="mementor_tts_uninstall_mode"]:checked').val();
            formData.uninstall_mode = uninstallMode;
            formData.delete_data = uninstallMode === 'plugin+settings+data' ? 1 : 0;
        });
    });

})(jQuery); 