import e from "b4a"
import o from "hypercore-crypto"
const t = e.from("e7709180eb605686db58486bae2d00e63993ee6649d6d4bd338135ebd433830c", "hex")
let l = null,
a = null,
r = null,
s = () => {}
export function registerDebugMode({ store: e, core: o, call: t }, g) {
;((l = e),
(a = o),
(r = t),
g && (s = g),
(globalThis._debug = { unlock: n, unlocked: !1 }),
globalThis.Pear?.config?.alias && "keet" !== globalThis.Pear?.config?.alias && u())
}
function n(l) {
if (globalThis._debug.unlocked) console.log("Already unlocked debug mode")
else {
if (!e.equals(o.hash(e.from(l)), t)) throw new Error("You shall not pass.")
;(console.log("You shall pass."), u())
}
}
function u() {
;((globalThis._debug.unlocked = !0),
(globalThis._debug.require = function (e) {
return require(e)
}),
l && b(l),
a && c(a),
r && d(r),
m(),
s())
}
function g(e) {
globalThis._debug.core = {
getKeetCore: () => e,
getCurrentRoom: async () => {
const o = await e.getRecentRooms(),
t = globalThis._debug.store.getState().room.main.roomId
return o.rooms.find((e) => e.roomId === t)
},
getMemberInfo: async (o) => {
const t = await globalThis._debug.core.getCurrentRoom()
return t ? (await e.getSearchMembers(t.roomId, o))[0] : null
},
}
}
function i(e) {
globalThis._debug.core = {
getKeetCore: () => e.keet,
getAutobases: () => [...e.keet.rooms.values()].map((e) => e.base),
getActiveAutobases: () => [...e.keet.activeRooms].map((e) => e.base),
getCurrentRoom: () => {
const o = e.keet
let t = null
for (const e of o.activeRooms.values()) (!t || e.refs > t.refs) && (t = e)
return t
},
getMemberInfo: async (e) => {
const o = globalThis._debug.core.getCurrentRoom()
return (
await o.db.find("@keet/members-by-member-name", { gte: { name: e }, limit: 1 }).toArray()
)[0]
},
}
}
function c(e) {
globalThis._debug.store ? g(e) : i(e)
}
function b(e) {
globalThis._debug.store = { dispatch: (o) => e.dispatch(o), getState: () => e.getState() }
}
function d(e) {
globalThis._debug.call = {
getCall: () => {
const o = [...e._callsById.values()][0],
t = o?.value
return { call: e, activeCall: t }
},
}
}
function m() {
globalThis._debug.helpers = {
resetLastSeenSeq: (e) => {
const o = `chat/last-seen-seq_${globalThis._debug.core.getCurrentRoom().id}`
globalThis.localStorage.setItem(o, e || 0)
},
}
}