/**
 * 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 ADMIN_DISMISS_ADMIN_NOTICE_WELCOME = 'errigal_dismiss_admin_notice_welcome';
	const CLASS_ADMIN_NOTICE_DISMISS = 'notice-dismiss';
	const ID_ADMIN_NOTICE_WELCOME = 'errigal-welcome-notice';
	const SELECTOR_ADMIN_NOTICE_WELCOME_DISMISS = `#${ ID_ADMIN_NOTICE_WELCOME } .${ CLASS_ADMIN_NOTICE_DISMISS }`;
	const OBJECT_NAME_WP_AJAXURL = 'ajaxurl';
	const EMPTY_STRING = '';
	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 isObjectType( value, expectedType ) {
		return Object.prototype.toString.call( value ) ===
			`[object ${ expectedType }]`;
	}

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

	function getGlobalString( propertyName ) {
		const GLOBAL_STRING = getObjectValue( window, propertyName );

		return isString( GLOBAL_STRING )
			? GLOBAL_STRING
			: EMPTY_STRING;
	}

	function getAjaxUrl() {
		return getGlobalString( OBJECT_NAME_WP_AJAXURL );
	}

	const EVENT_CLICK = 'click';
	const OBJECT_NAME_JQUERY = 'jQuery';
	const NULL_OBJECT = null;

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

	function getGlobalFunction( functionName ) {
		const GLOBAL_FUNCTION = getObjectValue( window, functionName );

		return isFunction( GLOBAL_FUNCTION )
			? GLOBAL_FUNCTION
			: NULL_OBJECT;
	}

	function isJqueryDocument( value ) {
		return !! value && isFunction( value.ready );
	}

	function getJqueryDocument() {
		const JQUERY = getGlobalFunction( OBJECT_NAME_JQUERY );

		if ( ! JQUERY ) {
			return NULL_OBJECT;
		}

		const JQUERY_DOCUMENT = JQUERY( document );

		return isJqueryDocument( JQUERY_DOCUMENT )
			? JQUERY_DOCUMENT
			: NULL_OBJECT;
	}

	function isJqueryObject( obj ) {
		return (
			!! obj &&
			isFunction( obj ) &&
			isObject( obj() ) &&
			isFunction( obj().on ) &&
			isFunction( obj.post )
		);
	}

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

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

		const JQUERY_DOCUMENT = getJqueryDocument();

		if ( ! JQUERY_DOCUMENT ) {
			return;
		}

		JQUERY_DOCUMENT.ready( ( $ ) => {
			if ( ! isJqueryObject( $ ) ) {
				return;
			}

			$( selector ).on( eventType, () => callback( $ ) );
		} );
	}

	function onJqueryClick( selector, callback ) {
		onJqueryEvent( EVENT_CLICK, selector, callback );
	}

	function sendJqueryPostRequest( $, url, data ) {
		if (
			! isJqueryObject( $ ) ||
			! isNonEmptyString( url ) ||
			! isObject( data )
		) {
			return;
		}

		$.post( url, data );
	}

	const OBJECT_KEY_ADMIN_NONCE = 'nonce';
	const OBJECT_KEY_ADMIN_FONT_SIZE_CLASS = 'font_size_class';
	const OBJECT_KEY_ADMIN_MEDIA_MODAL_BUTTON_TEXT_SELECT = 'admin_media_modal_button_text_select';
	const OBJECT_KEY_ADMIN_MEDIA_MODAL_TITLE = 'admin_media_modal_title';
	const OBJECT_KEYS_ADMIN = [
		OBJECT_KEY_ADMIN_MEDIA_MODAL_TITLE,
		OBJECT_KEY_ADMIN_MEDIA_MODAL_BUTTON_TEXT_SELECT,
		OBJECT_KEY_ADMIN_NONCE,
		OBJECT_KEY_ADMIN_FONT_SIZE_CLASS,
	];
	const OBJECT_NAME_ADMIN = 'ERRIGAL_ADMIN';
	const EMPTY_ARRAY = [];
	const INDEX_FIRST = 0;

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

	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;
	}

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

	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 getAdminObject() {
		return getGlobalObject( OBJECT_NAME_ADMIN, OBJECT_KEYS_ADMIN );
	}

	function getAdminNonce() {
		return getObjectValue( getAdminObject(), OBJECT_KEY_ADMIN_NONCE );
	}

	function addAdminNoticeDismissButtonEvent() {
		const AJAX_URL = getAjaxUrl();

		if ( ! isNonEmptyString( AJAX_URL ) ) {
			return;
		}

		const NONCE = getAdminNonce();

		if ( ! isNonEmptyString( NONCE ) ) {
			return;
		}

		onJqueryClick(
			SELECTOR_ADMIN_NOTICE_WELCOME_DISMISS,
			( $ ) => sendJqueryPostRequest(
				$,
				AJAX_URL,
				{
					action: ADMIN_DISMISS_ADMIN_NOTICE_WELCOME,
					nonce: NONCE,
				}
			)
		);
	}

	function addAdminNoticeEvents() {
		addAdminNoticeDismissButtonEvent();
	}

	const TAG_BODY = 'body';
	const TAG_HTML = 'html';
	const EVENT_ADDEDITOR = 'AddEditor';
	const EVENT_INIT = 'init';
	const ID_TINY_MCE_EDITOR_CONTENT = 'content';
	const OBJECT_NAME_WP_TINY_MCE = 'tinymce';

	function isTinyMce( obj ) {
		return !! obj && isFunction( obj.on );
	}

	function getTinyMce() {
		const TINY_MCE = getGlobalObject( OBJECT_NAME_WP_TINY_MCE );

		return isTinyMce( TINY_MCE )
			? TINY_MCE
			: NULL_OBJECT;
	}

	function isTinyMceEvent( event ) {
		return (
			!! event &&
			isObject( event.editor ) &&
			isNonEmptyString( event.editor.id ) &&
			isFunction( event.editor.on ) &&
			isFunction( event.editor.getBody ) &&
			event.editor.id === ID_TINY_MCE_EDITOR_CONTENT
		);
	}

	function onClassicEditorBodyReady( callback ) {
		if ( ! isFunction( callback ) ) {
			return;
		}

		const TINY_MCE = getTinyMce();

		if ( ! TINY_MCE ) {
			return;
		}

		TINY_MCE.on(
			EVENT_ADDEDITOR,
			( event ) => {
				if ( ! isTinyMceEvent( event ) ) {
					return;
				}

				event.editor.on( EVENT_INIT, () => callback( event.editor.getBody() ) );
			}
		);
	}

	const EVENT_DOMCONTENTLOADED = 'DOMContentLoaded';

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

	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 onDocumentDomContentLoaded( callback, options ) {
		onDocumentEvent( EVENT_DOMCONTENTLOADED, callback, options );
	}

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

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

	function hasTagName( element ) {
		return !! element && isNonEmptyString( element.tagName );
	}

	function isElementOfType( element, tagName ) {
		return (
			hasTagName( element ) &&
			isNonEmptyString( tagName ) &&
			areStringsEqualIgnoreCase( element.tagName, tagName )
		);
	}

	function getAdminFontSizeClass() {
		return getObjectValue( getAdminObject(), OBJECT_KEY_ADMIN_FONT_SIZE_CLASS );
	}

	function isHtml( element ) {
		return (
			isElementOfType( element, TAG_HTML ) &&
			!! element.classList &&
			!! element.classList.add
		);
	}

	function isBody( element ) {
		return isElementOfType( element, TAG_BODY );
	}

	function addSetClassicEditorFontSize() {
		const FONT_SIZE_CLASS = getAdminFontSizeClass();

		if ( ! isNonEmptyString( FONT_SIZE_CLASS ) ) {
			return;
		}

		onClassicEditorBodyReady( ( body ) => {
			if ( ! isBody( body ) ) {
				return;
			}

			const HTML = body.parentElement;

			if ( ! isHtml( HTML ) ) {
				return;
			}

			HTML.classList.add( FONT_SIZE_CLASS );
		} );
	}

	function addClassicEditorEvents() {
		onDocumentDomContentLoaded( addSetClassicEditorFontSize );
	}

	const ATTRIBUTE_SRC = 'src';
	const CLASS_WIDGET_HAS_IMAGE = 'has-image';
	const CSS_BACKGROUND_IMAGE = 'background-image';
	const CLASS_WIDGET_IMAGE = 'errigal-widget__image';
	const NULL_ELEMENT = null;

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

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

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

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

	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 getElementsByClassName(
		className,
		rootElement = document
	) {
		const ELEMENTS =
			callElementMethod(
				'getElementsByClassName',
				className,
				rootElement
			);

		return isHTMLCollection( ELEMENTS )
			? ELEMENTS
			: NULL_ELEMENT;
	}

	function getElementByClassName(
		className,
		rootElement = document
	) {
		return getFirstItem( getElementsByClassName( className, rootElement ) );
	}

	const CLASS_WIDGET_IMAGE_CONTROL = 'errigal-widget__image-control';

	function isWidgetImageControl( element ) {
		return isHTMLElement( element, CLASS_WIDGET_IMAGE_CONTROL );
	}

	function getWidgetImage( widgetImageControl ) {
		return isWidgetImageControl( widgetImageControl )
			? getElementByClassName( CLASS_WIDGET_IMAGE, widgetImageControl )
			: NULL_ELEMENT;
	}

	function getParentElement( element ) {
		return isHTMLElement( element )
			? element.parentElement
			: NULL_ELEMENT;
	}

	const CLASS_WIDGET_ADD_IMAGE_BUTTON_CONTAINER = 'errigal-widget__add-image-button-container';

	function isWidgetAddImageButtonContainer( element ) {
		return isHTMLElement( element, CLASS_WIDGET_ADD_IMAGE_BUTTON_CONTAINER );
	}

	const CLASS_WIDGET_ADD_IMAGE_BUTTON = 'errigal-widget__add-image-button';

	function isWidgetAddImageButton( element ) {
		return isHTMLElement( element, CLASS_WIDGET_ADD_IMAGE_BUTTON );
	}

	const CLASS_WIDGET_CHANGE_IMAGE_BUTTON = 'errigal-widget__change-image-button';

	function isWidgetChangeImageButton( element ) {
		return isHTMLElement( element, CLASS_WIDGET_CHANGE_IMAGE_BUTTON );
	}

	const CLASS_WIDGET_REMOVE_IMAGE_BUTTON = 'errigal-widget__remove-image-button';

	function isWidgetRemoveImageButton( element ) {
		return isHTMLElement( element, CLASS_WIDGET_REMOVE_IMAGE_BUTTON );
	}

	function isWidgetImageButton( element ) {
		return (
			isWidgetAddImageButton( element ) ||
			isWidgetChangeImageButton( element ) ||
			isWidgetRemoveImageButton( element )
		);
	}

	const CLASS_WIDGET_IMAGE_BUTTONS_CONTAINER = 'errigal-widget__image-buttons-container';

	function isWidgetImageButtonsContainer( element ) {
		return isHTMLElement( element, CLASS_WIDGET_IMAGE_BUTTONS_CONTAINER );
	}

	function getWidgetImageControl( widgetImageButton ) {
		if ( ! isWidgetImageButton( widgetImageButton ) ) {
			return NULL_ELEMENT;
		}

		const WIDGET_IMAGE_BUTTON_CONTAINER = getParentElement( widgetImageButton );

		if (
			! (
				isWidgetAddImageButtonContainer( WIDGET_IMAGE_BUTTON_CONTAINER ) ||
				isWidgetImageButtonsContainer( WIDGET_IMAGE_BUTTON_CONTAINER )
			)
		) {
			return NULL_ELEMENT;
		}

		const WIDGET_IMAGE_CONTROL =
			getParentElement( WIDGET_IMAGE_BUTTON_CONTAINER );

		return isWidgetImageControl( WIDGET_IMAGE_CONTROL )
			? WIDGET_IMAGE_CONTROL
			: NULL_ELEMENT;
	}

	const CLASS_WIDGET_IMAGE_PATH = 'errigal-widget__image-path';

	function getWidgetImagePathInput( widgetImageControl ) {
		return isWidgetImageControl( widgetImageControl )
			? getElementByClassName( CLASS_WIDGET_IMAGE_PATH, widgetImageControl )
			: NULL_ELEMENT;
	}

	function isWidgetImage( element ) {
		return isHTMLElement( element, CLASS_WIDGET_IMAGE );
	}

	const TAG_INPUT = 'input';

	function isHTMLElementOfType(
		tagName,
		element,
		...classNames
	) {
		return (
			isHTMLElement( element, ...classNames ) &&
			isElementOfType( element, tagName )
		);
	}

	function isInputElement( element, ...classNames ) {
		return isHTMLElementOfType( TAG_INPUT, element, ...classNames );
	}

	function isWidgetImagePathInput( element ) {
		return isInputElement( element, CLASS_WIDGET_IMAGE_PATH );
	}

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

		element.setAttribute( name, value );
	}

	const TAG_TEXTAREA = 'textarea';

	function isTextAreaElement( element, ...classNames ) {
		return isHTMLElementOfType( TAG_TEXTAREA, element, ...classNames );
	}

	function setInputValue( element, value ) {
		if ( ! ( isInputElement( element ) || isTextAreaElement( element ) ) ) {
			return;
		}

		element.value = value;
	}

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

		element.classList.add( className );
	}

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

		element.classList.remove( className );
	}

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

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

		TOGGLE_CLASS( element, className );
	}

	function hasStyle( element ) {
		return (
			!! element &&
			element.style instanceof CSSStyleDeclaration &&
			isFunction( element.style.setProperty )
		);
	}

	function isStyleProperty( property ) {
		return (
			isString( property ) &&
			hasStyle( document.documentElement ) &&
			property in document.documentElement.style
		);
	}

	function updateInlineStyle(
		element,
		property,
		value,
		isPriority
	) {
		if (
			! element ||
			! hasStyle( element ) ||
			! isStyleProperty( property ) ||
			! ( isString( value ) || isNumber( value ) ) ||
			! isBoolean( isPriority )
		) {
			return;
		}

		if ( isTrue( isPriority ) ) {
			element.style.setProperty( property, value, 'important' );

			return;
		}

		element.style.setProperty( property, value );
	}

	function updateElementInlineStyle(
		element,
		property,
		value,
		isPriority = false
	) {
		if ( ! isHTMLElement( element ) ) {
			return;
		}

		updateInlineStyle( element, property, value, isPriority );
	}

	const EVENT_CHANGE = 'change';
	const EVENT_TYPE_EVENT = 'Event';

	function isIe() {
		return !! window.MSInputMethodContext && !! document.documentMode;
	}

	function createEvent( type ) {
		if ( ! isNonEmptyString( type ) ) {
			return NULL_VALUE;
		}

		if ( isIe() ) {
			const EVENT_OBJECT = document.createEvent( EVENT_TYPE_EVENT );

			EVENT_OBJECT.initEvent( type, true, true );

			return EVENT_OBJECT;
		}

		return new Event( type, { bubbles: true, cancelable: true } );
	}

	function createChangeEvent() {
		return createEvent( EVENT_CHANGE );
	}

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

	function dispatchEvent( element, event ) {
		if (
			! isHTMLElement( element ) ||
			! isFunction( element.dispatchEvent ) ||
			! isEvent( event )
		) {
			return;
		}

		element.dispatchEvent( event );
	}

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

	const EVENT_SELECT = 'select';
	const MEDIA_TYPE_IMAGE = 'image';
	const OBJECT_NAME_WP = 'wp';

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

	function isMedia( mediaUploader ) {
		return (
			!! mediaUploader &&
			isFunction( mediaUploader.on ) &&
			isFunction( mediaUploader.open )
		);
	}

	function isWpMedia( wpObj ) {
		return !! wpObj && isFunction( wpObj.media );
	}

	function getMedia( args ) {
		if ( ! isObject( args ) ) {
			return NULL_OBJECT;
		}

		const WP = getWp();

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

		const MEDIA = WP.media( args );

		return isMedia( MEDIA )
			? MEDIA
			: NULL_OBJECT;
	}

	function getMediaUploader() {
		const ADMIN = getAdminObject();
		const TITLE = getObjectValue( ADMIN, OBJECT_KEY_ADMIN_MEDIA_MODAL_TITLE );

		if ( ! isNonEmptyString( TITLE ) ) {
			return NULL_OBJECT;
		}

		const BUTTON_TEXT =
			getObjectValue(
				ADMIN,
				OBJECT_KEY_ADMIN_MEDIA_MODAL_BUTTON_TEXT_SELECT
			);

		return isNonEmptyString( BUTTON_TEXT )
			? getMedia( {
				title: TITLE,
				library: {
					type: MEDIA_TYPE_IMAGE,
				},
				button: {
					text: BUTTON_TEXT,
				},
				multiple: false,
			} )
			: NULL_OBJECT;
	}

	const MEDIA_STATE_SELECTION = 'selection';

	function isMediaAttachment( value ) {
		return !! value && isString( value.url );
	}

	function isMediaAttachmentObject( obj ) {
		return !! obj && isFunction( obj.toJSON );
	}

	function isMediaStateSelection( value ) {
		return !! value && isFunction( value.first );
	}

	function isMediaUploaderInSelectEvent( mediaUploader ) {
		return (
			!! mediaUploader &&
			isFunction( mediaUploader.state ) &&
			isFunction( mediaUploader.state().get )
		);
	}

	function getMediaImagePath( mediaUploader ) {
		if ( ! isMediaUploaderInSelectEvent( mediaUploader ) ) {
			return EMPTY_STRING;
		}

		const STATE_SELECTION = mediaUploader.state().get( MEDIA_STATE_SELECTION );

		if ( ! isMediaStateSelection( STATE_SELECTION ) ) {
			return EMPTY_STRING;
		}

		const ATTACHMENT_OBJECT = STATE_SELECTION.first();

		if ( ! isMediaAttachmentObject( ATTACHMENT_OBJECT ) ) {
			return EMPTY_STRING;
		}

		const ATTACHMENT = ATTACHMENT_OBJECT.toJSON();

		return isMediaAttachment( ATTACHMENT )
			? ATTACHMENT.url
			: EMPTY_STRING;
	}

	function onMediaUploaderSelect( callback ) {
		if ( ! isFunction( callback ) ) {
			return;
		}

		const MEDIA_UPLOADER = getMediaUploader();

		if ( ! MEDIA_UPLOADER ) {
			return;
		}

		MEDIA_UPLOADER.on(
			EVENT_SELECT,
			() => callback( getMediaImagePath( MEDIA_UPLOADER ) )
		);
		MEDIA_UPLOADER.open();
	}

	function dispatchWidgetImagePathInputChangeEvent( widgetImagePathInput ) {
		if ( ! isWidgetImagePathInput( widgetImagePathInput ) ) {
			return;
		}

		dispatchEvent( widgetImagePathInput, createChangeEvent() );
	}

	function updateWidgetImagePathInputUrl( widgetImagePathInput, imagePath ) {
		if (
			! isWidgetImagePathInput( widgetImagePathInput ) ||
			! isString( imagePath )
		) {
			return;
		}

		setInputValue( widgetImagePathInput, imagePath );
	}

	function updateWidgetImageUrl( widgetImage, imagePath ) {
		if ( ! isWidgetImage( widgetImage ) || ! isString( imagePath ) ) {
			return;
		}

		if ( isIe() ) {
			updateElementInlineStyle(
				widgetImage,
				CSS_BACKGROUND_IMAGE,
				`url('${ imagePath }')`
			);

			return;
		}

		setAttribute( widgetImage, ATTRIBUTE_SRC, imagePath );
	}

	function updateWidgetImageAndControlVisibilities(
		widgetImageButton,
		imagePath
	) {
		if ( ! isString( imagePath ) ) {
			return;
		}

		const WIDGET_IMAGE_CONTROL = getWidgetImageControl( widgetImageButton );

		if ( ! WIDGET_IMAGE_CONTROL ) {
			return;
		}

		const WIDGET_IMAGE = getWidgetImage( WIDGET_IMAGE_CONTROL );

		if ( ! WIDGET_IMAGE ) {
			return;
		}

		const WIDGET_IMAGE_PATH_INPUT =
			getWidgetImagePathInput( WIDGET_IMAGE_CONTROL );

		if ( ! WIDGET_IMAGE_PATH_INPUT ) {
			return;
		}

		updateWidgetImageUrl( WIDGET_IMAGE, imagePath );
		updateWidgetImagePathInputUrl( WIDGET_IMAGE_PATH_INPUT, imagePath );
		dispatchWidgetImagePathInputChangeEvent( WIDGET_IMAGE_PATH_INPUT );
		toggleClass( isNonEmptyString( imagePath ), WIDGET_IMAGE_CONTROL, CLASS_WIDGET_HAS_IMAGE );
	}

	function addWidgetImageEvents() {
		onDocumentClick( ( event ) => {
			if ( ! isEvent( event ) ) {
				return;
			}

			if (
				isWidgetAddImageButton( event.target ) ||
				isWidgetChangeImageButton( event.target )
			) {
				onMediaUploaderSelect( ( imagePath ) =>
					updateWidgetImageAndControlVisibilities( event.target, imagePath )
				);

				return;
			}

			if ( isWidgetRemoveImageButton( event.target ) ) {
				updateWidgetImageAndControlVisibilities( event.target, EMPTY_STRING );
			}
		} );
	}

	function addWidgetEvents() {
		addWidgetImageEvents();
	}

	function addAdminEvents() {
		addAdminNoticeEvents();
		addClassicEditorEvents();
		addWidgetEvents();
	}

	addAdminEvents();
}() );
