'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { FolderPlus, Github, Loader2, Plus } from 'lucide-react';
import { Button } from '../../ui/button.js';
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '../../ui/tooltip.js';
import { Popover, PopoverContent, PopoverTrigger } from '../../ui/popover.js';
import { GitHubImportDialog } from '../../common/github-import-dialog/index.js';
import { useFeatureFlags } from '../../../hooks/feature-flags-context.js';
import { ReactFileManagerDialog } from '../../common/react-file-manager-dialog/index.js';
import { pickFolder } from './pick-folder.js';
export function AddRepositoryButton({ onSelect, onGitHubImport }) {
    const { t } = useTranslation('web');
    const featureFlags = useFeatureFlags();
    const [loading, setLoading] = useState(false);
    const [popoverOpen, setPopoverOpen] = useState(false);
    const [githubDialogOpen, setGithubDialogOpen] = useState(false);
    const [showReactPicker, setShowReactPicker] = useState(false);
    const { reactFileManager: useReactFileManager } = featureFlags;
    async function handleLocalFolder() {
        setPopoverOpen(false);
        if (loading)
            return;
        if (useReactFileManager) {
            setShowReactPicker(true);
            return;
        }
        setLoading(true);
        try {
            const path = await pickFolder();
            if (path) {
                onSelect?.(path);
            }
        }
        catch {
            // Native picker failed — fall back to React file manager
            setShowReactPicker(true);
        }
        finally {
            setLoading(false);
        }
    }
    function handleFromGitHub() {
        setPopoverOpen(false);
        setGithubDialogOpen(true);
    }
    function handleImportComplete(repository) {
        onGitHubImport?.(repository);
        if (repository.path) {
            onSelect?.(repository.path);
        }
    }
    function handleReactPickerSelect(path) {
        if (path) {
            onSelect?.(path);
        }
        setShowReactPicker(false);
    }
    return (_jsxs(_Fragment, { children: [_jsxs(Popover, { open: popoverOpen, onOpenChange: setPopoverOpen, children: [_jsx(TooltipProvider, { children: _jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(PopoverTrigger, { asChild: true, children: _jsx(Button, { variant: "ghost", size: "icon", "aria-label": t('addRepository.addRepository'), "data-testid": "add-repository-button", disabled: loading, children: loading ? (_jsx(Loader2, { className: "h-5 w-5 animate-spin" })) : (_jsx(Plus, { className: "h-5 w-5" })) }) }) }), _jsx(TooltipContent, { children: t('addRepository.addRepository') })] }) }), _jsxs(PopoverContent, { className: "w-48 p-1", align: "start", children: [_jsxs("button", { type: "button", className: "hover:bg-accent flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm", onClick: handleLocalFolder, "data-testid": "add-repo-local-folder", children: [_jsx(FolderPlus, { className: "h-4 w-4" }), "Local folder"] }), _jsxs("button", { type: "button", className: "hover:bg-accent flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm", onClick: handleFromGitHub, "data-testid": "add-repo-from-github", children: [_jsx(Github, { className: "h-4 w-4" }), "From GitHub"] })] })] }), _jsx(GitHubImportDialog, { open: githubDialogOpen, onOpenChange: setGithubDialogOpen, onImportComplete: handleImportComplete }), _jsx(ReactFileManagerDialog, { open: showReactPicker, onOpenChange: (open) => {
                    if (!open) {
                        setShowReactPicker(false);
                    }
                }, onSelect: handleReactPickerSelect })] }));
}
