import { generateTempId as e } from "./chat-file.model"
import {
updateChatPendingFile as t,
setIsUploadingFileParsing as r,
setChatPendingFileError as p,
removeChatPendingFile as d,
addChatPendingFileSuccess as s,
addChatPendingFiles as o,
} from "./chat-file.action"
export const chatFileUploadReducer = (y, a) => {
switch (a.type) {
case o.type: {
const e = a.payload.map(({ tempId: e }) => e),
t = a.payload.reduce((e, { file: t, tempId: r }) => {
const { name: p, type: d, size: s, byteLength: o, path: y, id: a, url: m } = t,
l = { name: p, type: d, byteLength: s || o || 0, path: y, id: a, url: m }
return ((e[r] = l), e)
}, {}),
r = a.payload.reduce(
(e, { tempId: t }) => (
(e[t] = { progress: 0, bytesAdded: 0, byteLength: 0, completed: !1 }),
e
),
{},
)
return {
...y,
tempIds: [...y.tempIds, ...e],
byTempId: { ...y.byTempId, ...t },
progressByTempId: { ...y.progressByTempId, ...r },
}
}
case r.type:
return { ...y, isUploadingFileParsing: a.payload }
case t.type: {
const { file: t, bytesAdded: r, byteLength: p, completed: d } = a.payload,
s = e(t),
o = p > 0 ? r / p : 0
return {
...y,
progressByTempId: {
...y.progressByTempId,
[s]: { bytesAdded: r, byteLength: p, progress: 100 * (d ? 1 : o), completed: d },
},
}
}
case s.type: {
const { file: t, result: r } = a.payload,
p = e(t)
return { ...y, resultByTempId: { ...y.resultByTempId, [p]: r } }
}
case d.type: {
const e = a.payload,
t = y.tempIds.filter((t) => t !== e),
r = { ...y.byTempId }
delete r[e]
const p = { ...y.progressByTempId }
delete p[e]
const d = { ...y.errorByTempId }
return (delete d[e], { ...y, tempIds: t, byTempId: r, progressByTempId: p, errorByTempId: d })
}
case p.type: {
const { file: t, error: r } = a.payload,
p = e(t)
return { ...y, errorByTempId: { ...y.errorByTempId, [p]: r } }
}
default:
return y
}
}