'use client';
import { useCallback, useEffect, useRef, useState } from 'react';
import { toast } from 'sonner';
import { buildPublicPreviewUrl, createPreviewToken, resolveSiteUrl, } from '../../lib/preview-link.js';
/**
 * Inline branded preview — drives an in-editor iframe that renders the draft
 * through the consumer's REAL public route (same `?preview=<token>` contract
 * as "View on site"), so editors see actual site CSS without leaving the
 * admin. Saves first when dirty, and reloads the frame after each save so
 * what you see always matches the persisted draft.
 */
export function useSitePreview({ collection, documentId, slug, urlPrefix, siteUrl, dirty, saveState, saveDraft, }) {
    const [active, setActive] = useState(false);
    const [url, setUrl] = useState(null);
    const [reloadKey, setReloadKey] = useState(0);
    const tokenRef = useRef(null);
    const prevSaveState = useRef(saveState);
    // After a save completes while the preview is open, rebuild the URL (the
    // slug may have changed in settings — the token is bound to the document
    // id, not the path) and reload the frame.
    useEffect(() => {
        if (active && prevSaveState.current === 'saving' && saveState === 'saved') {
            if (tokenRef.current && slug) {
                setUrl(buildPublicPreviewUrl({
                    siteUrl: resolveSiteUrl(siteUrl),
                    urlPrefix: urlPrefix ?? '',
                    slug,
                    token: tokenRef.current,
                }));
            }
            setReloadKey((k) => k + 1);
        }
        prevSaveState.current = saveState;
    }, [active, saveState, slug, siteUrl, urlPrefix]);
    const toggle = useCallback(() => {
        if (active) {
            setActive(false);
            return;
        }
        if (!documentId || !slug) {
            toast.error('Save the page before opening the site preview');
            return;
        }
        setActive(true);
        setUrl(null);
        void (async () => {
            try {
                if (dirty) {
                    const ok = await saveDraft();
                    if (!ok) {
                        setActive(false);
                        return;
                    }
                }
                const { token } = await createPreviewToken(collection, documentId);
                tokenRef.current = token;
                setUrl(buildPublicPreviewUrl({
                    siteUrl: resolveSiteUrl(siteUrl),
                    urlPrefix: urlPrefix ?? '',
                    slug,
                    token,
                }));
            }
            catch (err) {
                toast.error(err instanceof Error ? err.message : 'Failed to open site preview');
                setActive(false);
            }
        })();
    }, [active, collection, documentId, slug, urlPrefix, siteUrl, dirty, saveDraft]);
    return { active, url, reloadKey, toggle };
}
//# sourceMappingURL=useSitePreview.js.map