/**
 * Resolve a public URL path to the collection + slug it addresses, using the
 * same precedence as the `/resolve` endpoint:
 *   1. root → first prefix-less page collection, slug `home`
 *   2. a collection whose `urlPrefix` matches the leading segments
 *   3. a prefix-less page collection (single-segment fallback → `pages`)
 *   4. `<collection>/<slug…>` literal fallback for multi-segment paths
 */
export function matchPathToCollection(config, pathParam) {
    const segments = pathParam
        .replace(/^\/|\/$/g, '')
        .split('/')
        .filter(Boolean);
    const configCollections = config?.collections ?? {};
    const collectionDefs = Object.values(configCollections);
    let matchedCollection = null;
    let docSlug = null;
    if (segments.length === 0) {
        const pageCol = collectionDefs.find((c) => c.type === 'page' && !(c.urlPrefix ?? '').replace(/^\/|\/$/g, ''));
        matchedCollection = pageCol?.slug ?? 'pages';
        docSlug = 'home';
    }
    else {
        for (const col of collectionDefs) {
            const prefix = (col.urlPrefix ?? col.slug ?? '').replace(/^\/|\/$/g, '');
            if (prefix && segments.length >= 2) {
                const prefixParts = prefix.split('/');
                const pathPrefix = segments.slice(0, prefixParts.length).join('/');
                if (pathPrefix === prefix) {
                    matchedCollection = col.slug;
                    docSlug = segments.slice(prefixParts.length).join('/');
                    break;
                }
            }
            else if (!prefix && col.type === 'page') {
                matchedCollection = col.slug;
                docSlug = segments.join('/');
            }
        }
        if (!matchedCollection && segments.length === 1) {
            matchedCollection = 'pages';
            docSlug = segments[0];
        }
        if (!matchedCollection && segments.length >= 2) {
            matchedCollection = segments[0];
            docSlug = segments.slice(1).join('/');
        }
    }
    if (!matchedCollection || !docSlug)
        return null;
    return { collection: matchedCollection, slug: docSlug, isRoot: segments.length === 0 };
}
//# sourceMappingURL=resolve-path.js.map