const i = require("b4a"),
{ KeetSchema: e, PluginSchema: t } = require("@holepunchto/keet-core-schemas"),
n = new Map([
["@holepunchto/kv-store", require("../../plugins/kv-store/apply.js")],
["@holepunchto/word-count", require("../../plugins/word-count/apply.js")],
["@holepunchto/private-profile", require("../../plugins/private-profile/apply.js")],
])
module.exports = class {
static Operations = new Set(e.AllOperations.map((i) => i.TYPE))
constructor(i) {
;((this.view = i),
(this.internal = null),
(this.bee = null),
(this._plugins = new Map()),
(this._pluginsEncoding = t.Encoding))
}
initialize() {
;((this.internal = this.view.internal), (this.bee = this.view.bee))
}
async load() {
const e = await this.internal.listPlugins()
for (let t = 0; t < e.length; t++) {
const { name: s } = e[t],
l = n.get(s)
if (!l || this._plugins.has(s)) continue
const o = this._pluginsEncoding.sub(i.from([t])).prefix,
p = new l(this.view, o)
this._plugins.set(s, { plugin: p, name: s, prefix: o })
}
}
list() {
return [...this._plugins.values()]
}
get(i) {
const e = this._plugins.get(i)
return e ? e.plugin : null
}
async autobaseApply(i, e) {
await this.load()
for (const { plugin: t } of this.list()) await t.apply(i, e)
}
}