import { WorkflowFileStatus } from '../../files';

type StatusSorterItem = { key: WorkflowFileStatus } | WorkflowFileStatus | string;
type Sorter = Record<WorkflowFileStatus, number> & {
    byStatus: <T extends StatusSorterItem>(a: T, b: T) => number;
};

export const StatusSorter: Sorter = {
    NEW: 0,
    UNASSIGNED: 1,
    UNDER_REVIEW: 2,
    UNDER_APPROVAL: 3,
    APPROVED: 4,
    byStatus: (a: StatusSorterItem, b: StatusSorterItem): number => {
        if (typeof a !== typeof b) {
            throw TypeError('Used different types when calling StatusSorter.byStatus1');
        }

        const x = typeof a === 'string' ? (a as WorkflowFileStatus) : a.key;
        const y = typeof b === 'string' ? (b as WorkflowFileStatus) : b.key;
        return StatusSorter[x] - StatusSorter[y];
    },
};
