import{ObjectWrapper}from'./Object.js';export class Settings{constructor(globalStorage,localStorage){this._globalStorage=globalStorage;this._localStorage=localStorage;this._sessionStorage=new SettingsStorage({});this._eventSupport=new ObjectWrapper();this._registry=new Map();this._moduleSettings=new Map();self.runtime.extensions('setting').forEach(this._registerModuleSetting.bind(this));} _registerModuleSetting(extension){const descriptor=extension.descriptor();const settingName=descriptor['settingName'];const isRegex=descriptor['settingType']==='regex';const defaultValue=descriptor['defaultValue'];let storageType;switch(descriptor['storageType']){case('local'):storageType=SettingStorageType.Local;break;case('session'):storageType=SettingStorageType.Session;break;case('global'):storageType=SettingStorageType.Global;break;default:storageType=SettingStorageType.Global;} const setting=isRegex?this.createRegExpSetting(settingName,defaultValue,undefined,storageType):this.createSetting(settingName,defaultValue,storageType);if(extension.title()){setting.setTitle(extension.title());} if(descriptor['userActionCondition']){setting.setRequiresUserAction(!!Root.Runtime.queryParam(descriptor['userActionCondition']));} setting._extension=extension;this._moduleSettings.set(settingName,setting);} moduleSetting(settingName){const setting=this._moduleSettings.get(settingName);if(!setting){throw new Error('No setting registered: '+settingName);} return setting;} settingForTest(settingName){const setting=this._registry.get(settingName);if(!setting){throw new Error('No setting registered: '+settingName);} return setting;} createSetting(key,defaultValue,storageType){const storage=this._storageFromType(storageType);if(!this._registry.get(key)){this._registry.set(key,new Setting(this,key,defaultValue,this._eventSupport,storage));} return(this._registry.get(key));} createLocalSetting(key,defaultValue){return this.createSetting(key,defaultValue,SettingStorageType.Local);} createRegExpSetting(key,defaultValue,regexFlags,storageType){if(!this._registry.get(key)){this._registry.set(key,new RegExpSetting(this,key,defaultValue,this._eventSupport,this._storageFromType(storageType),regexFlags));} return(this._registry.get(key));} clearAll(){this._globalStorage.removeAll();this._localStorage.removeAll();const versionSetting=Common.settings.createSetting(VersionController._currentVersionName,0);versionSetting.set(VersionController.currentVersion);} _storageFromType(storageType){switch(storageType){case(SettingStorageType.Local):return this._localStorage;case(SettingStorageType.Session):return this._sessionStorage;case(SettingStorageType.Global):return this._globalStorage;} return this._globalStorage;}} export class SettingsStorage{constructor(object,setCallback,removeCallback,removeAllCallback,storagePrefix){this._object=object;this._setCallback=setCallback||function(){};this._removeCallback=removeCallback||function(){};this._removeAllCallback=removeAllCallback||function(){};this._storagePrefix=storagePrefix||'';} set(name,value){name=this._storagePrefix+name;this._object[name]=value;this._setCallback(name,value);} has(name){name=this._storagePrefix+name;return name in this._object;} get(name){name=this._storagePrefix+name;return this._object[name];} remove(name){name=this._storagePrefix+name;delete this._object[name];this._removeCallback(name);} removeAll(){this._object={};this._removeAllCallback();} _dumpSizes(){Common.console.log('Ten largest settings: ');const sizes={__proto__:null};for(const key in this._object){sizes[key]=this._object[key].length;} const keys=Object.keys(sizes);function comparator(key1,key2){return sizes[key2]-sizes[key1];} keys.sort(comparator);for(let i=0;i<10&&i