export const TODO_ID_PREFIX = "TODO-";
const TODO_ID_PATTERN = /^[a-f0-9]{8}$/i;
export function formatTodoId(id) {
    return `${TODO_ID_PREFIX}${id}`;
}
export function normalizeTodoId(id) {
    let trimmed = id.trim();
    if (trimmed.startsWith("#")) {
        trimmed = trimmed.slice(1);
    }
    if (trimmed.toUpperCase().startsWith(TODO_ID_PREFIX)) {
        trimmed = trimmed.slice(TODO_ID_PREFIX.length);
    }
    return trimmed;
}
export function validateTodoId(id) {
    const normalized = normalizeTodoId(id);
    if (!normalized || !TODO_ID_PATTERN.test(normalized)) {
        return { error: "Invalid todo id. Expected TODO-<hex>." };
    }
    return { id: normalized.toLowerCase() };
}
export function displayTodoId(id) {
    return formatTodoId(normalizeTodoId(id));
}
export function isTodoClosed(status) {
    return ["closed", "done"].includes(status.toLowerCase());
}
export function getTodoTitle(todo) {
    return todo.title || "(untitled)";
}
export function getTodoStatus(todo) {
    return todo.status || "open";
}
export function clearAssignmentIfClosed(todo) {
    if (isTodoClosed(getTodoStatus(todo))) {
        todo.assigned_to_session = undefined;
    }
}
export function sortTodos(todos) {
    return [...todos].sort((a, b) => {
        const aClosed = isTodoClosed(a.status);
        const bClosed = isTodoClosed(b.status);
        if (aClosed !== bClosed)
            return aClosed ? 1 : -1;
        const aAssigned = !aClosed && Boolean(a.assigned_to_session);
        const bAssigned = !bClosed && Boolean(b.assigned_to_session);
        if (aAssigned !== bAssigned)
            return aAssigned ? -1 : 1;
        return (a.created_at || "").localeCompare(b.created_at || "");
    });
}
export function splitTodosByAssignment(todos) {
    const assignedTodos = [];
    const openTodos = [];
    const closedTodos = [];
    for (const todo of todos) {
        if (isTodoClosed(getTodoStatus(todo))) {
            closedTodos.push(todo);
            continue;
        }
        if (todo.assigned_to_session) {
            assignedTodos.push(todo);
        }
        else {
            openTodos.push(todo);
        }
    }
    return { assignedTodos, openTodos, closedTodos };
}
//# sourceMappingURL=todos-model.js.map