/** * Discriminated selection algebra used by the OSS browser. Splitting the * shape into a tagged union (instead of two parallel sets) keeps the * "checked file" vs. "checked folder" distinction visible at every call * site without bug-prone implicit-conversion paths. */ export type OssSelectedItem = | { type: "file"; ossKey: string; size: number } | { type: "folder"; prefix: string }; export type OssSelection = OssSelectedItem[]; export function isFileSelected(sel: OssSelection, ossKey: string): boolean { return sel.some((s) => s.type === "file" && s.ossKey === ossKey); } export function isFolderSelected(sel: OssSelection, prefix: string): boolean { return sel.some((s) => s.type === "folder" && s.prefix === prefix); } export function toggleFile( sel: OssSelection, file: { ossKey: string; size: number }, ): OssSelection { if (isFileSelected(sel, file.ossKey)) { return sel.filter((s) => !(s.type === "file" && s.ossKey === file.ossKey)); } return [...sel, { type: "file", ossKey: file.ossKey, size: file.size }]; } export function toggleFolder( sel: OssSelection, folder: { prefix: string }, ): OssSelection { if (isFolderSelected(sel, folder.prefix)) { return sel.filter( (s) => !(s.type === "folder" && s.prefix === folder.prefix), ); } return [...sel, { type: "folder", prefix: folder.prefix }]; } /** * Split a selection into the two payloads consumers need at submission time: * an explicit ossKey list (files), and the prefixes that need to go through * `expandPrefixes()`. Order within each list is preserved so the UI can * render a stable preview. */ export function partitionSelection(sel: OssSelection): { fileKeys: string[]; folderPrefixes: string[]; } { const fileKeys: string[] = []; const folderPrefixes: string[] = []; for (const item of sel) { if (item.type === "file") fileKeys.push(item.ossKey); else folderPrefixes.push(item.prefix); } return { fileKeys, folderPrefixes }; }