import { cmsApi } from './api.js';
/**
 * Mint a signed, time-boxed preview token for a single document. The token is
 * bound to the (collection, documentId) pair server-side, so it can only
 * unlock the exact draft it was issued for. Used to build public preview URLs
 * (`/<prefix>/<slug>?preview=<token>`) that render a draft through the
 * consumer's real front-end — see {@link buildPublicPreviewUrl}.
 */
export async function createPreviewToken(collection, documentId, ttlSeconds) {
    const res = await cmsApi('/preview/token', {
        method: 'POST',
        body: JSON.stringify({ collection, documentId, ttlSeconds }),
    });
    if (res.error || !res.data) {
        throw new Error(res.error ?? 'Failed to create preview token');
    }
    return res.data;
}
/**
 * Build the public URL a draft preview opens at — the SAME path the
 * `/resolve` endpoint matches: `<urlPrefix>/<slug>` (or `/<slug>` for
 * top-level pages), with the preview token on the query string so the
 * public route can drop the `PUBLISHED` filter for this one document.
 */
export function buildPublicPreviewUrl({ siteUrl, urlPrefix, slug, token, }) {
    const base = (siteUrl ?? '').replace(/\/+$/, '');
    const prefix = (urlPrefix ?? '').replace(/^\/+|\/+$/g, '');
    const cleanSlug = slug.replace(/^\/+|\/+$/g, '');
    const path = prefix ? `/${prefix}/${cleanSlug}` : `/${cleanSlug}`;
    return `${base}${path}?preview=${encodeURIComponent(token)}`;
}
/** Resolve the public site origin: explicit config first, else the current tab's origin. */
export function resolveSiteUrl(configSiteUrl) {
    if (configSiteUrl && configSiteUrl.trim())
        return configSiteUrl.trim();
    if (typeof window !== 'undefined')
        return window.location.origin;
    return '';
}
//# sourceMappingURL=preview-link.js.map