import { INITIAL_STATE as o } from "./uploads.constant"
import { computeUploadsDerived as e } from "./uploads.model"
import {
setUpload as t,
setBatchUploads as s,
updateUploadWithMetadata as a,
updateUpload as r,
updateUploadFileState as d,
startUploadingSend as p,
clearUpload as l,
setProcessingFlag as n,
uploadsHydrateEvt as u,
} from "./uploads.action"
export const uploadsReducer = (i = o, c) => {
switch (c.type) {
case t.type: {
const { uploadId: o, data: t, roomId: s } = c.payload,
a = i.rooms[s]
return e(
{
...i,
uploads: { ...i.uploads, [o]: t },
rooms: { ...i.rooms, [s]: a ? [...a, o] : [o] },
},
i,
)
}
case s.type: {
const o = c.payload,
t = { ...i.uploads },
s = { ...i.rooms }
for (let e = 0; e < o.length; e += 1) {
const { uploadId: a, data: r, roomId: d } = o[e]
;((t[a] = r), s[d] ? s[d] === i.rooms[d] && (s[d] = [...s[d]]) : (s[d] = []), s[d].push(a))
}
return e({ ...i, uploads: t, rooms: s }, i)
}
case a.type: {
const { fileId: o, data: t } = c.payload,
s = i.uploads[o]
return s
? e({ ...i, uploads: { ...i.uploads, [o]: { ...s, ...t, isMetaPrepared: !0 } } }, i)
: i
}
case r.type: {
const { fileId: o, data: t } = c.payload,
s = i.uploads[o]
return s ? e({ ...i, uploads: { ...i.uploads, [o]: { ...s, ...t } } }, i) : i
}
case d.type: {
const { id: o, updates: t } = c.payload,
s = i.uploads[o]
return s
? e(
{
...i,
uploads: {
...i.uploads,
[o]: {
...s,
isDownloading: !1,
path: t.path ?? null,
byteLength: t.byteLength ?? 0,
type: t.type ?? void 0,
name: t.name ?? "",
dimensions: t.dimensions ?? null,
isLinkPreview: !!t.previewData,
title: t.previewData?.title ?? "",
description: t.previewData?.description ?? "",
icon: t.previewData?.icon ?? void 0,
},
},
},
i,
)
: i
}
case p.type: {
const o = c.payload
let t = !1
const s = { ...i.uploads }
for (let e = 0; e < o.length; e += 1) {
const a = s[o[e]]
a && ((s[o[e]] = { ...a, isSending: !0 }), (t = !0))
}
return t ? e({ ...i, uploads: s }, i) : i
}
case l.type: {
const t = c.payload
if (!t) return o
if (!t.id && !t.roomId) return i
if (t.id) {
const { id: o } = t,
s = { ...i.uploads }
delete s[o]
let a = i.rooms
const r = Object.keys(i.rooms)
for (let e = 0; e < r.length; e += 1) {
const t = r[e],
s = i.rooms[t],
d = s.filter((e) => e !== o)
d.length !== s.length && (a === i.rooms && (a = { ...i.rooms }), (a[t] = d))
}
return e({ ...i, uploads: s, rooms: a }, i)
}
const { roomId: s } = t,
a = i.rooms[s]
if (!a) return i
const r = { ...i.uploads }
for (let o = 0; o < a.length; o += 1) delete r[a[o]]
const d = { ...i.rooms }
return (delete d[s], e({ ...i, uploads: r, rooms: d }, i))
}
case n.type:
return { ...i, processingUploads: c.payload }
case u.type: {
const { uploads: t, rooms: s } = c.payload
return e({ ...o, uploads: t, rooms: s }, i)
}
default:
return i
}
}