import { select as e, put as r } from "redux-saga/effects"
import { getAvatarFileIds as t, makeGetFileEntriesByIds as l } from "../file.reducer"
import {
updateFileEntryListClearedSuccess as i,
updateFileEntryCleared as a,
updateFileEntryListCleared as o,
} from "../file.action"
import { taskErrorSaga as d } from "../../../error"
import {
NOTIFICATION_CODE as n,
userCustomNotification as c,
userInfoNotification as f,
} from "../../../notification"
import { getCoreBackend as s } from "../../../../backend"
import { apiCall as m } from "../../../../api/core"
import { fetchFileEntry as p } from "./file-entry.saga"
export function* mutateFileEntryCleared({ roomId: e, filePointer: r, cleared: t }) {
const l = s(),
i = t ? l.clearFile : l.unclearFile
;(yield m(i, e, { pointer: r }), t || (yield p(r, e)))
}
export function* updateFileEntryClearedHandler(s, m) {
let p = []
a.match(m) ? (p = [m?.payload?.fileId]) : o.match(m) && (p = m?.payload?.fileIds)
const { cleared: y } = m?.payload || {}
try {
const a = yield e(t)
let o = []
if ((y && (o = p.filter((e) => a.includes(e))), o.length > 0)) {
const e = p.length
if (
(yield r(
f({
title: "Failed to clear file",
message:
e > 1
? "Couldn't clear some files because they're used as group avatars"
: "Can't clear file because it's used as group avatar",
}),
),
1 === e)
)
return
p.filter((e) => -1 !== o.indexOf(e))
}
const d = yield e(l(p))
if (0 === d.length)
return void (yield r(
f({ title: "Failed to clear file", message: "File is not yet processed" }),
))
let m = 0
for (const e of d)
e.cleared !== y &&
((m += e.byteLength || 0),
e?.preview?.large?.pointer &&
(yield mutateFileEntryCleared({
roomId: s,
filePointer: e?.preview?.large.pointer,
cleared: y,
}),
(m += e?.preview?.large?.byteLength || 0)),
yield mutateFileEntryCleared({ roomId: s, filePointer: e.pointer, cleared: y }))
;(y && (yield r(c({ code: n.FILE_CLEAR_SUCCESS, bytesCleared: m }))),
yield r(i({ fileIds: p, cleared: y })))
} catch (e) {
yield d("clearFileHandler", e)
}
}