export const reorderElements = <T>(
    collection: T[],
    oldIndex: number,
    newIndex: number,
) => {
    if (oldIndex < newIndex) {
        return [
            ...collection.slice(0, oldIndex),
            ...collection.slice(oldIndex + 1, newIndex + 1),
            ...collection.slice(oldIndex, oldIndex + 1),
            ...collection.slice(newIndex + 1, collection.length),
        ];
    }
    if (oldIndex > newIndex) {
        return [
            ...collection.slice(0, newIndex),
            ...collection.slice(oldIndex, oldIndex + 1),
            ...collection.slice(newIndex, oldIndex),
            ...collection.slice(oldIndex + 1, collection.length),
        ];
    }

    return collection;
};

export const toPairs = <T>(collection: T[]): [T, T][] =>
    collection.flatMap((element, index, collection) => {
        if (!index) {
            return [];
        }

        const previousElement = collection[index - 1];
        // We just checked that index of the previous element is inside array bounds
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return [[previousElement!, element]];
    });
