import {
put as o,
select as e,
takeLatest as r,
takeEvery as i,
spawn as t,
getContext as m,
} from "redux-saga/effects"
import {
setError as d,
logError as s,
logInfo as l,
logWarning as a,
setWarning as y,
setInfo as c,
errorBackendSaga as n,
errorNotificationHandler as p,
} from "../../error"
import { getRoomItemById as f, getRoomTitle as g, setRoomShowWarning as u } from "../../room"
import { getAppCurrentRoomId as E } from "../app.reducer"
import { apiSubscribeSaga as I, safeCall as b } from "../../lib"
import { APP_CONTEXT_KEY as C, ENV_CONTEXT_KEY as O } from "../../constants"
import { getCoreBackend as S } from "../../../backend"
import { getStatsUnlocked as k } from "../../stats"
import {
NOTIFICATION_CODE as T,
userCustomNotification as W,
userWarnNotification as x,
} from "../../notification"
import { getPreferencesCoreWarningsEnabled as A } from "../../preferences"
import { ERROR_POSIX as G } from "../../../api/core"
function* P({ type: r, payload: i }) {
const t = i.roomId || (yield e(E)),
m = yield e(f(t)),
n = { ...i, roomId: t, roomTitle: m?.title },
{ message: p, errorCode: g } = i
g === G.ENOSPC || g === G.EIO
? (yield o(y(n)), yield o(W({ code: T.STORAGE_LOW_SPACE, message: p })))
: r === s.type
? yield o(d(n))
: r === a.type
? yield o(y(n))
: r === l.type && (yield o(c(n)))
}
function* $() {
const { onError: r } = yield m(O),
{ consoleWarn: i } = yield m(C)
r &&
(yield I([S().subscribeErrors], function* (t) {
if (t.isContractError) return void (i && i(t))
yield b(r, t, { source: "core" })
const m = yield e(k),
d = yield e(A),
s = t.details?.roomId
if (m) {
s && (yield o(u({ roomId: s, showWarning: !0 })))
const r = yield e(g(s))
;(yield o(y({ message: t.message, stack: t.stack, roomId: s, roomTitle: r })),
d &&
(yield o(
x({
message: r ? `Group ${r}: ${t.message}` : t.message,
compactMode: !0,
roomId: s,
}),
)))
}
}))
}
export function* appErrorSaga() {
;(yield r([d], p), yield i([s, a, l], P), yield t($), yield t(n))
}