import { delay as e, flush as i, put as t, select as r, take as l } from "redux-saga/effects"
import { getCoreBackend as o } from "../../../../backend"
import { apiCall as n } from "../../../../api/core"
import { apiSubscribeSaga as d } from "../../../lib"
import { userCustomNotification as a, NOTIFICATION_CODE as f } from "../../../notification"
import { getFileEntryById as c } from "../file.reducer"
import {
updateFileEntryClearedAll as m,
updateFileEntryClearedAllStatus as y,
updateFileEntry as s,
updateFileStats as p,
clearFileStats as h,
} from "../file.action"
import { CLEAR_ALL_STATUS as S, FILE_STATS_BUFFER_INTERVAL as u } from "../file.constants"
import { taskErrorSaga as C } from "../../../error"
import { fetchFileEntry as A } from "./file-entry.saga"
import { restoreCoreFile as F } from "../file.model"
export function* throttleFileStatsChanged(r) {
try {
for (;;) {
const o = yield l(r),
n = yield i(r)
let d = [o]
;(Array.isArray(n) && (d = n.reverse().concat(o)), yield t(p(d)), yield e(u))
}
} catch (e) {
yield C("throttleFileStatsChanged", e)
}
}
export function* fileSaga({ roomId: e, fileId: i, fileStatsChannel: l }) {
try {
const n = yield r(c, i),
a = o()
if (!n) throw new Error(`File entry with id ${i} is not found`)
if (n.pointer && !n.httpLink) {
const { httpLink: r, cleared: l } = (yield A(n.pointer, e)) || {}
r && (yield t(s({ id: i, httpLink: r, cleared: l })))
}
yield d(
[a.subscribeFileInfo, e, F(n)],
function* (e) {
yield t(l, { fileId: i, stats: e })
},
{ onCleanup: () => t(h({ fileId: i })) },
)
} catch (e) {
yield C("fileSaga", e)
}
}
export function* clearAllFilesHandler(e, i) {
try {
yield t(y(S.LOADING))
const r = o()
;(yield n(r.clearAllFiles, e),
i === e && (yield t(m(!0))),
yield t(a({ code: f.FILE_CLEAR_ALL_SUCCESS })),
yield t(y(S.SUCCESS)))
} catch (e) {
yield C("clearAllFilesHandler", e)
}
}