/*!
 * VisualEditor UserInterface CodeMirrorAction class.
 */

require( './ve.ui.CodeMirrorTool.js' );

/**
 * CodeMirror action
 *
 * @class
 * @extends ve.ui.Action
 * @constructor
 * @param {ve.ui.Surface} surface Surface to act on
 */
ve.ui.CodeMirrorAction = function VeUiCodeMirrorAction() {
	// Parent constructor
	ve.ui.CodeMirrorAction.super.apply( this, arguments );
};

/* Inheritance */

OO.inheritClass( ve.ui.CodeMirrorAction, ve.ui.Action );

/* Static Properties */

ve.ui.CodeMirrorAction.static.name = 'codeMirror';

/**
 * @inheritdoc
 */
ve.ui.CodeMirrorAction.static.methods = [ 'toggle' ];

/* Methods */

/**
 * @method
 * @param {boolean} [enable] State to force toggle to, inverts current state if undefined
 * @return {Promise} Action was executed
 */
ve.ui.CodeMirrorAction.prototype.toggle = async function ( enable ) {
	if ( !this.surface.mirror && ( enable || enable === undefined ) ) {
		await mw.loader.using( [ 'ext.CodeMirror.mode.mediawiki', 'jquery.client' ] );
		if ( this.surface.mirror ) {
			mw.log( '[CodeMirror] VE mirror already initialized by another action.' );
			return;
		}
		const CodeMirrorVisualEditor = require( '../codemirror.visualEditor.js' );
		const { mediawiki } = require( 'ext.CodeMirror.mode.mediawiki' );
		this.surface.mirror = new CodeMirrorVisualEditor(
			this.surface,
			mediawiki( {
				bidiIsolation: false,
				codeFolding: false,
				autocomplete: false,
				openLinks: false
			} )
		);
		this.surface.mirror.initialize();
		this.surface.mirror.setCodeMirrorPreference( true );
	} else if ( this.surface.mirror ) {
		this.surface.mirror.toggle( enable );
		this.surface.mirror.setCodeMirrorPreference( this.surface.mirror.isActive );
	}
};

/* Registration */

ve.ui.actionFactory.register( ve.ui.CodeMirrorAction );
