import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState, useId, useEffect, useCallback } from 'react';
import { GitCommit, Sparkles, Loader2 } from 'lucide-react';
import { useGitStore } from '../../stores/gitStore';
import { useCommitChanges, useGenerateCommitMessage } from '../../hooks/useGit';
import { Button } from '../ui/Button';
import { Textarea } from '../ui/Textarea';
import { Modal } from '../ui/Modal';
export function GitCommitModal() {
    const { isCommitModalOpen, closeCommitModal } = useGitStore();
    const commitSessionId = useGitStore((state) => state.commitSessionId);
    const commitChanges = useCommitChanges();
    const generateMessage = useGenerateCommitMessage(commitSessionId);
    const [message, setMessage] = useState('');
    const messageId = useId();
    const handleCommit = useCallback(async () => {
        if (!message.trim())
            return;
        try {
            await commitChanges.mutateAsync(message);
            setMessage('');
            closeCommitModal();
        }
        catch (error) {
            console.error('Failed to commit:', error);
        }
    }, [message, commitChanges, closeCommitModal]);
    const handleClose = () => {
        setMessage('');
        closeCommitModal();
    };
    const handleGenerateMessage = useCallback(async () => {
        try {
            const result = await generateMessage.mutateAsync();
            setMessage(result.message);
        }
        catch (error) {
            console.error('Failed to generate commit message:', error);
        }
    }, [generateMessage]);
    useEffect(() => {
        if (!isCommitModalOpen)
            return;
        const handleKeyDown = (e) => {
            if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') {
                e.preventDefault();
                e.stopPropagation();
                if (message.trim() && !commitChanges.isPending) {
                    handleCommit();
                }
            }
            if ((e.metaKey || e.ctrlKey) && e.key === 'g') {
                e.preventDefault();
                e.stopPropagation();
                handleGenerateMessage();
            }
        };
        document.addEventListener('keydown', handleKeyDown);
        return () => document.removeEventListener('keydown', handleKeyDown);
    }, [
        isCommitModalOpen,
        message,
        commitChanges.isPending,
        handleCommit,
        handleGenerateMessage,
    ]);
    return (_jsx(Modal, { isOpen: isCommitModalOpen, onClose: handleClose, title: _jsxs("div", { className: "flex items-center gap-2", children: [_jsx(GitCommit, { className: "w-5 h-5" }), _jsx("span", { children: "Commit Changes" })] }), maxWidth: "2xl", children: _jsxs("div", { className: "space-y-4", children: [_jsxs("div", { className: "space-y-2", children: [_jsx("label", { htmlFor: messageId, className: "text-sm font-medium text-foreground", children: "Commit Message" }), _jsx(Textarea, { id: messageId, value: message, onChange: (e) => setMessage(e.target.value), placeholder: "Enter commit message...", rows: 6, className: "w-full resize-none", autoFocus: true, disabled: generateMessage.isPending })] }), _jsx(Button, { variant: "secondary", onClick: handleGenerateMessage, className: "w-full", disabled: generateMessage.isPending, children: generateMessage.isPending ? (_jsxs(_Fragment, { children: [_jsx(Loader2, { className: "w-4 h-4 mr-2 animate-spin" }), "Generating..."] })) : (_jsxs(_Fragment, { children: [_jsx(Sparkles, { className: "w-4 h-4 mr-2" }), "Generate commit message", ' ', _jsx("span", { className: "text-muted-foreground ml-1", children: "(\u2318G)" })] })) }), generateMessage.isError && (_jsx("div", { className: "text-sm text-red-600 dark:text-red-400 bg-red-500/10 border border-red-500/20 rounded px-3 py-2", children: generateMessage.error?.message ||
                        'Failed to generate commit message' })), _jsxs("div", { className: "flex items-center justify-end gap-2 pt-4 border-t border-border", children: [_jsxs(Button, { variant: "ghost", onClick: handleClose, children: ["Cancel ", _jsx("span", { className: "text-muted-foreground ml-1", children: "(Esc)" })] }), _jsx(Button, { variant: "primary", onClick: handleCommit, disabled: !message.trim() || commitChanges.isPending, children: commitChanges.isPending ? (_jsx("span", { children: "Committing..." })) : (_jsxs(_Fragment, { children: [_jsx(GitCommit, { className: "w-4 h-4 mr-2" }), "Commit ", _jsx("span", { className: "text-muted-foreground ml-1", children: "(\u2318\u21B5)" })] })) })] }), commitChanges.isError && (_jsx("div", { className: "text-sm text-red-600 dark:text-red-400 bg-red-500/10 border border-red-500/20 rounded px-3 py-2", children: commitChanges.error?.message || 'Failed to commit changes' }))] }) }));
}
//# sourceMappingURL=GitCommitModal.js.map