'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useEffect } from 'react';
import { ArrowLeft, Eye, Loader2, AlertTriangle } from 'lucide-react';
import { toast } from 'sonner';
import { SEOPanel } from '../components/SEOPanel.js';
import { useApiData } from '../lib/useApiData.js';
import { cmsApi } from '../lib/api.js';
export function PageEditor({ id, onNavigate }) {
    const isNew = !id;
    const { data, loading, error } = useApiData(isNew ? '' : `/collections/pages/${id}`);
    const [title, setTitle] = useState('');
    const [slug, setSlug] = useState('');
    const [status, setStatus] = useState('draft');
    const [saving, setSaving] = useState(false);
    const [initialized, setInitialized] = useState(isNew);
    const [seoData, setSeoData] = useState({});
    useEffect(() => {
        if (data && !initialized) {
            setTitle(data.title ?? '');
            setSlug(data.slug ?? '');
            setStatus(data.status === 'PUBLISHED' ? 'published' : 'draft');
            setInitialized(true);
        }
    }, [data, initialized]);
    const savePage = async () => {
        setSaving(true);
        const body = JSON.stringify({
            title,
            slug,
            status: status === 'published' ? 'PUBLISHED' : 'DRAFT',
        });
        const res = isNew
            ? await cmsApi('/collections/pages', { method: 'POST', body })
            : await cmsApi(`/collections/pages/${id}`, { method: 'PUT', body });
        setSaving(false);
        if (res.error) {
            toast.error(res.error);
        }
        else {
            toast.success('Page saved successfully!');
            if (isNew && res.data?.id) {
                onNavigate?.(`/pages/${res.data.id}`);
            }
        }
    };
    const publishPage = async () => {
        setSaving(true);
        const body = JSON.stringify({
            title,
            slug,
            status: 'PUBLISHED',
        });
        const endpoint = isNew ? '/collections/pages' : `/collections/pages/${id}`;
        const method = isNew ? 'POST' : 'PUT';
        const res = await cmsApi(endpoint, { method, body });
        setSaving(false);
        if (res.error) {
            toast.error(res.error);
        }
        else {
            setStatus('published');
            toast.success('Page published!');
            if (isNew && res.data?.id) {
                onNavigate?.(`/pages/${res.data.id}`);
            }
        }
    };
    if (!isNew && loading) {
        return (_jsx("div", { className: "flex h-full items-center justify-center", children: _jsx(Loader2, { className: "text-primary h-6 w-6 animate-spin" }) }));
    }
    return (_jsxs("div", { className: "bg-card flex h-full flex-col", children: [error && (_jsxs("div", { className: "border-destructive/30 bg-destructive/10 mx-4 mt-3 flex items-center gap-3 rounded-lg border p-3", children: [_jsx(AlertTriangle, { className: "text-destructive h-5 w-5 shrink-0" }), _jsx("span", { className: "text-destructive flex-1 text-sm", children: error })] })), _jsx("div", { className: "border-border border-b px-4 py-3", children: _jsxs("div", { className: "flex items-center justify-between", children: [_jsxs("button", { onClick: () => onNavigate?.('/pages'), className: "text-muted-foreground hover:text-foreground inline-flex items-center gap-2 text-sm transition-colors", children: [_jsx(ArrowLeft, { className: "h-4 w-4" }), "Back to Pages"] }), _jsx("div", { className: "flex items-center gap-3", children: _jsxs("button", { className: "border-border hover:bg-muted flex items-center gap-2 rounded-lg border px-3 py-1.5 text-sm transition-colors", children: [_jsx(Eye, { className: "h-4 w-4" }), "Preview"] }) })] }) }), _jsxs("div", { className: "flex flex-1 overflow-hidden", children: [_jsx("div", { className: "flex-1 overflow-y-auto", children: _jsxs("div", { className: "mx-auto max-w-4xl p-3 pr-6 sm:p-4 sm:pr-8", children: [_jsx("input", { type: "text", value: title, onChange: (e) => setTitle(e.target.value), placeholder: "Page title", className: "placeholder:text-muted-foreground mb-4 w-full border-none px-0 text-2xl font-medium focus:ring-0 focus:outline-none sm:text-3xl" }), _jsxs("div", { className: "mb-4", children: [_jsx("label", { className: "text-muted-foreground mb-1 block text-xs font-medium", children: "URL Slug" }), _jsx("input", { type: "text", value: slug, onChange: (e) => setSlug(e.target.value), placeholder: "url-slug", className: "border-border focus-visible:ring-ring/50 w-full rounded-lg border px-3 py-1.5 text-sm focus:ring-2 focus:outline-none" })] }), _jsxs("div", { className: "border-border rounded-lg border-2 border-dashed p-8 text-center", children: [_jsx("p", { className: "text-muted-foreground mb-2", children: "No blocks added yet" }), _jsx("p", { className: "text-muted-foreground text-sm", children: "Click \"Add Block\" to start building your page" }), _jsx("button", { className: "bg-primary text-primary-foreground hover:bg-primary/90 mt-4 rounded-lg px-4 py-2 text-sm transition-colors", children: "Add Block" })] })] }) }), _jsx("div", { className: "border-border bg-muted w-[30%] overflow-y-auto border-l", children: _jsxs("div", { className: "space-y-4 p-4", children: [_jsxs("div", { className: "border-border bg-card rounded-lg border p-4", children: [_jsx("h3", { className: "text-foreground mb-3 text-sm font-medium", children: "Publish" }), _jsxs("div", { className: "space-y-3", children: [_jsxs("div", { children: [_jsx("label", { className: "text-foreground mb-1 block text-xs font-medium", children: "Status" }), _jsxs("select", { value: status, onChange: (e) => setStatus(e.target.value), className: "border-border focus-visible:ring-ring/50 w-full rounded-lg border px-3 py-1.5 text-sm focus:ring-2 focus:outline-none", children: [_jsx("option", { value: "draft", children: "Draft" }), _jsx("option", { value: "published", children: "Published" })] })] }), _jsx("button", { onClick: savePage, disabled: saving, className: "bg-primary text-primary-foreground hover:bg-primary/90 w-full rounded-lg py-2 text-sm font-medium transition-colors disabled:opacity-50", children: saving ? 'Saving...' : 'Save Page' }), status === 'draft' && (_jsx("button", { onClick: publishPage, disabled: saving, className: "bg-success text-success-foreground hover:bg-success/90 w-full rounded-lg py-2 text-sm font-medium transition-colors disabled:opacity-50", children: saving ? 'Publishing...' : 'Publish' }))] })] }), _jsx(SEOPanel, { title: title, slug: slug, seoData: seoData, onChange: setSeoData })] }) })] })] }));
}
//# sourceMappingURL=PageEditor.js.map