'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useEffect } from 'react';
import { X, RotateCcw, Clock, User, Loader2 } from 'lucide-react';
import { toast } from 'sonner';
import { cmsApi } from '../lib/api.js';
function timeAgo(dateStr) {
    const diff = Date.now() - new Date(dateStr).getTime();
    const minutes = Math.floor(diff / 60_000);
    if (minutes < 1)
        return 'just now';
    if (minutes < 60)
        return `${minutes}m ago`;
    const hours = Math.floor(minutes / 60);
    if (hours < 24)
        return `${hours}h ago`;
    const days = Math.floor(hours / 24);
    if (days < 30)
        return `${days}d ago`;
    return new Date(dateStr).toLocaleDateString();
}
const CHANGE_TYPE_LABELS = {
    CREATE: { label: 'Created', color: 'bg-green-100 text-green-800' },
    UPDATE: { label: 'Updated', color: 'bg-blue-100 text-blue-800' },
    DELETE: { label: 'Deleted', color: 'bg-red-100 text-red-800' },
    RESTORE: { label: 'Restored', color: 'bg-purple-100 text-purple-800' },
};
export function VersionHistory({ collectionSlug, documentId, open, onClose, onRestore, }) {
    const [versions, setVersions] = useState([]);
    const [loading, setLoading] = useState(false);
    const [restoring, setRestoring] = useState(null);
    useEffect(() => {
        if (open)
            fetchVersions();
    }, [open, collectionSlug, documentId]);
    async function fetchVersions() {
        setLoading(true);
        const res = await cmsApi(`/collections/${collectionSlug}/${documentId}/versions?pageSize=50`);
        if (res.data) {
            setVersions(res.data.versions ?? []);
        }
        setLoading(false);
    }
    async function handleRestore(version) {
        setRestoring(version.id);
        const res = await cmsApi(`/collections/${collectionSlug}/${documentId}/versions/${version.id}/restore`, { method: 'POST' });
        if (res.error) {
            toast.error(res.error);
        }
        else {
            toast.success('Version restored');
            if (onRestore && version.data) {
                onRestore(version.data);
            }
            fetchVersions();
        }
        setRestoring(null);
    }
    if (!open)
        return null;
    return (_jsxs("div", { className: "fixed inset-0 z-50 flex justify-end", children: [_jsx("div", { className: "fixed inset-0 bg-black/30", onClick: onClose }), _jsxs("div", { className: "animate-in slide-in-from-right relative flex w-full max-w-md flex-col bg-white shadow-xl", children: [_jsxs("div", { className: "flex items-center justify-between border-b border-gray-200 px-4 py-3", children: [_jsxs("div", { className: "flex items-center gap-2", children: [_jsx(Clock, { className: "h-5 w-5 text-gray-600" }), _jsx("h2", { className: "text-lg font-semibold text-gray-900", children: "Version History" })] }), _jsx("button", { onClick: onClose, className: "rounded-lg p-1.5 transition-colors hover:bg-gray-100", children: _jsx(X, { className: "h-5 w-5 text-gray-500" }) })] }), _jsx("div", { className: "flex-1 overflow-y-auto", children: loading ? (_jsx("div", { className: "flex items-center justify-center py-16", children: _jsx(Loader2, { className: "h-6 w-6 animate-spin text-gray-400" }) })) : versions.length === 0 ? (_jsx("div", { className: "py-16 text-center text-sm text-gray-500", children: "No version history available" })) : (_jsx("div", { className: "divide-y divide-gray-100", children: versions.map((version, index) => {
                                const typeInfo = CHANGE_TYPE_LABELS[version.changeType] ?? {
                                    label: version.changeType,
                                    color: 'bg-gray-100 text-gray-800',
                                };
                                const isLatest = index === 0;
                                return (_jsx("div", { className: "px-4 py-3 transition-colors hover:bg-gray-50", children: _jsxs("div", { className: "flex items-start justify-between gap-3", children: [_jsxs("div", { className: "min-w-0 flex-1", children: [_jsxs("div", { className: "mb-1 flex items-center gap-2", children: [_jsx("span", { className: `inline-flex rounded-full px-2 py-0.5 text-xs font-medium ${typeInfo.color}`, children: typeInfo.label }), isLatest && (_jsx("span", { className: "text-xs font-medium text-gray-400", children: "Current" }))] }), _jsxs("div", { className: "flex items-center gap-3 text-xs text-gray-500", children: [version.changedBy && (_jsxs("span", { className: "flex items-center gap-1", children: [_jsx(User, { className: "h-3 w-3" }), version.changedBy.name ?? version.changedBy.email ?? 'Unknown'] })), _jsx("span", { title: new Date(version.createdAt).toLocaleString(), children: timeAgo(version.createdAt) })] })] }), !isLatest && (_jsxs("button", { onClick: () => handleRestore(version), disabled: restoring === version.id, className: "flex items-center gap-1.5 rounded-lg bg-blue-50 px-2.5 py-1.5 text-xs font-medium text-blue-700 transition-colors hover:bg-blue-100 disabled:opacity-50", children: [restoring === version.id ? (_jsx(Loader2, { className: "h-3.5 w-3.5 animate-spin" })) : (_jsx(RotateCcw, { className: "h-3.5 w-3.5" })), "Restore"] }))] }) }, version.id));
                            }) })) })] })] }));
}
//# sourceMappingURL=VersionHistory.js.map