import { put as r, select as e, getContext as o } from "redux-saga/effects"
import { taskError as t, getLastError as i, logWarning as s } from "./error.reducer"
import { userErrorNotification as a } from "../notification/notification.reducer"
import { apiSubscribeSaga as n, safeCall as c } from "../lib"
import { APP_CONTEXT_KEY as l, ENV_CONTEXT_KEY as f } from "../constants"
import { isErrorObject as m } from "./error.model"
import { ERROR_TYPES as d } from "./error.constants"
import { getErrorBackend as p } from "../../backend"
export function* taskErrorSaga(e, i, a) {
const { isTest: n, onError: d } = yield o(f),
{ consoleWarn: p } = yield o(l)
if ("string" != typeof i && i?.isContractError) return void (p && p(i))
if ((n || console.error(i || e), a?.logOnly)) return
if (a?.logWarn) {
if ("string" == typeof i) {
const o = `Failed to execute "${e}" task: ${i}`
yield r(s({ message: o }))
} else {
const o = `Failed to execute "${e}" task: ${i?.message}`
yield r(s({ message: o, stack: i?.stack || "" }))
}
return
}
let g
;((g = "string" == typeof i ? t(e, { message: i }) : m(i) ? t(e, i) : t(e)),
yield r(g),
yield c(d, g.payload, { isCritical: a?.isCritical }))
}
export function* errorBackendSaga() {
yield n([p().subscribe], (r) => taskErrorSaga("keet-backend", r))
}
export function* errorNotificationHandler() {
const o = yield e(i),
t = 1 === o.grouped || o.notifyAll
o?.type === d.ERROR && t && (yield r(a({ message: o.message, stack: o.stack })))
}