/**
 * Pagination Formatter.
 * @description Generates a range of page numbers for pagination, including ellipses for skipped pages.
 * @param {number} current - The current page number.
 * @param {number} page_size - The total number of pages.
 * @example
 * import paginate from '$lib/paginate';
 * const pages = paginate(1, 10);
 */
export default (current = 1, page_size = 10) => {
    const range = [];
    const rangeWithDots = [];

    let x = 0;

    for (let i = 1; i <= page_size; i++) {
        if (i == 1 || i == page_size || (i >= current - 1 && i < current + 1 + 1)) {
            range.push(i);
        }
    }

    for (const i of range) {
        if (x) {
            if (i - x === 2) {
                rangeWithDots.push(x + 1);
            } else if (i - x !== 1) {
                rangeWithDots.push("...");
            }
        }

        rangeWithDots.push(i);
        x = i;
    }

    return [
        ...rangeWithDots.map((e) => ({
            name: e,
            active: e === current,
        })),
    ];
};
