/**
 * Script for the tools page.
 *
 * @package northmill-online-shared
 */

/* global northmill_online_shared_admin_tools */
jQuery( document ).ready(
	function () {
		new NMS_Tools();
	}
);


class NMS_Tools {
	constructor() {
		jQuery( '.nm-uninstall-data-deletion' ).on( 'click', this.toggle_uninstall_data_deletion );
	}

	/**
	 * Toggle whether to remove data on plugin uninstallation.
	 */
	toggle_uninstall_data_deletion() {
		const $btn   = jQuery( this );
		const enable = Boolean( $btn.data( 'enable' ) );

		if (
			! confirm(
				enable ?
					northmill_online_shared_admin_tools.l10n.confirm_enable_uninstall_data_deletion :
					northmill_online_shared_admin_tools.l10n.confirm_disable_uninstall_data_deletion
			)
		) {
			return;
		}

		NMS_AJAX.post(
			northmill_online_shared_admin_tools.uninstall_data_deletion_ajax,
			{ enabled: enable },
			NMS_AJAX.success_reload,
			NMS_AJAX.fail_alert
		);
	}
}
