( function () {
	'use strict';

	var body = document.body;
	var root = document.documentElement;
	var header = document.querySelector( '.calm-header' );

	if ( ! body || ! header || ! root ) {
		return;
	}

	function getHeaderOffset() {
		var rect = header.getBoundingClientRect();
		var computed = window.getComputedStyle ? window.getComputedStyle( header ) : null;
		var marginTop = computed ? parseFloat( computed.marginTop ) || 0 : 0;
		var marginBottom = computed ? parseFloat( computed.marginBottom ) || 0 : 0;

		return rect.height + marginTop + marginBottom;
	}

	function syncHeaderOffset() {
		root.style.setProperty( '--calm-header-offset', getHeaderOffset().toFixed( 2 ) + 'px' );
	}

	var offsetSyncQueued = false;
	function queueHeaderOffsetSync() {
		if ( offsetSyncQueued ) {
			return;
		}

		offsetSyncQueued = true;
		window.requestAnimationFrame( function () {
			offsetSyncQueued = false;
			syncHeaderOffset();
		} );
	}

	function getMenuStyleSourceElement() {
		var selector = '.wp-block-navigation .wp-block-navigation-item__content';
		return header.querySelector( selector ) || document.querySelector( selector );
	}

	function getMenuActiveStyleSourceElement( fallback ) {
		var selector = '.wp-block-navigation .wp-block-navigation-item.current-menu-item > .wp-block-navigation-item__content, .wp-block-navigation .wp-block-navigation-item.current_page_item > .wp-block-navigation-item__content, .wp-block-navigation .wp-block-navigation-item.current-menu-ancestor > .wp-block-navigation-item__content';
		return header.querySelector( selector ) || fallback;
	}

	function syncLanguageSwitcherMenuStyle() {
		if ( ! window.getComputedStyle ) {
			return;
		}

		var menuSource = getMenuStyleSourceElement();
		if ( ! menuSource ) {
			return;
		}

		var menuComputed = window.getComputedStyle( menuSource );
		var activeSource = getMenuActiveStyleSourceElement( menuSource );
		var activeComputed = window.getComputedStyle( activeSource );
		var switchers = header.querySelectorAll( '.wp-block-wuppi-language-switcher.is-dropdown-style-menu-link' );

		switchers.forEach( function ( switcher ) {
			switcher.style.setProperty( '--calm-lang-menu-font-size', menuComputed.fontSize );
			switcher.style.setProperty( '--calm-lang-menu-font-family', menuComputed.fontFamily );
			switcher.style.setProperty( '--calm-lang-menu-font-weight', menuComputed.fontWeight );
			switcher.style.setProperty( '--calm-lang-menu-font-style', menuComputed.fontStyle );
			switcher.style.setProperty( '--calm-lang-menu-line-height', menuComputed.lineHeight );
			switcher.style.setProperty( '--calm-lang-menu-letter-spacing', menuComputed.letterSpacing );
			switcher.style.setProperty( '--calm-lang-menu-text-transform', menuComputed.textTransform );
			switcher.style.setProperty( '--calm-lang-menu-color', menuComputed.color );
			switcher.style.setProperty( '--calm-lang-menu-active-color', activeComputed.color );
		} );
	}

	var syncQueued = false;
	function queueLanguageSwitcherStyleSync() {
		if ( syncQueued ) {
			return;
		}

		syncQueued = true;
		window.requestAnimationFrame( function () {
			syncQueued = false;
			syncLanguageSwitcherMenuStyle();
		} );
	}

	queueHeaderOffsetSync();
	queueLanguageSwitcherStyleSync();
	window.addEventListener( 'load', queueHeaderOffsetSync );
	window.addEventListener( 'resize', queueHeaderOffsetSync );
	window.setTimeout( queueLanguageSwitcherStyleSync, 250 );
	window.setTimeout( queueHeaderOffsetSync, 250 );
	window.addEventListener( 'resize', queueLanguageSwitcherStyleSync );

	function observeLanguageSwitcherStyleSources() {
		if ( 'undefined' === typeof MutationObserver ) {
			return;
		}

		var observer = new MutationObserver( function () {
			queueHeaderOffsetSync();
			queueLanguageSwitcherStyleSync();
		} );

		observer.observe(
			header,
			{
				childList: true,
				subtree: true,
				attributes: true,
				attributeFilter: [ 'class', 'style' ]
			}
		);

		queueHeaderOffsetSync();

		if ( document.head ) {
			observer.observe(
				document.head,
				{
					childList: true,
					subtree: true,
					characterData: true
				}
			);
		}
	}

	observeLanguageSwitcherStyleSources();

	if ( 'undefined' !== typeof ResizeObserver ) {
		var resizeObserver = new ResizeObserver( function () {
			queueHeaderOffsetSync();
		} );

		resizeObserver.observe( header );
	}

	var isFixedGlass = body.classList.contains( 'calm-header-mode-fixed-glass' );
	var isAutoHide = body.classList.contains( 'calm-header-mode-fixed-auto-hide' );

	if ( ! isFixedGlass && ! isAutoHide ) {
		return;
	}

	var desktopQuery = window.matchMedia( '(min-width: 782px)' );
	var headerSettings = window.wuppiHeaderSettings || {};
	var autoHideDelay = parseInt( headerSettings.autoHideDelay, 10 );
	var hideTimer = 0;
	var ticking = false;

	if ( ! autoHideDelay || autoHideDelay < 1 ) {
		autoHideDelay = 900;
	}

	function clearHideTimer() {
		if ( hideTimer ) {
			window.clearTimeout( hideTimer );
			hideTimer = 0;
		}
	}

	function resetState() {
		clearHideTimer();
		header.classList.remove( 'is-scrolled', 'is-revealed' );
	}

	function updateHeaderState() {
		ticking = false;

		if ( ! desktopQuery.matches ) {
			resetState();
			return;
		}

		var scrollY = window.scrollY || window.pageYOffset || 0;
		var hasScrolled = scrollY > 0;

		header.classList.toggle( 'is-scrolled', hasScrolled );

		if ( ! isAutoHide ) {
			return;
		}

		if ( ! hasScrolled ) {
			clearHideTimer();
			header.classList.remove( 'is-revealed' );
			return;
		}

		header.classList.add( 'is-revealed' );
		clearHideTimer();
		hideTimer = window.setTimeout( function () {
			header.classList.remove( 'is-revealed' );
		}, autoHideDelay );
	}

	function onScrollOrResize() {
		if ( ticking ) {
			return;
		}

		ticking = true;
		window.requestAnimationFrame( updateHeaderState );
	}

	header.addEventListener( 'focusin', function () {
		if ( ! isAutoHide || ! desktopQuery.matches || window.scrollY <= 0 ) {
			return;
		}

		header.classList.add( 'is-revealed' );
		clearHideTimer();
	} );

	window.addEventListener( 'scroll', onScrollOrResize, { passive: true } );
	window.addEventListener( 'resize', onScrollOrResize );

	if ( desktopQuery.addEventListener ) {
		desktopQuery.addEventListener( 'change', onScrollOrResize );
	} else if ( desktopQuery.addListener ) {
		desktopQuery.addListener( onScrollOrResize );
	}

	updateHeaderState();
}() );
