import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as Platform from"../platform/platform.js";import{Events as TargetManagerEvents,TargetManager}from"./SDKModel.js";import{TextSourceMap}from"./SourceMap.js";const UIStrings={devtoolsFailedToLoadSourcemapS:"DevTools failed to load SourceMap: {PH1}"},str_=i18n.i18n.registerUIStrings("sdk/SourceMapManager.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class SourceMapManager extends Common.ObjectWrapper.ObjectWrapper{constructor(e){super(),this._target=e,this._isEnabled=!0,this._relativeSourceURL=new Map,this._relativeSourceMapURL=new Map,this._resolvedSourceMapId=new Map,this._sourceMapById=new Map,this._sourceMapIdToLoadingClients=new Platform.MapUtilities.Multimap,this._sourceMapIdToClients=new Platform.MapUtilities.Multimap,TargetManager.instance().addEventListener(TargetManagerEvents.InspectedURLChanged,this._inspectedURLChanged,this)}setEnabled(e){if(e===this._isEnabled)return;this._isEnabled=e;const t=[...this._resolvedSourceMapId.keys()];for(const s of t){const e=this._relativeSourceURL.get(s),t=this._relativeSourceMapURL.get(s);this.detachSourceMap(s),this.attachSourceMap(s,e,t)}}_inspectedURLChanged(e){if(e.data!==this._target)return;const t=new Map(this._resolvedSourceMapId);for(const[s,o]of t){const e=this._relativeSourceURL.get(s),t=this._relativeSourceMapURL.get(s);if(void 0===e||void 0===t)continue;const a=this._resolveRelativeURLs(e,t);null!==a&&o!==a.sourceMapId&&(this.detachSourceMap(s),this.attachSourceMap(s,e,t))}}sourceMapForClient(e){const t=this._resolvedSourceMapId.get(e);return t&&this._sourceMapById.get(t)||null}clientsForSourceMap(e){const t=this._getSourceMapId(e.compiledURL(),e.url());return this._sourceMapIdToClients.has(t)?[...this._sourceMapIdToClients.get(t)]:[...this._sourceMapIdToLoadingClients.get(t)]}_getSourceMapId(e,t){return`${e}:${t}`}_resolveRelativeURLs(e,t){const s=Common.ParsedURL.ParsedURL.completeURL(this._target.inspectedURL(),e);if(!s)return null;const o=Common.ParsedURL.ParsedURL.completeURL(s,t);return o?{sourceURL:s,sourceMapURL:o,sourceMapId:this._getSourceMapId(s,o)}:null}attachSourceMap(e,t,s){if(void 0===t||!s)return;console.assert(!this._resolvedSourceMapId.has(e),"SourceMap is already attached to client");const o=this._resolveRelativeURLs(t,s);if(!o)return;this._relativeSourceURL.set(e,t),this._relativeSourceMapURL.set(e,s);const{sourceURL:a,sourceMapURL:r,sourceMapId:i}=o;function n(e,t){this._sourceMapIdToClients.set(e,t);const s=this._sourceMapById.get(e);this.dispatchEventToListeners(Events.SourceMapAttached,{client:t,sourceMap:s})}this._resolvedSourceMapId.set(e,i),this._isEnabled&&(this.dispatchEventToListeners(Events.SourceMapWillAttach,e),this._sourceMapById.has(i)?n.call(this,i,e):(this._sourceMapIdToLoadingClients.has(i)||TextSourceMap.load(r,a,e.createPageResourceLoadInitiator()).catch((e=>(Common.Console.Console.instance().warn(i18nString(UIStrings.devtoolsFailedToLoadSourcemapS,{PH1:e.message})),null))).then(function(e,t){this._sourceMapLoadedForTest();const s=this._sourceMapIdToLoadingClients.get(e);if(this._sourceMapIdToLoadingClients.deleteAll(e),!s.size)return;if(!t){for(const e of s)this.dispatchEventToListeners(Events.SourceMapFailedToAttach,e);return}this._sourceMapById.set(e,t);for(const o of s)n.call(this,e,o)}.bind(this,i)),this._sourceMapIdToLoadingClients.set(i,e)))}detachSourceMap(e){const t=this._resolvedSourceMapId.get(e);if(this._relativeSourceURL.delete(e),this._relativeSourceMapURL.delete(e),this._resolvedSourceMapId.delete(e),!t)return;if(!this._sourceMapIdToClients.hasValue(t,e))return void(this._sourceMapIdToLoadingClients.delete(t,e)&&this.dispatchEventToListeners(Events.SourceMapFailedToAttach,e));this._sourceMapIdToClients.delete(t,e);const s=this._sourceMapById.get(t);s&&(this._sourceMapIdToClients.has(t)||this._sourceMapById.delete(t),this.dispatchEventToListeners(Events.SourceMapDetached,{client:e,sourceMap:s}))}_sourceMapLoadedForTest(){}dispose(){TargetManager.instance().removeEventListener(TargetManagerEvents.InspectedURLChanged,this._inspectedURLChanged,this)}}export const Events={SourceMapWillAttach:Symbol("SourceMapWillAttach"),SourceMapFailedToAttach:Symbol("SourceMapFailedToAttach"),SourceMapAttached:Symbol("SourceMapAttached"),SourceMapDetached:Symbol("SourceMapDetached"),SourceMapChanged:Symbol("SourceMapChanged")};