'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from 'react';
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '../../ui/dialog.js';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '../../ui/tabs.js';
import { GitHubUrlInput } from './github-url-input.js';
import { GitHubRepoBrowser } from './github-repo-browser.js';
import { importGitHubRepository } from '../../../app/actions/import-github-repository.js';
export function GitHubImportDialog({ open, onOpenChange, onImportComplete, }) {
    const [importing, setImporting] = useState(false);
    const [error, setError] = useState('');
    async function handleImport(url) {
        setImporting(true);
        setError('');
        try {
            const result = await importGitHubRepository({ url });
            if (result.error) {
                setError(result.error);
            }
            else if (result.repository) {
                onImportComplete(result.repository);
                onOpenChange(false);
            }
        }
        catch {
            setError('An unexpected error occurred');
        }
        finally {
            setImporting(false);
        }
    }
    return (_jsx(Dialog, { open: open, onOpenChange: onOpenChange, children: _jsxs(DialogContent, { className: "overflow-hidden sm:max-w-lg", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { children: "Import from GitHub" }), _jsx(DialogDescription, { children: "Clone a GitHub repository and add it to Shep." })] }), _jsxs(Tabs, { defaultValue: "url", className: "min-w-0", children: [_jsxs(TabsList, { className: "grid w-full grid-cols-2", children: [_jsx(TabsTrigger, { value: "url", children: "URL" }), _jsx(TabsTrigger, { value: "browse", children: "Browse" })] }), _jsx(TabsContent, { value: "url", className: "mt-4", children: _jsx(GitHubUrlInput, { onSubmit: handleImport, loading: importing }) }), _jsx(TabsContent, { value: "browse", className: "mt-4", children: _jsx(GitHubRepoBrowser, { onSelect: handleImport, loading: importing }) })] }), error ? (_jsx("p", { className: "text-destructive text-sm", role: "alert", children: error })) : null] }) }));
}
