/**
 * Theme Name: errigal
 * Theme URI: https://projecterrigal.com/themes/errigal/
 * Author: Project errigal
 * Author URI: https://projecterrigal.com/
 * Version: 1.2.1
 */
/* eslint-disable no-unused-expressions */
/* eslint-disable eqeqeq */
/* eslint-disable no-mixed-operators */
/* eslint-disable no-nested-ternary */
( function() {
	'use strict';

	var CLASS_SHARED_COLLAPSED = 'collapsed';
	var CLASS_NAV_BREADCRUMB_LINK = 'errigal-breadcrumb__link';
	var CLASS_NAV_BREADCRUMB_LIST_ITEM_ANCESTOR = 'errigal-breadcrumb__ancestor';
	var SELECTOR_NAV_BREADCRUMB_ANCESTOR_LINK = '.'.concat( CLASS_NAV_BREADCRUMB_LIST_ITEM_ANCESTOR, ' > .' ).concat( CLASS_NAV_BREADCRUMB_LINK );
	var NULL_ELEMENT = null;

	function isFunction( value ) {
		return value instanceof Function;
	}

	var INDEX_FIRST = 0;

	function isHTMLCollection( elements ) {
		return elements instanceof HTMLCollection;
	}

	function isObjectType( value, expectedType ) {
		return Object.prototype.toString.call( value ) === '[object '.concat( expectedType, ']' );
	}

	function isNumber( value ) {
		return ! isNaN( value ) && isObjectType( value, 'Number' ) && isFinite( value );
	}

	function isPositiveNumber( value ) {
		return isNumber( value ) && value > 0;
	}

	function isIterable( items ) {
		return !! items && isPositiveNumber( items.length );
	}

	function isTrue( value ) {
		return value === true;
	}

	function forEach( items, callback ) {
		if ( ! isIterable( items ) || ! isFunction( callback ) ) {
			return false;
		}

		if ( isHTMLCollection( items ) ) {
			items = Array.prototype.slice.call( items );
		}

		for ( var i = INDEX_FIRST; i < items.length; i++ ) {
			if ( isTrue( callback( items[ i ], i, items ) ) ) {
				return true;
			}
		}

		return false;
	}

	function hasClassList( element ) {
		return !! element && element.classList instanceof DOMTokenList && isFunction( element.classList.contains );
	}

	function isHTMLElement( element ) {
		if ( ! ( element instanceof HTMLElement ) ) {
			return false;
		}

		for ( var _len = arguments.length, classNames = new Array( _len > 1 ? _len - 1 : 0 ), _key = 1; _key < _len; _key++ ) {
			classNames[ _key - 1 ] = arguments[ _key ];
		}

		return hasClassList( element ) && ! forEach( classNames, function( className ) {
			return ! element.classList.contains( className );
		} );
	}

	var EMPTY_STRING = '';

	function isString( value ) {
		return isObjectType( value, 'String' );
	}

	function isNonEmptyString( value ) {
		return isString( value ) && isFunction( value.trim ) && value.trim() !== EMPTY_STRING;
	}

	function isDocument( element ) {
		return element instanceof Document;
	}

	function isisHTMLElementOrDocument( element ) {
		return isHTMLElement( element ) || isDocument( element );
	}

	function callElementMethod( methodName, nameOrSelectors, rootElement ) {
		return isNonEmptyString( methodName ) && isNonEmptyString( nameOrSelectors ) && isisHTMLElementOrDocument( rootElement ) && isFunction( rootElement[ methodName ] ) ? rootElement[ methodName ]( nameOrSelectors ) : NULL_ELEMENT;
	}

	function querySelector( selectors ) {
		var rootElement = arguments.length > 1 && arguments[ 1 ] !== undefined ? arguments[ 1 ] : document;

		return callElementMethod( 'querySelector', selectors, rootElement );
	}

	function getFirstBreadcrumbAncestorLink() {
		return querySelector( SELECTOR_NAV_BREADCRUMB_ANCESTOR_LINK );
	}

	var CLASS_NAV_BREADCRUMB_LIST = 'errigal-breadcrumb__list';
	var NULL_VALUE = null;

	function getFirstItem( items ) {
		return isIterable( items ) ? items[ INDEX_FIRST ] : NULL_VALUE;
	}

	function getElementsByClassName( className ) {
		var rootElement = arguments.length > 1 && arguments[ 1 ] !== undefined ? arguments[ 1 ] : document;
		var ELEMENTS = callElementMethod( 'getElementsByClassName', className, rootElement );

		return isHTMLCollection( ELEMENTS ) ? ELEMENTS : NULL_ELEMENT;
	}

	function getElementByClassName( className ) {
		var rootElement = arguments.length > 1 && arguments[ 1 ] !== undefined ? arguments[ 1 ] : document;

		return getFirstItem( getElementsByClassName( className, rootElement ) );
	}

	function getBreadcrumbList() {
		return getElementByClassName( CLASS_NAV_BREADCRUMB_LIST );
	}

	var CLASS_NAV_BREADCRUMB_REVEAL_BUTTON = 'errigal-breadcrumb__reveal-button';

	function isBreadcrumbRevealButton( element ) {
		return isHTMLElement( element, CLASS_NAV_BREADCRUMB_REVEAL_BUTTON );
	}

	function removeClass( element, className ) {
		if ( ! isHTMLElement( element ) || ! hasClassList( element ) || ! isFunction( element.classList.remove ) || ! isNonEmptyString( className ) ) {
			return;
		}

		element.classList.remove( className );
	}

	function toLowerCase( value ) {
		return isNonEmptyString( value ) && isFunction( value.toLowerCase ) ? value.toLowerCase() : EMPTY_STRING;
	}

	function areStringsEqualIgnoreCase( str1, str2 ) {
		return toLowerCase( str1 ) === toLowerCase( str2 );
	}

	function isEvent( value ) {
		return value instanceof Event && isFunction( value.preventDefault ) && isFunction( value.stopPropagation );
	}

	function isKey( event, key, keyCode ) {
		return isEvent( event ) && isNonEmptyString( key ) && isNumber( keyCode ) ? areStringsEqualIgnoreCase( event.key, key ) || event.keyCode === keyCode : false;
	}

	function isEnterKey( event ) {
		return isKey( event, 'Enter', 13 );
	}

	var EVENT_CLICK = 'click';

	function isBoolean( value ) {
		return isObjectType( value, 'Boolean' );
	}

	function isObject( value ) {
		return value instanceof Object;
	}

	function isUndefined( value ) {
		return isObjectType( value, 'Undefined' );
	}

	function onDocumentEvent( eventType, callback, options ) {
		if ( ! isNonEmptyString( eventType ) || ! isFunction( callback ) || ! ( isUndefined( options ) || isObject( options ) || isBoolean( options ) ) ) {
			return;
		}

		document.addEventListener( eventType, callback, options || false );
	}

	function onDocumentClick( callback, options ) {
		onDocumentEvent( EVENT_CLICK, callback, options );
	}

	var EVENT_KEYDOWN = 'keydown';

	function onDocumentKeydown( callback, options ) {
		onDocumentEvent( EVENT_KEYDOWN, callback, options );
	}

	var EVENT_LOAD = 'load';

	function onWindowEvent( eventType, callback, options ) {
		if ( ! isNonEmptyString( eventType ) || ! isFunction( callback ) || ! ( isUndefined( options ) || isObject( options ) || isBoolean( options ) ) ) {
			return;
		}

		window.addEventListener( eventType, callback, options || false );
	}

	function onWindowLoad( callback, options ) {
		onWindowEvent( EVENT_LOAD, callback, options );
	}

	var EVENT_RESIZE = 'resize';

	function onWindowResize( callback, options ) {
		onWindowEvent( EVENT_RESIZE, callback, options );
	}

	var CALLBACK_INITIAL_ATTEMPT_COUNT = 0;
	var CALLBACK_INITIAL_INTERVAL = 0;
	var CALLBACK_INTERVAL = 50;
	var CALLBACK_MAX_ATTEMPT_COUNT = 20;

	function focusElement( element ) {
		var attemptCount = arguments.length > 1 && arguments[ 1 ] !== undefined ? arguments[ 1 ] : CALLBACK_INITIAL_ATTEMPT_COUNT;
		var interval = arguments.length > 2 && arguments[ 2 ] !== undefined ? arguments[ 2 ] : CALLBACK_INITIAL_INTERVAL;

		if ( ! isHTMLElement( element ) || ! isFunction( element.focus ) || ! isNumber( attemptCount ) || ! isNumber( interval ) || attemptCount === CALLBACK_MAX_ATTEMPT_COUNT ) {
			return;
		}

		element.focus();
		setTimeout( function() {
			if ( element === document.activeElement ) {
				return;
			}

			focusElement( element, ++attemptCount, CALLBACK_INTERVAL );
		}, interval );
	}

	var CLASS_NAV_BREADCRUMB_LIST_ITEM = 'errigal-breadcrumb__list-item';

	function getBreadcrumbListItems() {
		return getElementsByClassName( CLASS_NAV_BREADCRUMB_LIST_ITEM );
	}

	var RECT_WIDTH = 'width';
	var PIXEL_ZERO = 0;

	function getObjectValue( obj, key ) {
		return isObject( obj ) && key in obj ? obj[ key ] : NULL_VALUE;
	}

	function getRectProp( element, property ) {
		if ( ! isHTMLElement( element ) || ! isFunction( element.getBoundingClientRect ) ) {
			return PIXEL_ZERO;
		}

		var RECT_PROP = getObjectValue( element.getBoundingClientRect(), property );

		return isNumber( RECT_PROP ) ? RECT_PROP : PIXEL_ZERO;
	}

	function getWidth( element ) {
		return getRectProp( element, RECT_WIDTH );
	}

	function addClass( element, className ) {
		if ( ! isHTMLElement( element ) || ! hasClassList( element ) || ! isFunction( element.classList.add ) || ! isNonEmptyString( className ) ) {
			return;
		}

		element.classList.add( className );
	}

	function toggleClass( add, element, className ) {
		if ( ! isBoolean( add ) ) {
			return;
		}

		var TOGGLE_CLASS = isTrue( add ) ? addClass : removeClass;

		TOGGLE_CLASS( element, className );
	}

	function getBreadcrumbAncestorListItems() {
		return getElementsByClassName( CLASS_NAV_BREADCRUMB_LIST_ITEM_ANCESTOR );
	}

	function getBreadcrumbRevealButton() {
		return getElementByClassName( CLASS_NAV_BREADCRUMB_REVEAL_BUTTON );
	}

	var CLASS_NAV_BREADCRUMB_LIST_ITEM_REVEAL = 'errigal-breadcrumb__reveal';

	function getBreadcrumbRevealListItem() {
		return getElementByClassName( CLASS_NAV_BREADCRUMB_LIST_ITEM_REVEAL );
	}

	var ATTRIBUTE_ARIA_EXPANDED = 'aria-expanded';

	function setAttribute( element, name, value ) {
		if ( ! isHTMLElement( element ) || ! isFunction( element.setAttribute ) || ! isNonEmptyString( name ) ) {
			return;
		}

		element.setAttribute( name, value );
	}

	function toggleAriaExpanded( expanded ) {
		if ( ! isBoolean( expanded ) ) {
			return;
		}

		for ( var _len = arguments.length, elements = new Array( _len > 1 ? _len - 1 : 0 ), _key = 1; _key < _len; _key++ ) {
			elements[ _key - 1 ] = arguments[ _key ];
		}

		forEach( elements, function( element ) {
			return setAttribute( element, ATTRIBUTE_ARIA_EXPANDED, isTrue( expanded ) );
		} );
	}

	var ATTRIBUTE_ARIA_HIDDEN = 'aria-hidden';

	function toggleAriaHidden( hidden ) {
		if ( ! isBoolean( hidden ) ) {
			return;
		}

		for ( var _len = arguments.length, elements = new Array( _len > 1 ? _len - 1 : 0 ), _key = 1; _key < _len; _key++ ) {
			elements[ _key - 1 ] = arguments[ _key ];
		}

		forEach( elements, function( element ) {
			return setAttribute( element, ATTRIBUTE_ARIA_HIDDEN, isTrue( hidden ) );
		} );
	}

	function _toConsumableArray( r ) {
		return _arrayWithoutHoles( r ) || _iterableToArray( r ) || _unsupportedIterableToArray( r ) || _nonIterableSpread();
	}

	function _nonIterableSpread() {
		throw new TypeError( 'Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.' );
	}

	function _unsupportedIterableToArray( r, a ) {
		if ( r ) {
			if ( 'string' === typeof r ) {
				return _arrayLikeToArray( r, a );
			} var t = {}.toString.call( r ).slice( 8, -1 ); return 'Object' === t && r.constructor && ( t = r.constructor.name ), 'Map' === t || 'Set' === t ? Array.from( r ) : 'Arguments' === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test( t ) ? _arrayLikeToArray( r, a ) : void 0;
		}
	}

	function _iterableToArray( r ) {
		if ( 'undefined' !== typeof Symbol && null != r[ Symbol.iterator ] || null != r[ '@@iterator' ] ) {
			return Array.from( r );
		}
	}

	function _arrayWithoutHoles( r ) {
		if ( Array.isArray( r ) ) {
			return _arrayLikeToArray( r );
		}
	}

	function _arrayLikeToArray( r, a ) {
		( null == a || a > r.length ) && ( a = r.length ); for ( var e = 0, n = Array( a ); e < a; e++ ) {
			n[ e ] = r[ e ];
		} return n;
	}

	function toggleBreadcrumbRevealAndAncestorAriaAttributes( isAncestorHidden ) {
		toggleAriaExpanded( ! isAncestorHidden, getBreadcrumbRevealButton() );
		toggleAriaHidden( ! isAncestorHidden, getBreadcrumbRevealListItem() );
		toggleAriaHidden.apply( void 0, [ isAncestorHidden ].concat( _toConsumableArray( getBreadcrumbAncestorListItems() ) ) );
	}

	function isBreadcrumbListWrapped() {
		var totalWidth = 0;
		var BREADCRUMB_LIST_WIDTH = getWidth( getBreadcrumbList() );

		return forEach( getBreadcrumbListItems(), function( item ) {
			return ( totalWidth += getWidth( item ) ) >= BREADCRUMB_LIST_WIDTH;
		} );
	}

	function toggleBreadcrumbAncestor() {
		var IS_BREADCRUMB_LIST_WRAPPED = isBreadcrumbListWrapped();

		toggleClass( IS_BREADCRUMB_LIST_WRAPPED, getBreadcrumbList(), CLASS_SHARED_COLLAPSED );
		toggleBreadcrumbRevealAndAncestorAriaAttributes( IS_BREADCRUMB_LIST_WRAPPED );
	}

	function revealBreadcrumbAncestorAndFocusFirstAncestorLink() {
		removeClass( getBreadcrumbList(), CLASS_SHARED_COLLAPSED );
		toggleBreadcrumbRevealAndAncestorAriaAttributes( false );
		focusElement( getFirstBreadcrumbAncestorLink() );
	}

	function addBreadcrumbEvents() {
		onDocumentClick( function( event ) {
			if ( ! isEvent( event ) ) {
				return;
			}

			if ( isBreadcrumbRevealButton( event.target ) ) {
				revealBreadcrumbAncestorAndFocusFirstAncestorLink();
			}
		} );
		onDocumentKeydown( function( event ) {
			if ( ! isEvent( event ) ) {
				return;
			}

			if ( isEnterKey( event ) ) {
				if ( isBreadcrumbRevealButton( event.target ) ) {
					event.preventDefault();
					revealBreadcrumbAncestorAndFocusFirstAncestorLink();
				}
			}
		} );
		onWindowLoad( toggleBreadcrumbAncestor );
		onWindowResize( toggleBreadcrumbAncestor );
	}

	addBreadcrumbEvents();
}() );
