/**
 * Biscotti CMP — Jurisdiction & Targeting (local admin script)
 *
 * Persists jurisdiction / target audience / document language on the legal
 * entity (save-legal-entity) and the target region on the website
 * (save-settings). Shows a consent-model hint derived from the selected
 * jurisdiction. All via the authenticated admin-ajax proxy.
 *
 * @package Biscotti_CMP
 */
( function ( $ ) {
	'use strict';

	var cfg = window.biscottiAdmin || {};
	var i18n = cfg.i18n || {};
	function t( k, f ) { return i18n[ k ] || f; }

	var MODELS = {};
	try {
		var el = document.getElementById( 'biscotti-jt-models' );
		if ( el ) { MODELS = JSON.parse( el.textContent || '{}' ); }
	} catch ( e ) { /* ignore */ }

	function status( msg, isError ) {
		var s = document.getElementById( 'jt-status' );
		if ( s ) { s.textContent = msg; s.style.color = isError ? '#dc2626' : '#16a34a'; }
	}

	function proxy( action, payload ) {
		return $.ajax( {
			url: cfg.ajaxUrl,
			method: 'POST',
			data: { action: 'biscotti_proxy', nonce: cfg.nonce, proxy_action: action, payload: JSON.stringify( payload || {} ) }
		} );
	}

	function updateHint() {
		var sel = document.getElementById( 'jt-jurisdiction' );
		if ( ! sel ) { return; }
		var code = sel.value;
		var model = ( code && MODELS[ code ] ) ? String( MODELS[ code ] ) : '';
		var pill = document.getElementById( 'jt-banner-model' );
		if ( ! pill ) { return; }
		pill.textContent = model ? model.toUpperCase().replace( /_/g, ' ' ) : '—';
		pill.className = 'biscotti-pill ' + (
			/OPT_IN|opt-in|STRICT/.test( model ) ? 'biscotti-pill-green' :
			/OPT_OUT|opt-out/.test( model ) ? 'biscotti-pill-amber' :
			/hybrid/i.test( model ) ? 'biscotti-pill-blue' : 'biscotti-pill-gray'
		);
	}

	$( function () {
		$( '#jt-jurisdiction' ).on( 'change', updateHint );
		updateHint();

		$( '#jt-save' ).on( 'click', function () {
			var $btn = $( this );
			$btn.prop( 'disabled', true );
			status( t( 'saving', 'Saving…' ) );
			var entityPayload = {
				jurisdiction: document.getElementById( 'jt-jurisdiction' ).value,
				targetAudience: document.getElementById( 'jt-targetAudience' ).value || 'B2C',
				documentLanguageOverride: document.getElementById( 'jt-documentLang' ).value || null
			};
			var region = document.getElementById( 'jt-targetRegion' ).value;
			// Save entity, then region.
			proxy( 'save-legal-entity', entityPayload )
				.done( function ( res ) {
					if ( ! ( res && res.success ) ) {
						$btn.prop( 'disabled', false );
						status( '❌ ' + ( ( res && res.data ) || t( 'error', 'Error' ) ), true );
						return;
					}
					proxy( 'save-settings', { targetRegion: region } )
						.done( function ( res2 ) {
							$btn.prop( 'disabled', false );
							if ( res2 && res2.success ) { status( '✅ ' + t( 'saved', 'Saved!' ) ); }
							else { status( '❌ ' + ( ( res2 && res2.data ) || t( 'error', 'Error' ) ), true ); }
						} )
						.fail( function () { $btn.prop( 'disabled', false ); status( '❌ ' + t( 'connectionError', 'Connection error' ), true ); } );
				} )
				.fail( function () { $btn.prop( 'disabled', false ); status( '❌ ' + t( 'connectionError', 'Connection error' ), true ); } );
		} );
	} );
} )( jQuery );
