import { render } from '@testing-library/react';
import { getSettings } from '@/actions/getSettings';
import { Footer } from '@/components/Footer';
import DefaultFooter from './default';
import type { Mock } from 'vitest';

const privacyLinks = [
    { url: '', name: 'Privacy Policy' },
    { url: '', name: 'How We Use Your Data' },
    {
        url: '',
        name: 'Your California Privacy Rights',
    },
    {
        url: '',
        name: 'Do Not Sell My Personal Information',
    },
];

const legalEntity = {
    name: 'Sony Music US - Sony Music Now',
    address: '25 Madison Ave, New York',
};

vi.mock('@/actions/getSettings', () => ({
    getSettings: vi.fn(() => ({
        data: { privacyLinks: { en: privacyLinks }, legalEntity },
    })),
}));

vi.mock('@/components/Footer', () => ({
    Footer: vi.fn(() => <div>Footer</div>),
}));

describe('<DefaultFooter>', () => {
    afterEach(() => {
        vi.clearAllMocks();
    });

    describe('when search params has "IPCountry"', () => {
        it('should call "getSettings" with correct params', async () => {
            render(
                await DefaultFooter({
                    searchParams: Promise.resolve({ IPCountry: 'EE' }),
                })
            );

            expect(getSettings).toHaveBeenCalledWith({
                IPCountry: 'EE',
            });
        });
    });

    describe('when "getSettings" resolved with "data"', () => {
        it('should render "<Footer>" with correct props', async () => {
            render(await DefaultFooter({}));

            expect(Footer).toHaveBeenCalledWith(
                {
                    links: {
                        en: privacyLinks.map(link => ({
                            url: link.url,
                            name: link.name,
                        })),
                    },
                    legalEntity,
                },
                undefined
            );
        });
    });

    describe('when "getSettings" returns an error', () => {
        describe('when error type is any other', () => {
            it('should return null', async () => {
                const error = { type: 'BAD_REQUEST' };

                (getSettings as Mock).mockResolvedValue({ error });

                const result = await DefaultFooter({});

                expect(result).toBeNull();
            });
        });
    });
});
