/**
 * Theme Name: errigal
 * Theme URI: https://projecterrigal.com/themes/errigal/
 * Author: Project errigal
 * Author URI: https://projecterrigal.com/
 * Version: 1.2.1
 */
( function() {
	'use strict';

	const EVENT_READY = 'ready';
	const OBJECT_NAME_WP = 'wp';
	const EMPTY_ARRAY = [];
	const NULL_OBJECT = null;
	const INDEX_FIRST = 0;

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

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

	function isObjectType( value, expectedType ) {
		return Object.prototype.toString.call( value ) ===
			`[object ${ 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 ( let i = INDEX_FIRST; i < items.length; i++ ) {
			if ( isTrue( callback( items[ i ], i, items ) ) ) {
				return true;
			}
		}

		return false;
	}

	const NULL_VALUE = null;

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

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

	function hasObjectKey( value, index ) {
		return (
			!! value &&
			!! index &&
			isTrue( Object.prototype.hasOwnProperty.call( value, index ) )
		);
	}

	const EMPTY_STRING = '';

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

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

	function isStringArray( items ) {
		return Array.isArray( items ) && isIterable( items )
			? ! forEach( items, ( item ) => ! isNonEmptyString( item ) )
			: false;
	}

	function getGlobalObject(
		objectName,
		expectedKeys = EMPTY_ARRAY
	) {
		if ( ! ( expectedKeys === EMPTY_ARRAY || isStringArray( expectedKeys ) ) ) {
			return NULL_OBJECT;
		}

		const GLOBAL_OBJECT = getObjectValue( window, objectName );

		if ( ! isObject( GLOBAL_OBJECT ) ) {
			return NULL_OBJECT;
		}

		if ( ! isPositiveNumber( expectedKeys.length ) ) {
			return GLOBAL_OBJECT;
		}

		const HAS_EXPECTED_KEYS =
			! forEach(
				expectedKeys,
				( key ) => ! hasObjectKey( GLOBAL_OBJECT, key )
			);

		return isTrue( HAS_EXPECTED_KEYS )
			? GLOBAL_OBJECT
			: NULL_OBJECT;
	}

	function getWp() {
		return getGlobalObject( OBJECT_NAME_WP );
	}

	function isWpCustomise( wpObj ) {
		return (
			!! wpObj &&
			isFunction( wpObj.customize ) &&
			isFunction( wpObj.customize.bind )
		);
	}

	function bindCustomEvent( eventType, callback ) {
		if ( ! isNonEmptyString( eventType ) || ! isFunction( callback ) ) {
			return;
		}

		const WP = getWp();

		if ( ! isWpCustomise( WP ) ) {
			return;
		}

		WP.customize.bind( eventType, callback );
	}

	function onCustomControlsReady( callback ) {
		bindCustomEvent( EVENT_READY, callback );
	}

	const CUSTOMISER_SECTION_ID_FOOTER = 'footer';
	const EVENT_SECTION_TOGGLE = 'section-toggle';

	function isCustomiseSection( section ) {
		return (
			!! section &&
			isNonEmptyString( section.id ) &&
			isFunction( section.expanded )
		);
	}

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

	function isWpCustomisePreviewer( wpObj ) {
		return (
			!! wpObj &&
			isFunction( wpObj.customize ) &&
			isObject( wpObj.customize.previewer ) &&
			isFunction( wpObj.customize.previewer.send )
		);
	}

	function sendCustomiseSectionToggle( isExpanded, section ) {
		if ( ! isBoolean( isExpanded ) || ! isCustomiseSection( section ) ) {
			return;
		}

		const WP = getWp();

		if ( ! isWpCustomisePreviewer( WP ) ) {
			return;
		}

		WP.customize.previewer.send(
			EVENT_SECTION_TOGGLE,
			{
				isExpanded,
				sectionId: section.id,
			}
		);
	}

	function isWpCustomiseSection( wpObj ) {
		return (
			!! wpObj &&
			isFunction( wpObj.customize ) &&
			isFunction( wpObj.customize.section )
		);
	}

	function onCustomiseSectionToggle( sectionId, callback ) {
		if ( ! isNonEmptyString( sectionId ) || ! isFunction( callback ) ) {
			return;
		}

		const WP = getWp();

		if ( ! isWpCustomiseSection( WP ) ) {
			return;
		}

		WP.customize.section(
			sectionId,
			( section ) => {
				if ( ! isCustomiseSection( section ) ) {
					return;
				}

				section.expanded.bind( ( isExpanded ) => {
					if ( ! isBoolean( isExpanded ) ) {
						return;
					}

					callback( isExpanded, section );
				} );
			}
		);
	}

	function addFooterSectionToggleEvent() {
		onCustomiseSectionToggle(
			CUSTOMISER_SECTION_ID_FOOTER,
			sendCustomiseSectionToggle
		);
	}

	function addFooterEvents() {
		addFooterSectionToggleEvent();
	}

	const CUSTOM_ID_TAGLINE = 'blogdescription';
	const CUSTOM_ID_SITE_TITLE = 'blogname';
	const CUSTOM_IDS_SITE_IDENTITY_TEXTS = [
		CUSTOM_ID_SITE_TITLE,
		CUSTOM_ID_TAGLINE,
	];
	const CUSTOM_ID_LOGO = 'custom_logo';

	function isCustomControl( control ) {
		return (
			!! control &&
			isObject( control.container ) &&
			isFunction( control.active ) &&
			isFunction( control.active.set )
		);
	}

	function isWpCustomControl( wpObj ) {
		return (
			!! wpObj &&
			isFunction( wpObj.customize ) &&
			isFunction( wpObj.customize.control )
		);
	}

	function getCustomControl( id ) {
		if ( ! isNonEmptyString( id ) ) {
			return NULL_OBJECT;
		}

		const WP = getWp();

		if ( ! isWpCustomControl( WP ) ) {
			return NULL_OBJECT;
		}

		const CONTROL = WP.customize.control( id );

		return isCustomControl( CONTROL )
			? CONTROL
			: NULL_OBJECT;
	}

	function setCustomControlActiveState( id, isControlActive ) {
		if ( ! isNonEmptyString( id ) || ! isBoolean( isControlActive ) ) {
			return;
		}

		const CONTROL = getCustomControl( id );

		if ( ! CONTROL ) {
			return;
		}

		CONTROL.active.set( isControlActive );
	}

	function isCustomSetting( setting ) {
		return (
			isFunction( setting ) &&
			isNonEmptyString( setting.id ) &&
			isFunction( setting.bind )
		);
	}

	function bindCustomSetting( id, callback ) {
		if ( ! isNonEmptyString( id ) || ! isFunction( callback ) ) {
			return;
		}

		const WP = getWp();

		if ( ! isWpCustomise( WP ) ) {
			return;
		}

		WP.customize(
			id,
			( setting ) => {
				if ( ! isCustomSetting( setting ) ) {
					return;
				}

				setting.bind( ( value ) => callback( value, id ) );
			}
		);
	}

	function onCustomControlChange( id, callback ) {
		bindCustomSetting( id, callback );
	}

	function isSiteIdentityTextActive( logoId ) {
		return ! isPositiveNumber( logoId );
	}

	function toggleSiteIdentityTextControlVisibilities( logoId ) {
		forEach(
			CUSTOM_IDS_SITE_IDENTITY_TEXTS,
			( id ) => setCustomControlActiveState(
				id,
				isSiteIdentityTextActive( logoId )
			)
		);
	}

	function addLogoChangeEvent() {
		onCustomControlChange(
			CUSTOM_ID_LOGO,
			toggleSiteIdentityTextControlVisibilities
		);
	}

	function addSiteIdentityEvents() {
		addLogoChangeEvent();
	}

	function addCustomControlEvents() {
		onCustomControlsReady( () => {
			addSiteIdentityEvents();
			addFooterEvents();
		} );
	}

	addCustomControlEvents();
}() );
