( function () {
	'use strict';

	document.addEventListener( 'click', function ( event ) {
		const button = event.target.closest( '.plugoza-ett-copy-shortcode' );

		if ( ! button ) {
			return;
		}

		const input = document.getElementById( button.getAttribute( 'data-copy-target' ) );

		if ( ! input || ! navigator.clipboard ) {
			return;
		}

		navigator.clipboard.writeText( input.value ).then( function () {
			const originalText = button.textContent;
			button.textContent = plugozaEttAdmin.copied;
			window.setTimeout( function () {
				button.textContent = originalText;
			}, 1500 );
		} );
	} );

	document.addEventListener( 'change', function ( event ) {
		if ( event.target.matches( '.plugoza-ett-event-filter, .plugoza-ett-booking-toggle' ) ) {
			event.target.form.submit();
		}
	} );

	document.addEventListener( 'input', function ( event ) {
		if ( ! event.target.matches( '.plugoza-ett-order-search' ) ) {
			return;
		}

		window.clearTimeout( window.plugozaEttOrderSearchTimer );
		window.plugozaEttOrderSearchTimer = window.setTimeout( function () {
			if ( 0 === event.target.value.length || event.target.value.length >= 3 ) {
				event.target.form.submit();
			}
		}, 450 );
	} );

	document.addEventListener( 'DOMContentLoaded', function () {
		const description = document.getElementById( 'plugoza-ett-description' );
		const eventName   = document.getElementById( 'plugoza_ett_name' );

		if ( description && eventName && description.closest( 'p' ) ) {
			eventName.parentNode.appendChild( description.closest( 'p' ) );
		}

		document.querySelectorAll( 'input[name=action][value=plugoza_ett_save_ticket]' ).forEach( function ( input ) {
			input.closest( 'form' ).classList.add( 'plugoza-ett-ticket-add-form' );
		} );

		document.querySelectorAll( 'input[name=action][value=plugoza_ett_update_ticket]' ).forEach( function ( input ) {
			input.closest( 'form' ).classList.add( 'plugoza-ett-ticket-edit-form' );
		} );

		document.querySelectorAll( '.plugoza-ett-ticket-add-form input[name=price]' ).forEach( function ( input ) {
			const label = input.closest( 'label' );

			if ( ! label || label.querySelector( '.plugoza-ett-price-control' ) ) {
				return;
			}

			const title   = document.createElement( 'span' );
			const control = document.createElement( 'span' );
			const unit    = document.createElement( 'span' );
			const nodes   = Array.prototype.slice.call( label.childNodes );
			let currency  = '';

			nodes.forEach( function ( node ) {
				if ( 3 === node.nodeType && node.textContent.trim() ) {
					currency = node.textContent.trim();
				}
			} );

			title.className = 'plugoza-ett-field-label';
			title.textContent = nodes[ 0 ] && nodes[ 0 ].textContent.trim() ? nodes[ 0 ].textContent.trim() : '';
			control.className = 'plugoza-ett-price-control';
			unit.textContent = currency;
			label.textContent = '';
			control.appendChild( input );
			control.appendChild( unit );
			label.appendChild( title );
			label.appendChild( control );
		} );
	} );
}() );
