import { File } from '@red/domain';

interface PageRange {
    startPage: number;
    endPage: number;
}

export function extractPageRanges(inputValue: string, file: File): PageRange[] {
    const value = inputValue.replace(/[^0-9-,]/g, '');
    return value.split(',').map(range => {
        const splitted = range.split('-');
        const startPage = parseInt(splitted[0], 10);
        const endPage = splitted.length > 1 ? parseInt(splitted[1], 10) : startPage;
        if (!startPage || !endPage || startPage > file.numberOfPages || endPage > file.numberOfPages) {
            throw new Error();
        }
        return {
            startPage,
            endPage,
        };
    });
}
