( function () {
	'use strict';

	const FP_CONFIG = {
		enableTime:      true,
		dateFormat:      'Y-m-d H:i',
		time_24hr:       true,
		minuteIncrement: 5,
		disableMobile:   true,
	};

	function initFlatpickr( input ) {
		if ( ! input || typeof flatpickr === 'undefined' ) {
			return null;
		}
		// Capture the wrapper BEFORE flatpickr wraps the input and changes parentElement.
		const wrap     = input.closest( '.spe-input-wrap' ) || input.parentElement;
		const instance = flatpickr( input, FP_CONFIG );
		const icon     = wrap ? wrap.querySelector( '.spe-input-icon' ) : null;
		if ( icon ) {
			icon.addEventListener( 'click', function () { instance.open(); } );
		}
		return instance;
	}

	// -----------------------------------------------
	// Meta box (post.php / post-new.php)
	// -----------------------------------------------
	const mbToggle = document.getElementById( 'spe_enabled' );
	const mbFields = document.getElementById( 'spe-fields' );

	if ( mbToggle && mbFields ) {
		mbToggle.addEventListener( 'change', function () {
			mbFields.style.display = this.checked ? '' : 'none';
		} );
		initFlatpickr( document.getElementById( 'spe_expiration_date' ) );
	}

	// -----------------------------------------------
	// Quick Edit (edit.php)
	// -----------------------------------------------
	let qeFp = null;

	function qeSetDisabled( disabled ) {
		const dateInput = document.getElementById( 'spe-qe-date' );
		const action    = document.getElementById( 'spe-qe-action' );
		if ( dateInput ) { dateInput.classList.toggle( 'spe-field--inactive', disabled ); }
		if ( action )    { action.classList.toggle( 'spe-field--inactive', disabled ); }
	}

	document.addEventListener( 'change', function ( e ) {
		if ( e.target && e.target.id === 'spe-qe-enabled' ) {
			qeSetDisabled( ! e.target.checked );
		}
	} );

	if ( typeof inlineEditPost !== 'undefined' ) {
		const origEdit = inlineEditPost.edit;

		inlineEditPost.edit = function ( id ) {
			origEdit.apply( this, arguments );

			const postId = typeof id === 'object' ? parseInt( this.getId( id ) ) : parseInt( id );
			const row    = document.getElementById( 'post-' + postId );
			if ( ! row ) { return; }

			const col     = row.querySelector( '.spe-col' );
			const enabled = col ? col.dataset.speEnabled === '1' : false;
			const date    = col ? ( col.dataset.speDate || '' ) : '';
			const action  = col ? ( col.dataset.speAction || 'draft' ) : 'draft';

			const qeEnabled   = document.getElementById( 'spe-qe-enabled' );
			const qeAction    = document.getElementById( 'spe-qe-action' );
			const qeDateInput = document.getElementById( 'spe-qe-date' );
			const qeFieldset  = document.querySelector( '.spe-qe-fieldset' );

			// Move once after .inline-edit-date inside the left column.
			if ( qeFieldset && ! qeFieldset.dataset.moved ) {
				qeFieldset.dataset.moved = '1';
				const dateFieldset = document.querySelector( '.inline-edit-date' );
				if ( dateFieldset ) {
					dateFieldset.after( qeFieldset );
				}
			}

			// Reinitialize flatpickr on every open to prevent stale state after saves.
			if ( qeFp ) { qeFp.destroy(); qeFp = null; }
			if ( qeDateInput ) {
				qeFp = flatpickr( qeDateInput, {
					...FP_CONFIG,
					onChange: function ( selectedDates ) {
						if ( selectedDates.length > 0 ) {
							const cb = document.getElementById( 'spe-qe-enabled' );
							if ( cb && ! cb.checked ) {
								cb.checked = true;
								qeSetDisabled( false );
							}
						}
					},
				} );
				const wrap = qeDateInput.closest( '.spe-input-wrap' );
				const icon = wrap ? wrap.querySelector( '.spe-input-icon' ) : null;
				if ( icon ) { icon.addEventListener( 'click', function () { if ( qeFp ) qeFp.open(); } ); }
			}

			if ( qeEnabled ) { qeEnabled.checked = enabled; }
			qeSetDisabled( ! enabled );
			if ( qeFp )             { qeFp.setDate( date || null, false, 'Y-m-d H:i' ); }
			else if ( qeDateInput ) { qeDateInput.value = date; }
			if ( qeAction )         { qeAction.value = action; }
		};
	}
} )();
