import*as SDK from"../sdk/sdk.js";export class ApplicationCacheModel extends SDK.SDKModel.SDKModel{constructor(e){super(e),e.registerApplicationCacheDispatcher(new ApplicationCacheDispatcher(this)),this._agent=e.applicationCacheAgent(),this._agent.invoke_enable();const t=e.model(SDK.ResourceTreeModel.ResourceTreeModel);if(!t)throw new Error("Target must provide an ResourceTreeModel");t.addEventListener(SDK.ResourceTreeModel.Events.FrameNavigated,this._frameNavigatedCallback,this),t.addEventListener(SDK.ResourceTreeModel.Events.FrameDetached,this._frameDetached,this),this._statuses=new Map,this._manifestURLsByFrame=new Map,this._mainFrameNavigated(),this._onLine=!0}_frameNavigatedCallback(e){this._frameNavigated(e)}async _frameNavigated(e){const t=e.data;if(t.isMainFrame())return void this._mainFrameNavigated();const a=t.id,s=await this._agent.invoke_getManifestForFrame({frameId:a});null===s||s||this._frameManifestRemoved(a)}_frameDetached(e){const t=e.data;this._frameManifestRemoved(t.id)}reset(){this._statuses.clear(),this._manifestURLsByFrame.clear(),this.dispatchEventToListeners(Events.FrameManifestsReset)}async _mainFrameNavigated(){const e=await this._agent.invoke_getFramesWithManifests();if(!e.getError())for(const t of e.frameIds)this._frameManifestUpdated(t.frameId,t.manifestURL,t.status)}_frameManifestUpdated(e,t,a){if(a===UNCACHED)return void this._frameManifestRemoved(e);if(!t)return;const s=this._manifestURLsByFrame.get(e);s&&t!==s&&this._frameManifestRemoved(e);const i=this._statuses.get(e)!==a;this._statuses.set(e,a),this._manifestURLsByFrame.has(e)||(this._manifestURLsByFrame.set(e,t),this.dispatchEventToListeners(Events.FrameManifestAdded,e)),i&&this.dispatchEventToListeners(Events.FrameManifestStatusUpdated,e)}_frameManifestRemoved(e){const t=this._manifestURLsByFrame.delete(e);this._statuses.delete(e),t&&this.dispatchEventToListeners(Events.FrameManifestRemoved,e)}frameManifestURL(e){return this._manifestURLsByFrame.get(e)||""}frameManifestStatus(e){return this._statuses.get(e)||UNCACHED}get onLine(){return this._onLine}_statusUpdated(e,t,a){this._frameManifestUpdated(e,t,a)}async requestApplicationCache(e){const t=await this._agent.invoke_getApplicationCacheForFrame({frameId:e});return t.getError()?null:t.applicationCache}_networkStateUpdated(e){this._onLine=e,this.dispatchEventToListeners(Events.NetworkStateChanged,e)}}SDK.SDKModel.SDKModel.register(ApplicationCacheModel,SDK.SDKModel.Capability.DOM,!1);export var Events;!function(e){e.FrameManifestStatusUpdated="FrameManifestStatusUpdated",e.FrameManifestAdded="FrameManifestAdded",e.FrameManifestRemoved="FrameManifestRemoved",e.FrameManifestsReset="FrameManifestsReset",e.NetworkStateChanged="NetworkStateChanged"}(Events||(Events={}));export class ApplicationCacheDispatcher{constructor(e){this._applicationCacheModel=e}applicationCacheStatusUpdated({frameId:e,manifestURL:t,status:a}){this._applicationCacheModel._statusUpdated(e,t,a)}networkStateUpdated({isNowOnline:e}){this._applicationCacheModel._networkStateUpdated(e)}}export const UNCACHED=0;export const IDLE=1;export const CHECKING=2;export const DOWNLOADING=3;export const UPDATEREADY=4;export const OBSOLETE=5;