import { jsx as _jsx } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2026 HopCode Team
 * SPDX-License-Identifier: Apache-2.0
 */
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render } from 'ink-testing-library';
import { TextInput } from './TextInput.js';
import { useKeypress } from '../../hooks/useKeypress.js';
vi.mock('../../hooks/useKeypress.js', () => ({
    useKeypress: vi.fn(),
}));
vi.mock('../../semantic-colors.js', () => ({
    theme: {
        text: { accent: 'cyan' },
        status: { error: 'red' },
    },
}));
const mockedUseKeypress = vi.mocked(useKeypress);
function makeKey(overrides) {
    return {
        name: '',
        ctrl: false,
        meta: false,
        shift: false,
        paste: false,
        sequence: '',
        ...overrides,
    };
}
function captureKeypressHandler() {
    const calls = mockedUseKeypress.mock.calls;
    if (calls.length === 0) {
        throw new Error('useKeypress was not called');
    }
    // Return the most recent handler
    return calls[calls.length - 1][0];
}
describe('TextInput', () => {
    let onChange;
    let onSubmit;
    beforeEach(() => {
        vi.clearAllMocks();
        onChange = vi.fn();
        onSubmit = vi.fn();
    });
    describe('multiline mode (height > 1)', () => {
        it('submits on plain Enter', () => {
            render(_jsx(TextInput, { value: "", onChange: onChange, onSubmit: onSubmit, height: 5 }));
            const handler = captureKeypressHandler();
            handler(makeKey({ name: 'return', sequence: '\r' }));
            expect(onSubmit).toHaveBeenCalledTimes(1);
        });
        it('does NOT submit on Shift+Enter � inserts newline instead', () => {
            render(_jsx(TextInput, { value: "", onChange: onChange, onSubmit: onSubmit, height: 5 }));
            const handler = captureKeypressHandler();
            handler(makeKey({ name: 'return', shift: true, sequence: '\r' }));
            expect(onSubmit).not.toHaveBeenCalled();
            // onChange should be called with the newline character
            expect(onChange).toHaveBeenCalled();
        });
        it('does NOT submit on Ctrl+Enter � inserts newline instead', () => {
            render(_jsx(TextInput, { value: "", onChange: onChange, onSubmit: onSubmit, height: 5 }));
            const handler = captureKeypressHandler();
            handler(makeKey({ name: 'return', ctrl: true, sequence: '\r' }));
            expect(onSubmit).not.toHaveBeenCalled();
            expect(onChange).toHaveBeenCalled();
        });
    });
    describe('single-line mode (height = 1)', () => {
        it('submits on plain Enter', () => {
            render(_jsx(TextInput, { value: "", onChange: onChange, onSubmit: onSubmit, height: 1 }));
            const handler = captureKeypressHandler();
            handler(makeKey({ name: 'return', sequence: '\r' }));
            expect(onSubmit).toHaveBeenCalledTimes(1);
        });
        it('submits on Shift+Enter (no newline concept in single-line)', () => {
            render(_jsx(TextInput, { value: "", onChange: onChange, onSubmit: onSubmit, height: 1 }));
            const handler = captureKeypressHandler();
            handler(makeKey({ name: 'return', shift: true, sequence: '\r' }));
            expect(onSubmit).toHaveBeenCalledTimes(1);
        });
        it('ellipsizes long single-line values in the middle when enabled', () => {
            const { lastFrame } = render(_jsx(TextInput, { value: "sk-token-plan-abcdefghijklmnopqrstuvwxyz0123456789", onChange: onChange, inputWidth: 20, ellipsizeOverflow: true }));
            expect(lastFrame()).toContain('sk-token-...23456789');
        });
    });
});
//# sourceMappingURL=TextInput.test.js.map