'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from 'react';
import { Loader2 } from 'lucide-react';
import { Input } from '../../ui/input.js';
import { Button } from '../../ui/button.js';
import { Label } from '../../ui/label.js';
/**
 * Matches supported GitHub URL formats:
 * - https://github.com/owner/repo
 * - https://github.com/owner/repo.git
 * - git@github.com:owner/repo.git
 * - owner/repo (shorthand)
 */
const GITHUB_URL_PATTERN = /^(?:https:\/\/github\.com\/([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+?)(?:\.git)?|git@github\.com:([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+?)(?:\.git)?|([a-zA-Z0-9._-]+)\/([a-zA-Z0-9._-]+))$/;
export function isValidGitHubUrl(url) {
    return GITHUB_URL_PATTERN.test(url.trim());
}
export function GitHubUrlInput({ onSubmit, loading = false }) {
    const [url, setUrl] = useState('');
    const [error, setError] = useState('');
    function handleSubmit() {
        const trimmed = url.trim();
        if (!trimmed) {
            setError('Please enter a GitHub URL');
            return;
        }
        if (!isValidGitHubUrl(trimmed)) {
            setError('Enter a valid GitHub URL (e.g. owner/repo or https://github.com/owner/repo)');
            return;
        }
        setError('');
        onSubmit(trimmed);
    }
    function handleKeyDown(e) {
        if (e.key === 'Enter') {
            e.preventDefault();
            handleSubmit();
        }
    }
    return (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(Label, { htmlFor: "github-url-input", children: "GitHub URL" }), _jsxs("div", { className: "flex gap-2", children: [_jsx(Input, { id: "github-url-input", placeholder: "owner/repo or https://github.com/owner/repo", value: url, onChange: (e) => {
                            setUrl(e.target.value);
                            if (error)
                                setError('');
                        }, onKeyDown: handleKeyDown, disabled: loading, "aria-invalid": !!error, "aria-describedby": error ? 'github-url-error' : undefined }), _jsx(Button, { onClick: handleSubmit, disabled: loading || !url.trim(), size: "sm", children: loading ? _jsx(Loader2, { className: "h-4 w-4 animate-spin" }) : 'Import' })] }), error ? (_jsx("p", { id: "github-url-error", className: "text-destructive text-sm", role: "alert", children: error })) : null] }));
}
