(function () {
	var input = document.getElementById( 'cvs_target_version' );
	if (input) {
		input.addEventListener(
			'change',
			function () {
				var value = (input.value || '').trim();
				if ( ! /^\d+\.\d+(\.\d+)?/.test( value )) {
					return;
				}
				input.classList.remove( 'cvs-input-warn' );
			}
		);

		input.addEventListener(
			'blur',
			function () {
				var value = (input.value || '').trim();
				if (value && ! /^\d+\.\d+(\.\d+)?(-[a-zA-Z0-9.]+)?$/.test( value )) {
					input.classList.add( 'cvs-input-warn' );
				}
			}
		);
	}

	var intro   = document.getElementById( 'cvs-intro' );
	var toggle  = intro ? intro.querySelector( '.cvs-intro-toggle' ) : null;
	var content = document.getElementById( 'cvs-intro-content' );

	if (toggle && content && typeof cvsAdmin !== 'undefined') {
		toggle.addEventListener(
			'click',
			function () {
				var collapsed = ! content.hasAttribute( 'hidden' );
				if (collapsed) {
					content.setAttribute( 'hidden', 'hidden' );
					intro.classList.add( 'is-collapsed' );
					toggle.setAttribute( 'aria-expanded', 'false' );
					toggle.textContent = toggle.getAttribute( 'data-label-expand' ) || 'Show';
				} else {
					content.removeAttribute( 'hidden' );
					intro.classList.remove( 'is-collapsed' );
					toggle.setAttribute( 'aria-expanded', 'true' );
					toggle.textContent = toggle.getAttribute( 'data-label-collapse' ) || 'Collapse';
				}

				var body = new FormData();
				body.append( 'action', 'cvs_save_intro_collapsed' );
				body.append( 'nonce', cvsAdmin.nonce );
				body.append( 'collapsed', collapsed ? '1' : '0' );
				fetch( cvsAdmin.ajaxUrl, { method: 'POST', credentials: 'same-origin', body: body } );
			}
		);
	}
})();
