export const TASK_PRIORITY_MODEL = "scheduling_v1" as const; export const STORED_TASK_PRIORITIES = [ "asap", "soon", "later", "not_planned", ] as const; export const TASK_PRIORITY_INPUTS = [ "asap", "soon", "in_turn", "null", "later", "not_planned", ] as const; export type StoredTaskPriority = (typeof STORED_TASK_PRIORITIES)[number]; export type TaskPriority = StoredTaskPriority | null; export type TaskPriorityInput = (typeof TASK_PRIORITY_INPUTS)[number]; export type TaskSchedulingBand = "asap" | "regular" | "later" | "excluded"; const PRIORITY_LABELS: Readonly> = { asap: "ASAP", soon: "Soon", later: "Later", not_planned: "Not planned", }; const PRIORITY_RANK: Readonly> = { asap: 0, soon: 1, later: 3, not_planned: 4, }; export function isStoredTaskPriority( priority: string, ): priority is StoredTaskPriority { return (STORED_TASK_PRIORITIES as readonly string[]).includes(priority); } export function isCurrentTaskPriority( priority: string | null, ): priority is TaskPriority { return priority === null || isStoredTaskPriority(priority); } export function parseTaskPriorityInput( input: string | null | undefined, ): TaskPriority { if (input === null || input === undefined) return null; if (input === "in_turn" || input === "null") return null; if (isStoredTaskPriority(input)) return input; throw new Error( `Unknown task priority "${input}". Expected one of: ${TASK_PRIORITY_INPUTS.join(", ")}. Both in_turn and null persist the normal ordering as priority: null.`, ); } export function taskPriorityLabel(priority: string | null): string { if (priority === null) return "In turn"; if (isStoredTaskPriority(priority)) return PRIORITY_LABELS[priority]; return priority.replaceAll("_", " "); } export function taskCardPriorityLabel( priority: string | null, archived = false, ): string | null { if (priority === null || (archived && priority === "not_planned")) { return null; } if (priority === "not_planned") return null; return taskPriorityLabel(priority); } export function taskSchedulingBand(priority: TaskPriority): TaskSchedulingBand { if (priority === "asap") return "asap"; if (priority === "later") return "later"; if (priority === "not_planned") return "excluded"; return "regular"; } export function isTaskPriorityEligible(priority: TaskPriority): boolean { return taskSchedulingBand(priority) !== "excluded"; } function taskPriorityRank(priority: TaskPriority): number { return priority === null ? 2 : PRIORITY_RANK[priority]; } interface PrioritizedTaskLike { data: { id: string; priority: string | null; created_at: string; }; } /** * Priority ordering uses immutable creation metadata and a stable ID fallback, * so rendering never needs to rewrite task files merely to retain card order. */ export function sortTasksByPriority( tasks: readonly T[], ): T[] { return [...tasks].sort((left, right) => { const leftPriority = isCurrentTaskPriority(left.data.priority) ? left.data.priority : null; const rightPriority = isCurrentTaskPriority(right.data.priority) ? right.data.priority : null; return ( taskPriorityRank(leftPriority) - taskPriorityRank(rightPriority) || left.data.created_at.localeCompare(right.data.created_at) || left.data.id.localeCompare(right.data.id) ); }); }