import*as FormatterModule from"../formatter/formatter.js";import*as i18n from"../i18n/i18n.js";import*as Persistence from"../persistence/persistence.js";import*as SourceFrame from"../source_frame/source_frame.js";import*as UI from"../ui/ui.js";import*as Workspace from"../workspace/workspace.js";import{Events,registerEditorAction}from"./SourcesView.js";const UIStrings={prettyPrintS:"Pretty print {PH1}",prettyPrint:"Pretty print"},str_=i18n.i18n.registerUIStrings("sources/ScriptFormatterEditorAction.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let scriptFormatterEditorActionInstance;export class ScriptFormatterEditorAction{constructor(){this._pathsToFormatOnLoad=new Set,this._sourcesView,this._button}static instance(t={forceNew:null}){const{forceNew:r}=t;return scriptFormatterEditorActionInstance&&!r||(scriptFormatterEditorActionInstance=new ScriptFormatterEditorAction),scriptFormatterEditorActionInstance}_editorSelected(t){const r=t.data;this._updateButton(r),this._isFormatableScript(r)&&this._pathsToFormatOnLoad.has(r.url())&&!FormatterModule.SourceFormatter.SourceFormatter.instance().hasFormatted(r)&&this._showFormatted(r)}async _editorClosed(t){const r=t.data.uiSourceCode;t.data.wasSelected&&this._updateButton(null);const e=await FormatterModule.SourceFormatter.SourceFormatter.instance().discardFormattedUISourceCode(r);e&&this._pathsToFormatOnLoad.delete(e.url())}_updateButton(t){const r=this._isFormatableScript(t);this._button.element.classList.toggle("hidden",!r),t&&this._button.setTitle(i18nString(UIStrings.prettyPrintS,{PH1:t.name()}))}button(t){return this._button||(this._sourcesView=t,this._sourcesView.addEventListener(Events.EditorSelected,(t=>{this._editorSelected(t)})),this._sourcesView.addEventListener(Events.EditorClosed,(t=>{this._editorClosed(t)})),this._button=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.prettyPrint),"largeicon-pretty-print"),this._button.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this._onFormatScriptButtonClicked,this),this._updateButton(t.currentUISourceCode())),this._button}_isFormatableScript(t){return!!t&&(!t.project().canSetFileContent()&&(t.project().type()!==Workspace.Workspace.projectTypes.Formatter&&(!Persistence.Persistence.PersistenceImpl.instance().binding(t)&&("application/wasm"!==t.mimeType()&&t.contentType().hasScripts()))))}isCurrentUISourceCodeFormatable(){const t=this._sourcesView.currentUISourceCode();return this._isFormatableScript(t)}_onFormatScriptButtonClicked(t){this.toggleFormatScriptSource()}toggleFormatScriptSource(){const t=this._sourcesView.currentUISourceCode();t&&this._isFormatableScript(t)&&(this._pathsToFormatOnLoad.add(t.url()),this._showFormatted(t))}async _showFormatted(t){const r=await FormatterModule.SourceFormatter.SourceFormatter.instance().format(t);if(t!==this._sourcesView.currentUISourceCode())return;const e=this._sourcesView.viewForFile(t);let o=[0,0];if(e instanceof SourceFrame.SourceFrame.SourceFrameImpl){const t=e.selection();o=r.mapping.originalToFormatted(t.startLine,t.startColumn)}this._sourcesView.showSourceLocation(r.formattedSourceCode,o[0],o[1])}}registerEditorAction(ScriptFormatterEditorAction.instance);