/* global tncfb3dLitePlugins */
/**
 * Admin behaviours on the Plugins screen (enqueued, not inline):
 *  - "Deactivate Lite" button in the dual-activation notice (Lite + Pro active).
 *  - Deactivation-feedback modal shown when deactivating this plugin.
 * Dynamic values (nonces, URLs, strings) come from the localized
 * `tncfb3dLitePlugins` object.
 */
function tncfb3dLitePluginsInit() {
	var cfg = ( typeof tncfb3dLitePlugins !== 'undefined' ) ? tncfb3dLitePlugins : {};

	// ── Dual-activation notice: one-click "Deactivate Lite" ──────────────────
	var deactivateBtn = document.getElementById( 'tncfb-deactivate-lite' );
	if ( deactivateBtn ) {
		deactivateBtn.addEventListener( 'click', function ( e ) {
			e.preventDefault();
			deactivateBtn.textContent = cfg.i18n.deactivating;
			deactivateBtn.classList.add( 'disabled' );
			fetch( cfg.ajaxUrl + '?action=tncfb3d_deactivate_lite&_ajax_nonce=' + encodeURIComponent( cfg.deactivateNonce ), { method: 'POST' } )
				.then( function ( r ) { return r.json(); } )
				.then( function ( data ) {
					if ( data.success ) {
						var notice = document.getElementById( 'tncfb-dual-notice' );
						if ( notice ) { notice.remove(); }
					} else {
						deactivateBtn.textContent = cfg.i18n.deactivateError;
					}
				} );
		} );
	}

	// ── Deactivation feedback modal ──────────────────────────────────────────
	var modal = document.getElementById( 'tncfb3d-deactivate-modal' );
	if ( ! modal ) { return; }
	var deactivateUrl = '';
	var link = document.querySelector( 'tr[data-plugin="' + cfg.pluginFile + '"] .deactivate a' );
	if ( ! link ) { return; }

	link.addEventListener( 'click', function ( e ) {
		e.preventDefault();
		deactivateUrl = this.href;
		modal.style.display = 'block';
		document.getElementById( 'tncfb3d-deactivate-skip' ).href = deactivateUrl;
	} );

	document.querySelectorAll( 'input[name="tncfb3d_reason"]' ).forEach( function ( r ) {
		r.addEventListener( 'change', function () {
			document.getElementById( 'tncfb3d-deactivate-details' ).style.display = 'block';
			var btn = document.getElementById( 'tncfb3d-deactivate-submit' );
			btn.disabled = false;
			btn.style.opacity = '1';
		} );
	} );

	document.getElementById( 'tncfb3d-deactivate-contact' ).addEventListener( 'change', function () {
		document.getElementById( 'tncfb3d-deactivate-email' ).style.display = this.checked ? 'block' : 'none';
	} );

	document.getElementById( 'tncfb3d-deactivate-cancel' ).addEventListener( 'click', function () {
		modal.style.display = 'none';
	} );

	document.getElementById( 'tncfb3d-deactivate-submit' ).addEventListener( 'click', function () {
		var btn = this;
		btn.disabled = true;
		btn.textContent = cfg.i18n.submitting;
		var reason = document.querySelector( 'input[name="tncfb3d_reason"]:checked' );
		var details = document.getElementById( 'tncfb3d-deactivate-details' ).value;
		var contact = document.getElementById( 'tncfb3d-deactivate-contact' ).checked;
		var email = contact ? document.getElementById( 'tncfb3d-deactivate-email' ).value : '';
		var data = new FormData();
		data.append( 'action', 'tncfb3d_deactivation_feedback' );
		data.append( 'reason', reason ? reason.value : '' );
		data.append( 'details', details );
		data.append( 'contact', contact ? '1' : '' );
		data.append( 'email', email );
		data.append( 'site', cfg.site );
		data.append( 'version', cfg.version );
		data.append( '_wpnonce', cfg.feedbackNonce );
		fetch( cfg.ajaxUrl, { method: 'POST', body: data } )
			.then( function () { window.location.href = deactivateUrl; } )
			.catch( function () { window.location.href = deactivateUrl; } );
	} );
}

// Run after the DOM is parsed so the notice + modal markup exist regardless of
// where this footer script lands relative to admin_footer-plugins.php output.
if ( document.readyState === 'loading' ) {
	document.addEventListener( 'DOMContentLoaded', tncfb3dLitePluginsInit );
} else {
	tncfb3dLitePluginsInit();
}
