import { act } from 'react';
import { describe, expect, it } from 'vitest';
import { useSubmitState } from '../../src/hooks/useSubmitState';
import { renderHook } from '../helpers/renderHook';

describe('useSubmitState', () => {
    it('tracks submit lifecycle and feedback state', () => {
        const { result } = renderHook(() => useSubmitState<string | null>(null));

        expect(result.current.isSubmitting).toBe(false);
        expect(result.current.submitError).toBeNull();
        expect(result.current.status).toBeNull();

        act(() => {
            result.current.startSubmitting();
            result.current.setSubmitError('Oops');
            result.current.setStatus('Done');
        });

        expect(result.current.isSubmitting).toBe(true);
        expect(result.current.submitError).toBe('Oops');
        expect(result.current.status).toBe('Done');

        act(() => {
            result.current.finishSubmitting();
            result.current.clearFeedback();
        });

        expect(result.current.isSubmitting).toBe(false);
        expect(result.current.submitError).toBeNull();
        expect(result.current.status).toBeNull();
    });

    it('restores the typed initial status in clearFeedback', () => {
        const { result } = renderHook(() => useSubmitState<'idle' | 'done'>('idle'));

        act(() => {
            result.current.setStatus('done');
            result.current.clearFeedback();
        });

        expect(result.current.status).toBe('idle');
    });
});
