import type { DocBlock, DocPage, DocSection } from '@/docs/types';
import type { UserRole } from '@/types/auth';

export function section(
    id: string,
    heading: string,
    blocks: DocBlock[],
): DocSection {
    return { id, heading, blocks };
}

export function p(text: string): DocBlock {
    return { type: 'paragraph', text };
}

export function list(items: string[]): DocBlock {
    return { type: 'list', items };
}

export function callout(
    variant: 'note' | 'tip' | 'warning',
    text: string,
    title?: string,
): DocBlock {
    return { type: 'callout', variant, text, title };
}

export function docPage(
    slug: string,
    title: string,
    description: string,
    sections: DocSection[],
    options?: {
        roles?: UserRole[];
        relatedAppRoute?: string;
    },
): DocPage {
    return {
        slug,
        title,
        description,
        sections,
        ...options,
    };
}

export function pageAccessible(page: DocPage, role?: UserRole): boolean {
    if (!page.roles || page.roles.length === 0) {
        return true;
    }

    if (!role) {
        return false;
    }

    return page.roles.includes(role);
}

export function filterPagesByRole(
    pages: DocPage[],
    role?: UserRole,
): DocPage[] {
    return pages.filter((page) => pageAccessible(page, role));
}
