import*as Common from"../common/common.js";import*as SDK from"../sdk/sdk.js";import*as Workspace from"../workspace/workspace.js";import{ContentProviderBasedProject}from"./ContentProviderBasedProject.js";const uiSourceCodeToScriptsMap=new WeakMap,scriptToUISourceCodeMap=new WeakMap;export class DefaultScriptMapping{constructor(e,t,o){this._debuggerModel=e,this._debuggerWorkspaceBinding=o,this._project=new ContentProviderBasedProject(t,"debugger:"+e.target().id(),Workspace.Workspace.projectTypes.Debugger,"",!0),this._eventListeners=[e.addEventListener(SDK.DebuggerModel.Events.GlobalObjectCleared,this._debuggerReset,this),e.addEventListener(SDK.DebuggerModel.Events.ParsedScriptSource,this._parsedScriptSource,this),e.addEventListener(SDK.DebuggerModel.Events.DiscardedAnonymousScriptSource,this._discardedScriptSource,this)],this._uiSourceCodeToScriptsMap=new WeakMap}static scriptForUISourceCode(e){const t=uiSourceCodeToScriptsMap.get(e);return t?t.values().next().value:null}rawLocationToUILocation(e){const t=e.script();if(!t)return null;const o=scriptToUISourceCodeMap.get(t);if(!o)return null;const r=e.lineNumber-(t.isInlineScriptWithSourceURL()?t.lineOffset:0);let s=e.columnNumber||0;return t.isInlineScriptWithSourceURL()&&!r&&s&&(s-=t.columnOffset),o.uiLocation(r,s)}uiLocationToRawLocations(e,t,o){const r=this._uiSourceCodeToScriptsMap.get(e);return r?r.isInlineScriptWithSourceURL()?[this._debuggerModel.createRawLocation(r,t+r.lineOffset,t?o:o+r.columnOffset)]:[this._debuggerModel.createRawLocation(r,t,o)]:[]}_parsedScriptSource(e){const t=e.data,o=Common.ParsedURL.ParsedURL.extractName(t.sourceURL),r="debugger:///VM"+t.scriptId+(o?" "+o:""),s=this._project.createUISourceCode(r,Common.ResourceType.resourceTypes.Script);this._uiSourceCodeToScriptsMap.set(s,t);const i=uiSourceCodeToScriptsMap.get(s);i?i.add(t):uiSourceCodeToScriptsMap.set(s,new Set([t])),scriptToUISourceCodeMap.set(t,s),this._project.addUISourceCodeWithProvider(s,t,null,"text/javascript"),this._debuggerWorkspaceBinding.updateLocations(t)}_discardedScriptSource(e){const t=e.data,o=scriptToUISourceCodeMap.get(t);if(!o)return;scriptToUISourceCodeMap.delete(t),this._uiSourceCodeToScriptsMap.delete(o);const r=uiSourceCodeToScriptsMap.get(o);r&&(r.delete(t),r.size||uiSourceCodeToScriptsMap.delete(o)),this._project.removeUISourceCode(o.url())}_debuggerReset(){this._project.reset()}dispose(){Common.EventTarget.EventTarget.removeEventListeners(this._eventListeners),this._debuggerReset(),this._project.dispose()}}