import*as Bindings from"../bindings/bindings.js";import*as Components from"../components/components.js";import*as i18n from"../i18n/i18n.js";import*as UI from"../ui/ui.js";import{Plugin}from"./Plugin.js";const UIStrings={sourceMappedFromS:"(source mapped from {PH1})",providedViaDebugInfoByS:"(provided via debug info by {PH1})"},str_=i18n.i18n.registerUIStrings("sources/ScriptOriginPlugin.js",UIStrings);export class ScriptOriginPlugin extends Plugin{constructor(i,r){super(),this._textEditor=i,this._uiSourceCode=r}static accepts(i){return i.contentType().hasScripts()||Boolean(ScriptOriginPlugin._script(i))}async rightToolbarItems(){const i=Bindings.CompilerScriptMapping.CompilerScriptMapping.uiSourceCodeOrigin(this._uiSourceCode);if(i.length)return i.map((i=>{const r=i18n.i18n.getFormatLocalizedString(str_,UIStrings.sourceMappedFromS,{PH1:Components.Linkifier.Linkifier.linkifyURL(i)});return new UI.Toolbar.ToolbarItem(r)}));const r=Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance().pluginManager;if(r)for(const e of r.scriptsForUISourceCode(this._uiSourceCode))if(e.hasSourceURL){const i=i18n.i18n.getFormatLocalizedString(str_,UIStrings.providedViaDebugInfoByS,{PH1:Components.Linkifier.Linkifier.linkifyURL(e.sourceURL)});return[new UI.Toolbar.ToolbarItem(i)]}const n=await ScriptOriginPlugin._script(this._uiSourceCode);if(!n||!n.originStackTrace)return[];const o=linkifier.linkifyStackTraceTopFrame(n.debuggerModel.target(),n.originStackTrace);return[new UI.Toolbar.ToolbarItem(o)]}static async _script(i){const r=await Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance().uiLocationToRawLocations(i,0,0);for(const n of r){const i=n.script();if(i&&i.originStackTrace)return i}return null}}export const linkifier=new Components.Linkifier.Linkifier;