// hoanghapc/src/lib/productPage.ts
import { categories } from "../data/categories";
import { productList } from "../data/product-list";

export type ProductResult =
    | { type: "product_category"; data: any }
    | { type: "product_detail"; data: any };

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

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

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

    // DETAIL
    const product = productList.find(p => p.productUrl === url);
    if (product) {
        return { type: "product_detail", data: product };
    }

    return null;
}
