refactor: extract parent resolution

This commit is contained in:
Carlos Valente
2026-02-13 19:59:57 +01:00
committed by Carlos Valente
parent 80a3b04493
commit 5777ef3532
4 changed files with 71 additions and 19 deletions
@@ -382,6 +382,32 @@ export function getInsertAfterId(
return insertionList[atIndex - 1];
}
type ResolveInsertParentOptions = {
parent?: EntryId | null;
after?: EntryId;
before?: EntryId;
};
/**
* Resolves the parent ID for an insertion.
* Uses explicit parent first, then infers from sibling references.
*/
export function resolveInsertParent(rundown: Rundown, options: ResolveInsertParentOptions): EntryId | null {
if (options.parent) {
return options.parent;
}
const referenceId = options.after ?? options.before;
if (!referenceId) return null;
const maybeSibling = rundown.entries[referenceId];
if (maybeSibling && 'parent' in maybeSibling && maybeSibling.parent) {
return maybeSibling.parent;
}
return null;
}
/**
* Add entry to rundown, mutates the rundown in place.
* Handles the following cases: