// ---------------------------------------------------------------------------
// Legacy registry (backwards-compatible)
// ---------------------------------------------------------------------------
const TYPE_REGISTRY = new Map();
/** Register a Schema.org type mapping for a collection. */
export function registerSchemaType(collection, mapping) {
    TYPE_REGISTRY.set(collection, mapping);
}
/** Get the Schema.org mapping for a collection. */
export function getSchemaType(collection) {
    return TYPE_REGISTRY.get(collection);
}
/** Auto-generate JSON-LD structured data from a document. */
export function generateJsonLd(collection, data, baseUrl) {
    const mapping = TYPE_REGISTRY.get(collection);
    if (!mapping)
        return null;
    const jsonLd = {
        '@context': 'https://schema.org',
        '@type': mapping.schemaType,
    };
    for (const [schemaField, documentField] of Object.entries(mapping.fieldMappings)) {
        const value = data[documentField];
        if (value !== undefined && value !== null) {
            jsonLd[schemaField] = value;
        }
    }
    if (baseUrl && data['slug']) {
        jsonLd['url'] = `${baseUrl}/${data['slug']}`;
    }
    return jsonLd;
}
/** Render a JSON-LD script tag for embedding in HTML. */
export function renderJsonLdScript(jsonLd) {
    return `<script type="application/ld+json">${JSON.stringify(jsonLd)}</script>`;
}
// ---------------------------------------------------------------------------
// Graph builder helpers
// ---------------------------------------------------------------------------
function slugify(text) {
    return text
        .toLowerCase()
        .replace(/[^a-z0-9]+/g, '-')
        .replace(/^-|-$/g, '');
}
function stripTrailingSlash(url) {
    return url.endsWith('/') ? url.slice(0, -1) : url;
}
function resolveWebPageType(page) {
    const hasFaq = page.faqItems && page.faqItems.length > 0;
    switch (page.type) {
        case 'collection':
            return 'CollectionPage';
        case 'profile':
            return 'ProfilePage';
        case 'about':
            return 'AboutPage';
        case 'contact':
            return 'ContactPage';
        case 'search':
            return ['CollectionPage', 'SearchResultsPage'];
        case 'faq':
            return hasFaq ? ['WebPage', 'FAQPage'] : 'WebPage';
        default:
            return hasFaq ? ['WebPage', 'FAQPage'] : 'WebPage';
    }
}
function isArticleType(type) {
    return type === 'post' || type === 'article' || type === 'blog';
}
// ---------------------------------------------------------------------------
// Individual node builders
// ---------------------------------------------------------------------------
/** Build the WebSite node with optional SearchAction. */
export function buildWebSiteSchema(site) {
    const siteUrl = stripTrailingSlash(site.siteUrl);
    const node = {
        '@type': 'WebSite',
        '@id': `${siteUrl}/#website`,
        url: siteUrl,
        name: site.siteName,
        publisher: { '@id': `${siteUrl}/#organization` },
    };
    if (site.siteDescription) {
        node.description = site.siteDescription;
    }
    if (site.defaultLanguage) {
        node.inLanguage = site.defaultLanguage;
    }
    if (site.searchUrlTemplate) {
        node.potentialAction = {
            '@type': 'SearchAction',
            target: {
                '@type': 'EntryPoint',
                urlTemplate: site.searchUrlTemplate,
            },
            'query-input': 'required name=search_term_string',
        };
    }
    return node;
}
/** Build the Organization node. */
export function buildOrganizationSchema(site) {
    const siteUrl = stripTrailingSlash(site.siteUrl);
    const orgType = site.organizationType ?? 'Organization';
    const node = {
        '@type': orgType,
        '@id': `${siteUrl}/#organization`,
        name: site.organizationName ?? site.siteName,
        url: siteUrl,
    };
    if (site.organizationLogo) {
        node.logo = {
            '@type': 'ImageObject',
            '@id': site.organizationLogo,
            url: site.organizationLogo,
            contentUrl: site.organizationLogo,
        };
        node.image = { '@id': site.organizationLogo };
    }
    if (site.organizationSameAs && site.organizationSameAs.length > 0) {
        node.sameAs = site.organizationSameAs;
    }
    return node;
}
/** Build the WebPage node. */
export function buildWebPageSchema(site, page) {
    const siteUrl = stripTrailingSlash(site.siteUrl);
    const node = {
        '@type': resolveWebPageType(page),
        '@id': page.url,
        url: page.url,
        name: page.title,
        isPartOf: { '@id': `${siteUrl}/#website` },
    };
    if (page.description) {
        node.description = page.description;
    }
    if (page.datePublished) {
        node.datePublished = page.datePublished;
    }
    if (page.dateModified) {
        node.dateModified = page.dateModified;
    }
    const lang = page.language ?? site.defaultLanguage;
    if (lang) {
        node.inLanguage = lang;
    }
    if (page.breadcrumbs && page.breadcrumbs.length > 0) {
        node.breadcrumb = { '@id': `${page.url}#breadcrumb` };
    }
    if (page.featuredImage) {
        node.primaryImageOfPage = { '@id': page.featuredImage.url };
        node.thumbnailUrl = page.featuredImage.url;
    }
    if (isArticleType(page.type)) {
        node.potentialAction = [{ '@type': 'ReadAction', target: [page.url] }];
    }
    const hasFaq = page.faqItems && page.faqItems.length > 0;
    if (hasFaq) {
        node.mainEntity = page.faqItems.map((item) => ({
            '@type': 'Question',
            name: item.question,
            acceptedAnswer: { '@type': 'Answer', text: item.answer },
        }));
    }
    return node;
}
/** Build the Article / BlogPosting node. */
export function buildArticleSchema(site, page) {
    const siteUrl = stripTrailingSlash(site.siteUrl);
    const articleType = page.type === 'blog' ? 'BlogPosting' : 'Article';
    const node = {
        '@type': articleType,
        '@id': `${page.url}#article`,
        isPartOf: { '@id': page.url },
        mainEntityOfPage: { '@id': page.url },
        headline: page.title,
        publisher: { '@id': `${siteUrl}/#organization` },
    };
    if (page.datePublished) {
        node.datePublished = page.datePublished;
    }
    if (page.dateModified) {
        node.dateModified = page.dateModified;
    }
    if (page.description) {
        node.description = page.description;
    }
    if (page.author) {
        node.author = {
            '@id': `${siteUrl}/#/schema/Person/${slugify(page.author.name)}`,
        };
    }
    if (page.featuredImage) {
        node.image = { '@id': page.featuredImage.url };
    }
    else if (page.images && page.images.length > 0) {
        node.image = { '@id': page.images[0].url };
    }
    if (page.articleSection && page.articleSection.length > 0) {
        node.articleSection = page.articleSection;
    }
    if (page.keywords && page.keywords.length > 0) {
        node.keywords = page.keywords;
    }
    const lang = page.language ?? site.defaultLanguage;
    if (lang) {
        node.inLanguage = lang;
    }
    return node;
}
/** Build a BreadcrumbList node. */
export function buildBreadcrumbSchema(breadcrumbs) {
    const url = breadcrumbs.length > 0 ? breadcrumbs[breadcrumbs.length - 1].url : '';
    return {
        '@type': 'BreadcrumbList',
        '@id': `${url}#breadcrumb`,
        itemListElement: breadcrumbs.map((crumb, index) => ({
            '@type': 'ListItem',
            position: index + 1,
            name: crumb.name,
            item: crumb.url,
        })),
    };
}
/** Build a FAQPage schema (standalone, without graph wrapper). */
export function buildFAQSchema(items) {
    return {
        '@type': 'FAQPage',
        mainEntity: items.map((item) => ({
            '@type': 'Question',
            name: item.question,
            acceptedAnswer: { '@type': 'Answer', text: item.answer },
        })),
    };
}
// ---------------------------------------------------------------------------
// Full graph builder
// ---------------------------------------------------------------------------
/** Build a complete `@graph` JSON-LD object for a page. */
export function buildSchemaGraph(site, page) {
    const siteUrl = stripTrailingSlash(site.siteUrl);
    const graph = [];
    graph.push(buildWebSiteSchema(site));
    graph.push(buildOrganizationSchema(site));
    graph.push(buildWebPageSchema(site, page));
    if (page.breadcrumbs && page.breadcrumbs.length > 0) {
        graph.push(buildBreadcrumbSchema(page.breadcrumbs));
    }
    if (isArticleType(page.type)) {
        graph.push(buildArticleSchema(site, page));
    }
    if (page.author) {
        const personNode = {
            '@type': 'Person',
            '@id': `${siteUrl}/#/schema/Person/${slugify(page.author.name)}`,
            name: page.author.name,
        };
        if (page.author.url) {
            personNode.url = page.author.url;
        }
        if (page.author.image) {
            personNode.image = {
                '@type': 'ImageObject',
                '@id': page.author.image,
                url: page.author.image,
                contentUrl: page.author.image,
            };
        }
        graph.push(personNode);
    }
    if (page.featuredImage) {
        const imgNode = {
            '@type': 'ImageObject',
            '@id': page.featuredImage.url,
            url: page.featuredImage.url,
            contentUrl: page.featuredImage.url,
        };
        if (page.featuredImage.width) {
            imgNode.width = page.featuredImage.width;
        }
        if (page.featuredImage.height) {
            imgNode.height = page.featuredImage.height;
        }
        graph.push(imgNode);
    }
    return {
        '@context': 'https://schema.org',
        '@graph': graph,
    };
}
//# sourceMappingURL=structured-data.js.map