import*as SDK from"../sdk/sdk.js";import*as Workspace from"../workspace/workspace.js";import{DebuggerWorkspaceBinding}from"./DebuggerWorkspaceBinding.js";import{LiveLocationPool}from"./LiveLocation.js";const debuggerModelToMessageHelperMap=new WeakMap;export class PresentationConsoleMessageManager{constructor(){SDK.SDKModel.TargetManager.instance().observeModels(SDK.DebuggerModel.DebuggerModel,this),SDK.ConsoleModel.ConsoleModel.instance().addEventListener(SDK.ConsoleModel.Events.ConsoleCleared,this._consoleCleared,this),SDK.ConsoleModel.ConsoleModel.instance().addEventListener(SDK.ConsoleModel.Events.MessageAdded,(e=>this._consoleMessageAdded(e.data))),SDK.ConsoleModel.ConsoleModel.instance().messages().forEach(this._consoleMessageAdded,this)}modelAdded(e){debuggerModelToMessageHelperMap.set(e,new PresentationConsoleMessageHelper(e))}modelRemoved(e){const s=debuggerModelToMessageHelperMap.get(e);s&&s._consoleCleared()}_consoleMessageAdded(e){const s=e.runtimeModel();if(!e.isErrorOrWarning()||!e.runtimeModel()||e.source===SDK.ConsoleModel.MessageSource.Violation||!s)return;const o=debuggerModelToMessageHelperMap.get(s.debuggerModel());o&&o._consoleMessageAdded(e)}_consoleCleared(){for(const e of SDK.SDKModel.TargetManager.instance().models(SDK.DebuggerModel.DebuggerModel)){const s=debuggerModelToMessageHelperMap.get(e);s&&s._consoleCleared()}}}export class PresentationConsoleMessageHelper{constructor(e){this._debuggerModel=e,this._pendingConsoleMessages=new Map,this._presentationConsoleMessages=[],e.addEventListener(SDK.DebuggerModel.Events.ParsedScriptSource,(e=>{queueMicrotask((()=>{this._parsedScriptSource(e)}))})),e.addEventListener(SDK.DebuggerModel.Events.GlobalObjectCleared,this._debuggerReset,this),this._locationPool=new LiveLocationPool}_consoleMessageAdded(e){const s=this._rawLocation(e);s?this._addConsoleMessageToScript(e,s):this._addPendingConsoleMessage(e)}_rawLocation(e){if(e.scriptId)return this._debuggerModel.createRawLocationByScriptId(e.scriptId,e.line,e.column);const s=e.stackTrace&&e.stackTrace.callFrames?e.stackTrace.callFrames[0]:null;return s?this._debuggerModel.createRawLocationByScriptId(s.scriptId,s.lineNumber,s.columnNumber):e.url?this._debuggerModel.createRawLocationByURL(e.url,e.line,e.column):null}_addConsoleMessageToScript(e,s){this._presentationConsoleMessages.push(new PresentationConsoleMessage(e,s,this._locationPool))}_addPendingConsoleMessage(e){if(!e.url)return;const s=this._pendingConsoleMessages.get(e.url);s?s.push(e):this._pendingConsoleMessages.set(e.url,[e])}_parsedScriptSource(e){const s=e.data,o=this._pendingConsoleMessages.get(s.sourceURL);if(!o)return;const t=[];for(const n of o){const e=this._rawLocation(n);e&&s.scriptId===e.scriptId?this._addConsoleMessageToScript(n,e):t.push(n)}t.length?this._pendingConsoleMessages.set(s.sourceURL,t):this._pendingConsoleMessages.delete(s.sourceURL)}_consoleCleared(){this._pendingConsoleMessages=new Map,this._debuggerReset()}_debuggerReset(){for(const e of this._presentationConsoleMessages)e.dispose();this._presentationConsoleMessages=[],this._locationPool.disposeAll()}}export class PresentationConsoleMessage{constructor(e,s,o){this._text=e.messageText,this._level=e.level===SDK.ConsoleModel.MessageLevel.Error?Workspace.UISourceCode.Message.Level.Error:Workspace.UISourceCode.Message.Level.Warning,DebuggerWorkspaceBinding.instance().createLiveLocation(s,this._updateLocation.bind(this),o)}async _updateLocation(e){this._uiMessage&&this._uiMessage.remove();const s=await e.uiLocation();s&&(this._uiMessage=s.uiSourceCode.addLineMessage(this._level,this._text,s.lineNumber,s.columnNumber))}dispose(){this._uiMessage&&this._uiMessage.remove()}}