import type { Schema } from "@/lib/schema.server"; export type ResolvedEntry = { key: string; data: { root: any; content: any[] }; params: Record; }; const TEMPLATE_PATTERNS: { key: string; prefix: string; param: string }[] = [ { key: "/products/*", prefix: "/products/", param: "handle" }, { key: "/collections/*", prefix: "/collections/", param: "handle" }, ]; export function resolveSchemaEntry( schema: Schema, route: string, ): ResolvedEntry | null { if (schema[route]) { return { key: route, data: schema[route], params: {} }; } for (const { key, prefix, param } of TEMPLATE_PATTERNS) { if (route.startsWith(prefix) && route.length > prefix.length && schema[key]) { const value = route.slice(prefix.length); return { key, data: applyParams(schema[key], { [param]: value }), params: { [param]: value }, }; } } return null; } export function templateKeyForRoute(route: string): string | null { for (const { key, prefix } of TEMPLATE_PATTERNS) { if (route.startsWith(prefix) && route.length > prefix.length) return key; } return null; } function applyParams( data: { root: any; content: any[] }, params: Record, ): { root: any; content: any[] } { const handle = params.handle; if (!handle) return data; const content = data.content.map((block) => { const props = block?.props ?? {}; if ("product" in props && (props.product == null || !props.product?.handle)) { return { ...block, props: { ...props, product: { handle } } }; } if ("collection" in props && (props.collection == null || !props.collection?.handle)) { return { ...block, props: { ...props, collection: { handle } } }; } return block; }); return { ...data, content }; }