// src/lib/articlePage.ts
import { categories } from "@/data/categories";
import { articleList } from "@/data/articles";

export type ArticleResult =
    | { type: "article_home"; data: any }
    | { type: "article_category"; data: any }
    | { type: "article_detail"; data: { slug: string } };

export function resolveArticlePage(slug: string): ArticleResult | null {
    const url = "/" + slug;

    // HOME
    if (url === "/tin-tuc") {
        return { type: "article_home", data: null };
    }

    // CATEGORY
    const cats = categories.article.all_category.article;
    for (const parent of cats) {
        if (parent.url === url) {
            return { type: "article_category", data: parent };
        }

        for (const child of parent.children ?? []) {
            if (child.url === url) {
                return { type: "article_category", data: child };
            }
        }
    }

    // DETAIL
    const allArticles = articleList.flatMap(article => article.list);
    for (const article of allArticles) {
        if (article.url === url) {
            return { type: "article_detail", data: { slug } };
        }
    }    
}
