const e = require("ready-resource"),
t = require("scope-lock"),
{ KeetContractError: r } = require("../core/contract/errors"),
s = 1e4,
i = 12096e5,
a = {
INVALID_INVITATION_TYPE: "INVALID_INVITATION_TYPE",
UNKNOWN_EXPIRATION: "UNKNOWN_EXPIRATION",
INVALID_INVITE: "INVALID_INVITE",
INVALID_CONTACT_INFO: "INVALID_CONTACT_INFO",
MODERATORS_ONLY: "MODERATORS_ONLY",
UNEXPECTED_ERROR: "UNEXPECTED_ERROR",
INVALID_BUILD_TYPE: "INVALID_BUILD_TYPE",
}
class o extends e {
constructor(e, r) {
;(super(),
(this.keet = e),
(this.errors = []),
(this._flushLock = new t({ debounce: !0 })),
(this._flushInterval = r.errorFlushInterval || s),
(this._flushTimerDisabled = null === r.errorFlushTimer),
(this._flushTimer = null),
(this._gcMaxAge = r.errorGcMaxAge || i),
this.ready().catch(e.onerror))
}
async _open() {
;(await this.keet.ready(), await this._gc())
}
async _close() {
;((this._flushTimerDisabled = !0), this._resetTimeout(), await this._flush())
}
add(e) {
;(this.errors.push(e),
this.keet.emit("caught-error", e),
this._flushTimerDisabled || this._scheduleFlush())
}
normalize(e, t = {}) {
return n.isKeetError(e)
? e
: r.isContractError(e)
? ((e.timestamp = Date.now()), e)
: n.fromError(e, t)
}
_scheduleFlush() {
this._flushTimer || (this._flushTimer = setTimeout(this._flush.bind(this), this._flushInterval))
}
_resetTimeout() {
this._flushTimer && (clearTimeout(this._flushTimer), (this._flushTimer = null))
}
debugPauseFlush() {
this._resetTimeout()
}
debugResumeFlush() {
this._scheduleFlush()
}
async _flush() {
if (!this.keet.readOnly && (await this._flushLock.lock()))
try {
const e = await this.keet.internalEnterLocalTransaction()
try {
const t = [...this.errors]
this.errors = []
for (const r of t)
await e.insert("@local/errors", {
code: r.code,
message: r.message,
stack: r.stack,
details: r.details,
timestamp: r.timestamp,
})
await e.flush()
} finally {
await this.keet.internalExitLocalTransaction()
}
} finally {
;(this._flushLock.unlock(), this._resetTimeout())
}
}
async _gc() {
if (this.closing) return
const e = Date.now() - this._gcMaxAge,
t = await this.keet.local.find("@local/errors", { lte: { timestamp: e } }).toArray(),
r = await this.keet.internalEnterLocalTransaction()
try {
for (const e of t) await r.delete("@local/errors", e)
await r.flush()
} finally {
await this.keet.internalExitLocalTransaction()
}
}
}
class n extends Error {
constructor(e, t, r = {}, s = this.constructor) {
;(super(t ? `${e}: ${t}` : e),
(this.isKeetError = !0),
(this.code = e),
(this.details = r),
(this.timestamp = Date.now()),
Error.captureStackTrace && Error.captureStackTrace(this, s))
}
static isKeetError(e) {
return e?.isKeetError
}
static fromError(e, t = {}) {
const r = c(e, t)
return (
(e.code = r),
(e.message = `${r}: ${e.message}`),
(e.timestamp = Date.now()),
e.stack || (e.stack = ""),
(e.details = {}),
t.roomKey && (e.details.roomKey = t.roomKey),
t.coreKey && (e.details.coreKey = t.coreKey),
e
)
}
static INVALID_INVITATION_TYPE(
e = "Attempting to use a device sync invitation as a room invitation",
t = {},
) {
return new n(a.INVALID_INVITATION_TYPE, e, t, n.INVALID_INVITATION_TYPE)
}
static UNKNOWN_EXPIRATION(e = "Unknown expiration", t = {}) {
return new n(a.UNKNOWN_EXPIRATION, e, t, n.UNKNOWN_EXPIRATION)
}
static INVALID_INVITE(e = "Invalid invite", t = {}) {
return new n(a.INVALID_INVITE, e, t, n.INVALID_INVITE)
}
static INVALID_CONTACT_INFO(e = "Invalid contact info", t = {}) {
return new n(a.INVALID_CONTACT_INFO, e, t, n.INVALID_CONTACT_INFO)
}
static MODERATORS_ONLY(e = "", t = {}) {
return new n(a.MODERATORS_ONLY, e, t, n.MODERATORS_ONLY)
}
static UNEXPECTED_ERROR(e = "", t = {}) {
return new n(a.UNEXPECTED_ERROR, e, t, n.UNEXPECTED_ERROR)
}
static INVALID_BUILD_TYPE(e = "", t = {}) {
return new n(a.INVALID_BUILD_TYPE, e, t, n.INVALID_BUILD_TYPE)
}
}
function c(e, t) {
return e.cause?.code
? e.cause.code
: t.prefix
? t.prefix
: t.roomKey
? "UNEXPECTED_ROOM_ERROR"
: "UNEXPECTED_ERROR"
}
module.exports = { ERRORS: a, KeetErrorV2: n, KeetErrorManager: o }