'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useId, useState } from 'react';
import * as Dialog from '@radix-ui/react-dialog';
import { Calendar, Clock, Loader2, X } from 'lucide-react';
import { toast } from 'sonner';
import { cmsApi } from '../lib/api.js';
import { adminPortalContainer } from '../lib/portal-container.js';
/**
 * Convert a Date or ISO string to the `YYYY-MM-DDTHH:mm` shape that the
 * `<input type="datetime-local">` element expects, in the user's local
 * timezone (the input is timezone-naive but we present + parse it as local).
 */
function toLocalDateTimeInput(value) {
    if (!value)
        return '';
    const d = typeof value === 'string' ? new Date(value) : value;
    if (Number.isNaN(d.getTime()))
        return '';
    const pad = (n) => String(n).padStart(2, '0');
    return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
}
/** Round "now + 1 hour" to the next quarter-hour for a friendly default. */
function defaultScheduleTime() {
    const d = new Date(Date.now() + 60 * 60 * 1000);
    d.setMinutes(Math.ceil(d.getMinutes() / 15) * 15, 0, 0);
    return toLocalDateTimeInput(d);
}
export function SchedulePublishDialog({ collectionSlug, documentId, scheduledAt, scheduledUnpublishAt, open, onClose, onScheduled, }) {
    const [publishAt, setPublishAt] = useState('');
    const [unpublishAt, setUnpublishAt] = useState('');
    const [includeUnpublish, setIncludeUnpublish] = useState(false);
    const [saving, setSaving] = useState(false);
    const [cancelling, setCancelling] = useState(false);
    const publishAtId = useId();
    const unpublishAtId = useId();
    const hasExistingSchedule = Boolean(scheduledAt || scheduledUnpublishAt);
    useEffect(() => {
        if (!open)
            return;
        setPublishAt(scheduledAt ? toLocalDateTimeInput(scheduledAt) : defaultScheduleTime());
        setUnpublishAt(scheduledUnpublishAt ? toLocalDateTimeInput(scheduledUnpublishAt) : '');
        setIncludeUnpublish(Boolean(scheduledUnpublishAt));
    }, [open, scheduledAt, scheduledUnpublishAt]);
    async function handleSave() {
        if (!publishAt && !(includeUnpublish && unpublishAt)) {
            toast.error('Pick a publish or unpublish time');
            return;
        }
        // datetime-local is parsed by the browser as local time; new Date() on
        // that string respects the user's timezone and serialises to a UTC
        // ISO string the server can compare against `Date.now()` cleanly.
        const publishDate = publishAt ? new Date(publishAt) : null;
        const unpublishDate = includeUnpublish && unpublishAt ? new Date(unpublishAt) : null;
        if (publishDate && publishDate.getTime() <= Date.now()) {
            toast.error('Publish time must be in the future');
            return;
        }
        if (unpublishDate && unpublishDate.getTime() <= Date.now()) {
            toast.error('Unpublish time must be in the future');
            return;
        }
        if (publishDate && unpublishDate && unpublishDate <= publishDate) {
            toast.error('Unpublish time must be after publish time');
            return;
        }
        setSaving(true);
        const res = await cmsApi(`/collections/${collectionSlug}/${documentId}/schedule`, {
            method: 'POST',
            body: JSON.stringify({
                scheduledAt: publishDate ? publishDate.toISOString() : null,
                scheduledUnpublishAt: unpublishDate ? unpublishDate.toISOString() : null,
            }),
        });
        setSaving(false);
        if (res.error) {
            toast.error(res.error);
            return;
        }
        toast.success(publishDate ? 'Publish scheduled' : 'Unpublish scheduled');
        onScheduled?.({
            status: res.data?.status ?? 'SCHEDULED',
            scheduledAt: res.data?.scheduledAt ?? (publishDate ? publishDate.toISOString() : null),
            scheduledUnpublishAt: res.data?.scheduledUnpublishAt ?? (unpublishDate ? unpublishDate.toISOString() : null),
        });
        onClose();
    }
    async function handleCancelSchedule() {
        setCancelling(true);
        const res = await cmsApi(`/collections/${collectionSlug}/${documentId}/schedule`, {
            method: 'DELETE',
        });
        setCancelling(false);
        if (res.error) {
            toast.error(res.error);
            return;
        }
        toast.success('Schedule cancelled');
        onScheduled?.({
            status: res.data?.status ?? 'DRAFT',
            scheduledAt: null,
            scheduledUnpublishAt: null,
        });
        onClose();
    }
    return (_jsx(Dialog.Root, { open: open, onOpenChange: (next) => !next && onClose(), children: _jsxs(Dialog.Portal, { container: adminPortalContainer(), children: [_jsx(Dialog.Overlay, { className: "fixed inset-0 z-50 bg-black/40" }), _jsxs(Dialog.Content, { className: "bg-card text-card-foreground border-border fixed top-1/2 left-1/2 z-50 w-full max-w-md -translate-x-1/2 -translate-y-1/2 rounded-lg border shadow-xl focus:outline-none", children: [_jsxs("div", { className: "border-border flex items-center justify-between border-b px-4 py-3", children: [_jsxs("div", { className: "flex items-center gap-2", children: [_jsx(Calendar, { className: "text-muted-foreground h-5 w-5", "aria-hidden": true }), _jsx(Dialog.Title, { className: "text-foreground text-lg font-medium", children: "Schedule publishing" })] }), _jsx(Dialog.Close, { asChild: true, children: _jsx("button", { type: "button", "aria-label": "Close dialog", className: "text-muted-foreground hover:bg-accent hover:text-foreground focus-visible:ring-ring rounded-lg p-1.5 transition-colors focus-visible:ring-2 focus-visible:outline-none", children: _jsx(X, { className: "h-5 w-5", "aria-hidden": true }) }) })] }), _jsxs("div", { className: "space-y-4 px-4 py-4", children: [_jsxs("div", { className: "space-y-1.5", children: [_jsx("label", { htmlFor: publishAtId, className: "text-foreground text-sm font-medium", children: "Publish at" }), _jsxs("div", { className: "relative", children: [_jsx(Clock, { className: "text-muted-foreground pointer-events-none absolute top-2.5 left-2.5 h-4 w-4", "aria-hidden": true }), _jsx("input", { id: publishAtId, type: "datetime-local", value: publishAt, onChange: (e) => setPublishAt(e.target.value), min: toLocalDateTimeInput(new Date(Date.now() + 60 * 1000)), className: "border-input bg-background text-foreground focus:border-ring focus:ring-ring w-full rounded-md border py-2 pr-3 pl-9 text-sm focus:ring-1 focus:outline-none" })] }), _jsx("p", { className: "text-muted-foreground text-xs", children: "The document will move from DRAFT to PUBLISHED at this time (your local timezone)." })] }), _jsxs("label", { className: "text-foreground flex items-center gap-2 text-sm", children: [_jsx("input", { type: "checkbox", checked: includeUnpublish, onChange: (e) => setIncludeUnpublish(e.target.checked), className: "border-input text-primary focus:ring-ring h-4 w-4 rounded" }), "Also schedule an unpublish"] }), includeUnpublish && (_jsxs("div", { className: "space-y-1.5", children: [_jsx("label", { htmlFor: unpublishAtId, className: "text-foreground text-sm font-medium", children: "Unpublish at" }), _jsxs("div", { className: "relative", children: [_jsx(Clock, { className: "text-muted-foreground pointer-events-none absolute top-2.5 left-2.5 h-4 w-4", "aria-hidden": true }), _jsx("input", { id: unpublishAtId, type: "datetime-local", value: unpublishAt, onChange: (e) => setUnpublishAt(e.target.value), min: publishAt || toLocalDateTimeInput(new Date(Date.now() + 60 * 1000)), className: "border-input bg-background text-foreground focus:border-ring focus:ring-ring w-full rounded-md border py-2 pr-3 pl-9 text-sm focus:ring-1 focus:outline-none" })] })] }))] }), _jsxs("div", { className: "border-border flex items-center justify-between gap-2 border-t px-4 py-3", children: [hasExistingSchedule ? (_jsxs("button", { type: "button", onClick: handleCancelSchedule, disabled: cancelling || saving, className: "text-destructive hover:bg-destructive/10 inline-flex items-center gap-1.5 rounded-md px-3 py-1.5 text-sm font-medium disabled:opacity-50", children: [cancelling ? (_jsx(Loader2, { className: "h-4 w-4 animate-spin", "aria-hidden": true })) : (_jsx(X, { className: "h-4 w-4", "aria-hidden": true })), "Cancel schedule"] })) : (_jsx("span", {})), _jsxs("div", { className: "flex items-center gap-2", children: [_jsx(Dialog.Close, { asChild: true, children: _jsx("button", { type: "button", className: "text-foreground hover:bg-accent rounded-md px-3 py-1.5 text-sm", children: "Close" }) }), _jsxs("button", { type: "button", onClick: handleSave, disabled: saving || cancelling, className: "bg-primary text-primary-foreground inline-flex items-center gap-1.5 rounded-md px-3 py-1.5 text-sm font-medium hover:opacity-90 disabled:opacity-50", children: [saving ? (_jsx(Loader2, { className: "h-4 w-4 animate-spin", "aria-hidden": true })) : (_jsx(Calendar, { className: "h-4 w-4", "aria-hidden": true })), hasExistingSchedule ? 'Reschedule' : 'Schedule'] })] })] })] })] }) }));
}
//# sourceMappingURL=SchedulePublishDialog.js.map