import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";const UIStrings={databaseNoLongerHasExpected:"Database no longer has expected version.",anUnexpectedErrorSOccurred:"An unexpected error {PH1} occurred."},str_=i18n.i18n.registerUIStrings("resources/DatabaseModel.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class Database{constructor(e,a,t,s,i){this._model=e,this._id=a,this._domain=t,this._name=s,this._version=i}get id(){return this._id}get name(){return this._name}set name(e){this._name=e}get version(){return this._version}set version(e){this._version=e}get domain(){return this._domain}set domain(e){this._domain=e}async tableNames(){const{tableNames:e}=await this._model._agent.invoke_getDatabaseTableNames({databaseId:this._id})||[];return e.sort()}async executeSql(e,a,t){const s=await this._model._agent.invoke_executeSQL({databaseId:this._id,query:e}),i=s.getError()||null;if(i)return void t(i);const n=s.sqlError;if(!n)return void a(s.columnNames||[],s.values||[]);let r;r=n.message?n.message:2===n.code?i18nString(UIStrings.databaseNoLongerHasExpected):i18nString(UIStrings.anUnexpectedErrorSOccurred,{PH1:n.code}),t(r)}}export class DatabaseModel extends SDK.SDKModel.SDKModel{constructor(e){super(e),this._databases=[],this._agent=e.databaseAgent(),this.target().registerDatabaseDispatcher(new DatabaseDispatcher(this))}enable(){this._enabled||(this._agent.invoke_enable(),this._enabled=!0)}disable(){this._enabled&&(this._enabled=!1,this._databases=[],this._agent.invoke_disable(),this.dispatchEventToListeners(Events.DatabasesRemoved))}databases(){const e=[];for(const a of this._databases)e.push(a);return e}_addDatabase(e){this._databases.push(e),this.dispatchEventToListeners(Events.DatabaseAdded,e)}}SDK.SDKModel.SDKModel.register(DatabaseModel,SDK.SDKModel.Capability.DOM,!1);export var Events;!function(e){e.DatabaseAdded="DatabaseAdded",e.DatabasesRemoved="DatabasesRemoved"}(Events||(Events={}));export class DatabaseDispatcher{constructor(e){this._model=e}addDatabase({database:e}){this._model._addDatabase(new Database(this._model,e.id,e.domain,e.name,e.version))}}