'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * ConnectProviderModal — token paste dialog for cloud deploy providers.
 *
 * Opens when the user selects an enabled-but-not-connected provider from
 * the Deploy dropdown. Shows the provider icon, a "Get a token" external
 * link, a textarea for the token, and a Connect button that submits to
 * POST /api/cloud-providers/:provider/connect via the useCloudDeployAction
 * hook.
 */
import { useState } from 'react';
import { ExternalLink } from 'lucide-react';
import { CloudDeploymentProvider } from '../../../../../../packages/core/src/domain/generated/output.js';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '../../ui/dialog.js';
import { Button } from '../../ui/button.js';
import { Textarea } from '../../ui/textarea.js';
import { CLOUD_PROVIDER_ICONS } from './cloud-provider-icons.js';
const PROVIDER_TOKEN_PAGES = {
    [CloudDeploymentProvider.CloudflarePages]: 'https://dash.cloudflare.com/profile/api-tokens',
    [CloudDeploymentProvider.Vercel]: 'https://vercel.com/account/tokens',
    [CloudDeploymentProvider.Netlify]: 'https://app.netlify.com/user/applications#personal-access-tokens',
    [CloudDeploymentProvider.AwsAmplify]: 'https://us-east-1.console.aws.amazon.com/iam/home#/security_credentials',
    [CloudDeploymentProvider.GcpCloudRun]: 'https://console.cloud.google.com/apis/credentials',
};
const PROVIDER_DISPLAY_NAMES = {
    [CloudDeploymentProvider.CloudflarePages]: 'Cloudflare Pages',
    [CloudDeploymentProvider.Vercel]: 'Vercel',
    [CloudDeploymentProvider.Netlify]: 'Netlify',
    [CloudDeploymentProvider.AwsAmplify]: 'AWS Amplify',
    [CloudDeploymentProvider.GcpCloudRun]: 'Google Cloud Run',
};
export function ConnectProviderModal({ provider, mode = 'connect', onClose, onSubmit, }) {
    const [token, setToken] = useState('');
    const [submitting, setSubmitting] = useState(false);
    const [error, setError] = useState(null);
    const Icon = provider ? CLOUD_PROVIDER_ICONS[provider] : null;
    const tokenUrl = provider ? PROVIDER_TOKEN_PAGES[provider] : '';
    const displayName = provider ? PROVIDER_DISPLAY_NAMES[provider] : '';
    async function handleSubmit() {
        if (!provider || token.trim().length === 0)
            return;
        setSubmitting(true);
        setError(null);
        try {
            await onSubmit(provider, token.trim());
            setToken('');
            onClose();
        }
        catch (err) {
            setError(err instanceof Error ? err.message : 'Failed to connect');
        }
        finally {
            setSubmitting(false);
        }
    }
    return (_jsx(Dialog, { open: provider !== null, onOpenChange: (open) => {
            if (!open)
                onClose();
        }, children: _jsxs(DialogContent, { className: "max-w-md", children: [_jsxs(DialogHeader, { children: [_jsxs(DialogTitle, { className: "flex items-center gap-2", children: [Icon ? _jsx(Icon, { className: "size-5" }) : null, mode === 'update' ? `Update ${displayName} token` : `Connect to ${displayName}`] }), _jsx(DialogDescription, { children: mode === 'update'
                                ? `Paste a new ${displayName} API token to replace the one stored on this machine. The previous token will be overwritten.`
                                : `Paste your ${displayName} API token below. It is stored encrypted on this machine and never sent anywhere else.` })] }), _jsxs("a", { href: tokenUrl, target: "_blank", rel: "noopener noreferrer", className: "text-primary inline-flex cursor-pointer items-center gap-1 text-xs hover:underline", children: ["Get a token ", _jsx(ExternalLink, { className: "size-3" })] }), _jsx(Textarea, { value: token, onChange: (e) => setToken(e.target.value), placeholder: "Paste your API token here\u2026", className: "mt-2 min-h-[80px] font-mono text-xs" }), error ? _jsx("p", { className: "text-destructive text-xs", children: error }) : null, _jsxs(DialogFooter, { children: [_jsx(Button, { variant: "ghost", onClick: onClose, disabled: submitting, className: "cursor-pointer", children: "Cancel" }), _jsx(Button, { onClick: handleSubmit, disabled: submitting || token.trim().length === 0, className: "cursor-pointer", children: submitting
                                ? mode === 'update'
                                    ? 'Updating…'
                                    : 'Connecting…'
                                : mode === 'update'
                                    ? 'Update token'
                                    : 'Connect' })] })] }) }));
}
