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";import{CSSWorkspaceBinding}from"./CSSWorkspaceBinding.js";import{NetworkProject}from"./NetworkProject.js";const uiSourceCodeToSourceMapMap=new WeakMap;export class SASSSourceMapping{constructor(e,o,r){this._sourceMapManager=o,this._project=new ContentProviderBasedProject(r,"cssSourceMaps:"+e.id(),Workspace.Workspace.projectTypes.Network,"",!1),NetworkProject.setTargetForProject(this._project,e),this._eventListeners=[this._sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapAttached,(e=>{this._sourceMapAttached(e)}),this),this._sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapDetached,(e=>{this._sourceMapDetached(e)}),this),this._sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapChanged,(e=>{this._sourceMapChanged(e)}),this)]}_sourceMapAttachedForTest(e){}async _sourceMapAttached(e){const o=e.data.client,r=e.data.sourceMap;for(const t of r.sourceURLs()){let e=this._project.uiSourceCodeForURL(t);if(e){NetworkProject.addFrameAttribution(e,o.frameId);continue}const c=r.sourceContentProvider(t,Common.ResourceType.resourceTypes.SourceMapStyleSheet),s=Common.ResourceType.ResourceType.mimeFromURL(t)||c.contentType().canonicalMimeType(),a=r.embeddedContentByURL(t),n="string"===typeof a?new Workspace.UISourceCode.UISourceCodeMetadata(null,a.length):null;e=this._project.createUISourceCode(t,c.contentType()),NetworkProject.setInitialFrameAttribution(e,o.frameId),uiSourceCodeToSourceMapMap.set(e,r),this._project.addUISourceCodeWithProvider(e,c,n,s)}await CSSWorkspaceBinding.instance().updateLocations(o),this._sourceMapAttachedForTest(r)}async _sourceMapDetached(e){const o=e.data.client,r=e.data.sourceMap,t=this._sourceMapManager.clientsForSourceMap(r);for(const c of r.sourceURLs())if(t.length){const e=this._project.uiSourceCodeForURL(c);if(!e)continue;NetworkProject.removeFrameAttribution(e,o.frameId)}else this._project.removeFile(c);await CSSWorkspaceBinding.instance().updateLocations(o)}async _sourceMapChanged(e){const o=e.data.sourceMap,r=e.data.newSources,t=this._sourceMapManager.clientsForSourceMap(o);for(const s of r.keys()){const e=this._project.uiSourceCodeForURL(s);if(!e){console.error("Failed to update source for "+s);continue}const o=r.get(s);e.setWorkingCopy(o)}const c=t.map((e=>CSSWorkspaceBinding.instance().updateLocations(e)));await Promise.all(c)}rawLocationToUILocation(e){const o=e.header();if(!o)return null;const r=this._sourceMapManager.sourceMapForClient(o);if(!r)return null;const t=r.findEntry(e.lineNumber,e.columnNumber);if(!t||!t.sourceURL)return null;const c=this._project.uiSourceCodeForURL(t.sourceURL);return c?c.uiLocation(t.sourceLineNumber||0,t.sourceColumnNumber):null}uiLocationToRawLocations(e){const o=uiSourceCodeToSourceMapMap.get(e.uiSourceCode);if(!o)return[];const r=o.findReverseEntries(e.uiSourceCode.url(),e.lineNumber,e.columnNumber||0),t=[];for(const c of this._sourceMapManager.clientsForSourceMap(o))t.push(...r.map((e=>new SDK.CSSModel.CSSLocation(c,e.lineNumber,e.columnNumber))));return t}dispose(){this._project.dispose(),Common.EventTarget.EventTarget.removeEventListeners(this._eventListeners)}}