import*as Common from"../common/common.js";import*as SDK from"../sdk/sdk.js";export class DOMStorage extends Common.ObjectWrapper.ObjectWrapper{constructor(e,t,r){super(),this._model=e,this._securityOrigin=t,this._isLocalStorage=r}static storageId(e,t){return{securityOrigin:e,isLocalStorage:t}}get id(){return DOMStorage.storageId(this._securityOrigin,this._isLocalStorage)}get securityOrigin(){return this._securityOrigin}get isLocalStorage(){return this._isLocalStorage}getItems(){return this._model._agent.invoke_getDOMStorageItems({storageId:this.id}).then((({entries:e})=>e))}setItem(e,t){this._model._agent.invoke_setDOMStorageItem({storageId:this.id,key:e,value:t})}removeItem(e){this._model._agent.invoke_removeDOMStorageItem({storageId:this.id,key:e})}clear(){this._model._agent.invoke_clear({storageId:this.id})}}!function(e){let t;!function(e){e.DOMStorageItemsCleared="DOMStorageItemsCleared",e.DOMStorageItemRemoved="DOMStorageItemRemoved",e.DOMStorageItemAdded="DOMStorageItemAdded",e.DOMStorageItemUpdated="DOMStorageItemUpdated"}(t=e.Events||(e.Events={}))}(DOMStorage||(DOMStorage={}));export class DOMStorageModel extends SDK.SDKModel.SDKModel{constructor(e){super(e),this._securityOriginManager=e.model(SDK.SecurityOriginManager.SecurityOriginManager),this._storages={},this._agent=e.domstorageAgent()}enable(){if(!this._enabled){if(this.target().registerDOMStorageDispatcher(new DOMStorageDispatcher(this)),this._securityOriginManager){this._securityOriginManager.addEventListener(SDK.SecurityOriginManager.Events.SecurityOriginAdded,this._securityOriginAdded,this),this._securityOriginManager.addEventListener(SDK.SecurityOriginManager.Events.SecurityOriginRemoved,this._securityOriginRemoved,this);for(const e of this._securityOriginManager.securityOrigins())this._addOrigin(e)}this._agent.invoke_enable(),this._enabled=!0}}clearForOrigin(e){if(this._enabled){for(const t of[!0,!1]){const r=this._storageKey(e,t),s=this._storages[r];if(!s)return;s.clear()}this._removeOrigin(e),this._addOrigin(e)}}_securityOriginAdded(e){this._addOrigin(e.data)}_addOrigin(e){const t=new Common.ParsedURL.ParsedURL(e);if(t.isValid&&"data"!==t.scheme&&"about"!==t.scheme&&"javascript"!==t.scheme)for(const r of[!0,!1]){const t=this._storageKey(e,r);console.assert(!this._storages[t]);const s=new DOMStorage(this,e,r);this._storages[t]=s,this.dispatchEventToListeners(Events.DOMStorageAdded,s)}}_securityOriginRemoved(e){this._removeOrigin(e.data)}_removeOrigin(e){for(const t of[!0,!1]){const r=this._storageKey(e,t),s=this._storages[r];s&&(delete this._storages[r],this.dispatchEventToListeners(Events.DOMStorageRemoved,s))}}_storageKey(e,t){return JSON.stringify(DOMStorage.storageId(e,t))}_domStorageItemsCleared(e){const t=this.storageForId(e);if(!t)return;t.dispatchEventToListeners(DOMStorage.Events.DOMStorageItemsCleared,{})}_domStorageItemRemoved(e,t){const r=this.storageForId(e);if(!r)return;const s={key:t};r.dispatchEventToListeners(DOMStorage.Events.DOMStorageItemRemoved,s)}_domStorageItemAdded(e,t,r){const s=this.storageForId(e);if(!s)return;const o={key:t,value:r};s.dispatchEventToListeners(DOMStorage.Events.DOMStorageItemAdded,o)}_domStorageItemUpdated(e,t,r,s){const o=this.storageForId(e);if(!o)return;const i={key:t,oldValue:r,value:s};o.dispatchEventToListeners(DOMStorage.Events.DOMStorageItemUpdated,i)}storageForId(e){return this._storages[JSON.stringify(e)]}storages(){const e=[];for(const t in this._storages)e.push(this._storages[t]);return e}}SDK.SDKModel.SDKModel.register(DOMStorageModel,SDK.SDKModel.Capability.DOM,!1);export var Events;!function(e){e.DOMStorageAdded="DOMStorageAdded",e.DOMStorageRemoved="DOMStorageRemoved"}(Events||(Events={}));export class DOMStorageDispatcher{constructor(e){this._model=e}domStorageItemsCleared({storageId:e}){this._model._domStorageItemsCleared(e)}domStorageItemRemoved({storageId:e,key:t}){this._model._domStorageItemRemoved(e,t)}domStorageItemAdded({storageId:e,key:t,newValue:r}){this._model._domStorageItemAdded(e,t,r)}domStorageItemUpdated({storageId:e,key:t,oldValue:r,newValue:s}){this._model._domStorageItemUpdated(e,t,r,s)}}