import { and, asc, eq, isNull } from 'drizzle-orm'; import { db, tables } from './db/index.ts'; import type { MatterPosition } from '#lib/matter.ts'; import type { ManuscriptNodeKind } from '#lib/manuscript-tree.ts'; export type { ManuscriptNodeKind } from '#lib/manuscript-tree.ts'; export type OrderedKind = ManuscriptNodeKind | 'note' | 'storyline' | 'plot_card' | 'note_type'; export type LiveOrderedKind = Exclude; export type SiblingScope = ReturnType; export type OrderedRow = { id: string; ordinal: number }; export type OrderedExecutor = Pick; type LiveOrderedTable = | typeof tables.book | typeof tables.part | typeof tables.chapter | typeof tables.scene | typeof tables.note | typeof tables.storyline | typeof tables.plotCard; export function orderedTable(kind: LiveOrderedKind): LiveOrderedTable; export function orderedTable(kind: 'note_type'): typeof tables.noteType; export function orderedTable(kind: OrderedKind): LiveOrderedTable | typeof tables.noteType; export function orderedTable(kind: OrderedKind) { switch (kind) { case 'book': return tables.book; case 'part': return tables.part; case 'chapter': return tables.chapter; case 'scene': return tables.scene; case 'note': return tables.note; case 'storyline': return tables.storyline; case 'plot_card': return tables.plotCard; case 'note_type': return tables.noteType; default: throw new RangeError('Unknown ordered item type.'); } } export function chapterScope( bookId: string, partId: string | null, matter: MatterPosition | null = null ): SiblingScope { return and( eq(tables.chapter.bookId, bookId), partId ? eq(tables.chapter.partId, partId) : isNull(tables.chapter.partId), matter ? eq(tables.chapter.matter, matter) : isNull(tables.chapter.matter) ); } export function listOrderedSiblings( kind: LiveOrderedKind, scope: SiblingScope, executor: OrderedExecutor = db ) { const table = orderedTable(kind); return executor .select({ id: table.id, ordinal: table.ordinal }) .from(table) .where(and(scope, isNull(table.deletedAt))) .orderBy(asc(table.ordinal), asc(table.createdAt), asc(table.id)) .all(); } export function writeOrder( kind: OrderedKind, ids: readonly string[], options: { previous?: readonly OrderedRow[]; executor?: OrderedExecutor } = {} ) { const table = orderedTable(kind); const executor = options.executor ?? db; const previous = options.previous; const ordinals = previous ? new Map(previous.map((row) => [row.id, row.ordinal])) : null; for (const [ordinal, id] of ids.entries()) { if (ordinals?.get(id) === ordinal) continue; executor.update(table).set({ ordinal }).where(eq(table.id, id)).run(); } } /** Drops foreign and duplicate requests, then appends omitted rows in their current order. */ export function planRequestedOrder( rows: readonly OrderedRow[], orderedIds: readonly string[] ): { ids: string[]; changed: boolean } { const own = new Set(rows.map((row) => row.id)); const requested = [...new Set(orderedIds.filter((id) => own.has(id)))]; const ids = [...requested, ...rows.map((row) => row.id).filter((id) => !requested.includes(id))]; const changed = rows.some((row, index) => row.id !== ids[index] || row.ordinal !== index); return { ids, changed }; }