import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { render, screen, waitFor } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import { describe, expect, it, vi } from 'vitest';
import { renderWithForm } from '../../lib/testing.js';
import { Form } from '../form/index.js';
import { CountryCodeField } from './country-code-field.js';
describe('CountryCodeField Component', () => {
    const defaultProps = {
        name: 'country',
        label: 'Select Country',
    };
    it('should match snapshot', () => {
        const { asFragment } = renderWithForm(_jsx(CountryCodeField, { ...defaultProps }));
        expect(asFragment()).toMatchSnapshot();
    });
    it('should render with label', () => {
        renderWithForm(_jsx(CountryCodeField, { ...defaultProps }));
        expect(screen.getByText('Select Country')).toBeInTheDocument();
    });
    it('should render with description', () => {
        renderWithForm(_jsx(CountryCodeField, { ...defaultProps, description: "Please select your country" }));
        expect(screen.getByText('Please select your country')).toBeInTheDocument();
    });
    it('should show required indicator when required prop is true', () => {
        renderWithForm(_jsx(CountryCodeField, { ...defaultProps, required: true }));
        expect(screen.getByText('*')).toBeInTheDocument();
    });
    it('should disable the field when disabled prop is true', () => {
        renderWithForm(_jsx(CountryCodeField, { ...defaultProps, disabled: true }));
        expect(screen.getByRole('combobox')).toBeDisabled();
    });
    it('should display error messages', async () => {
        renderWithForm(_jsx(CountryCodeField, { ...defaultProps, errors: ['Country is required'] }));
        await waitFor(() => {
            expect(screen.getByText('Country is required')).toBeInTheDocument();
        });
    });
    it('should display warning messages', () => {
        renderWithForm(_jsx(CountryCodeField, { ...defaultProps, warnings: ['Some countries may not be available'] }));
        expect(screen.getByText('Some countries may not be available')).toBeInTheDocument();
    });
    it('should handle value changes', async () => {
        const user = userEvent.setup();
        const onChange = vi.fn();
        renderWithForm(_jsx(CountryCodeField, { ...defaultProps, onChange: onChange }));
        const select = screen.getByRole('combobox');
        await user.click(select);
        await user.click(screen.getByText('United States'));
        expect(onChange).toHaveBeenCalledWith('US');
    });
    it('should filter countries based on search when enableSearch is true', async () => {
        const user = userEvent.setup();
        renderWithForm(_jsx(CountryCodeField, { ...defaultProps, enableSearch: true, placeholder: "Search..." }));
        await user.click(screen.getByRole('combobox'));
        await user.type(screen.getByPlaceholderText('Search...'), 'united');
        expect(screen.getByText('United States')).toBeInTheDocument();
        expect(screen.getByText('United Kingdom')).toBeInTheDocument();
        expect(screen.queryByText('France')).not.toBeInTheDocument();
        expect(screen.queryByText('Germany')).not.toBeInTheDocument();
    });
    it('should filter countries based on allowedCountries prop', async () => {
        const user = userEvent.setup();
        renderWithForm(_jsx(CountryCodeField, { ...defaultProps, allowedCountries: ['US', 'GB'] }));
        const select = screen.getByRole('combobox');
        await user.click(select);
        expect(screen.getByText('United States')).toBeInTheDocument();
        expect(screen.getByText('United Kingdom')).toBeInTheDocument();
        expect(screen.queryByText('France')).not.toBeInTheDocument();
    });
    it('should filter out countries based on excludedCountries prop', async () => {
        const user = userEvent.setup();
        renderWithForm(_jsx(CountryCodeField, { ...defaultProps, excludedCountries: ['FR', 'DE'] }));
        const select = screen.getByRole('combobox');
        await user.click(select);
        expect(screen.getByText('United States')).toBeInTheDocument();
        expect(screen.getByText('United Kingdom')).toBeInTheDocument();
        expect(screen.queryByText('France')).not.toBeInTheDocument();
        expect(screen.queryByText('Germany')).not.toBeInTheDocument();
    });
    it('should handle both allowedCountries and excludedCountries props', async () => {
        const user = userEvent.setup();
        renderWithForm(_jsx(CountryCodeField, { ...defaultProps, allowedCountries: ['US', 'GB'], excludedCountries: ['FR'] }));
        const select = screen.getByRole('combobox');
        await user.click(select);
        expect(screen.getByText('United States')).toBeInTheDocument();
        expect(screen.getByText('United Kingdom')).toBeInTheDocument();
        expect(screen.queryByText('France')).not.toBeInTheDocument();
    });
    it('should include dependent areas when includeDependentAreas is true', async () => {
        const user = userEvent.setup();
        const onChange = vi.fn();
        renderWithForm(_jsx(CountryCodeField, { ...defaultProps, includeDependentAreas: true, onChange: onChange }));
        const select = screen.getByRole('combobox');
        await user.click(select);
        expect(screen.getByText('Puerto Rico')).toBeInTheDocument(); // US territory
        expect(screen.getByText('Guam')).toBeInTheDocument(); // US territory
        await user.click(screen.getByText('Puerto Rico'));
        expect(onChange).toHaveBeenCalledWith('PR');
        expect(screen.getByText('Puerto Rico')).toBeInTheDocument();
    });
    it('should show an error if trying to submit empty with required enabled', async () => {
        const user = userEvent.setup();
        renderWithForm(_jsxs(_Fragment, { children: [_jsx(CountryCodeField, { ...defaultProps, required: true }), _jsx("button", { type: "submit", children: "Submit" })] }));
        await user.click(screen.getByText('Submit'));
        expect(screen.getByText('This field is required')).toBeInTheDocument();
    });
    it('should submit the country code', async () => {
        const onSubmit = vi.fn();
        const user = userEvent.setup();
        render(_jsxs(Form, { onSubmit: onSubmit, children: [_jsx(CountryCodeField, { ...defaultProps, value: "US" }), _jsx("button", { type: "submit", children: "Submit" })] }));
        await user.click(screen.getByText('Submit'));
        expect(onSubmit).toHaveBeenCalledWith({ country: 'US' });
    });
});
//# sourceMappingURL=country-code-field.test.js.map