import React from 'react';
import { render } from '@testing-library/react';
import * as AppConfigModule from '@theorchard/suite-frontend';
import { mockAppConfig } from 'lib/test-helpers/factories';
import { MemoryRouter } from 'react-router-dom';
import { FEATURE_REACT_LEGAL_PAGES } from 'src/constants';
import IdentityContext from 'src/context';
import Footer, { HELP_LINK_ID } from '../footer';

jest.mock('@theorchard/suite-frontend', () => ({
    __esModule: true,
    ...jest.requireActual('@theorchard/suite-frontend'),
}));

describe('<Footer>', () => {
    const renderComponent = (identity: any) =>
        render(
            <IdentityContext.Provider value={{ identity } as any}>
                <MemoryRouter>
                    <Footer />
                </MemoryRouter>
            </IdentityContext.Provider>
        );

    afterEach(() => {
        jest.resetAllMocks();
    });

    describe.each([
        [
            'anonymous',
            {
                isAnonymous: (): boolean => true,
                hasFeatureFlag: (): boolean => false,
                isAwalUser: (): boolean => false,
            },
            false,
        ],
        [
            'authenticated',
            {
                isAnonymous: (): boolean => false,
                hasFeatureFlag: (): boolean => false,
                isAwalUser: (): boolean => false,
            },
            true,
        ],
        [
            'subaccount',
            {
                isAnonymous: (): boolean => false,
                subaccountId: 'mock-subaccount',
                hasFeatureFlag: (): boolean => false,
                isAwalUser: (): boolean => false,
            },
            false,
        ],
    ])('user is %s', (_, identity, supportLink) => {
        beforeEach(() => {
            jest.spyOn(AppConfigModule, 'useAppConfig').mockReturnValue(
                mockAppConfig
            );
        });

        test(`renders support link: ${supportLink}`, () => {
            const { container } = renderComponent(identity);
            const mailtoLinks =
                container.querySelectorAll('a[href^="mailto:"]');
            expect(mailtoLinks.length > 0).toEqual(supportLink);
        });

        test('renders help link', () => {
            const { container } = renderComponent(identity);
            expect(
                container.querySelector(`#${HELP_LINK_ID}`)
            ).toBeInTheDocument();
        });

        test('renders copyright year', () => {
            const year = new Date().getFullYear();
            const { getByText } = renderComponent(identity);
            expect(
                getByText(`Copyright © ${year} Orchard Enterprises NY, Inc.`)
            ).toBeInTheDocument();
        });
    });

    describe('is an AWAL user', () => {
        const identity = {
            isAnonymous: () => false,
            hasFeatureFlag: () => false,
            isAwalUser: () => true,
        };

        beforeEach(() => {
            jest.spyOn(AppConfigModule, 'useAppConfig').mockReturnValue({
                ...mockAppConfig,
                brand: 'awal',
            });
        });

        test('does not render support link', () => {
            const { container } = renderComponent(identity);

            const privacyLink = container.querySelector('#privacyPolicy');
            const supportLink = container.querySelector(`#${HELP_LINK_ID}`);

            expect(privacyLink).toBeInTheDocument();
            expect(supportLink).toBeNull();
        });
    });

    describe('legal links', () => {
        beforeEach(() => {
            jest.spyOn(AppConfigModule, 'useAppConfig').mockReturnValue({
                ...mockAppConfig,
                brand: 'awal',
            });
        });

        test('link is an external anchor (FooterLink) when FF is OFF', () => {
            const { container } = renderComponent({
                isAnonymous: () => false,
                isAwalUser: () => true,
                hasFeatureFlag: () => false,
            });
            const privacyLink = container.querySelector('#privacyPolicy')!;

            expect(privacyLink).toBeInTheDocument();
            expect(privacyLink.tagName.toLowerCase()).toEqual('a');
            expect(privacyLink).toHaveAttribute('href');
            // FooterLink renders with target="_blank"
            expect(privacyLink).toHaveAttribute('target', '_blank');
        });

        test('link is react-router Link when FF is ON', () => {
            const { container } = renderComponent({
                isAnonymous: () => false,
                isAwalUser: () => true,
                hasFeatureFlag: (ff: string) =>
                    ff === FEATURE_REACT_LEGAL_PAGES,
            });
            const privacyLink = container.querySelector('#privacyPolicy')!;

            expect(privacyLink).toBeInTheDocument();
            expect(privacyLink.tagName.toLowerCase()).toEqual('a');
            // react-router Link does not set a target attribute
            expect(privacyLink).not.toHaveAttribute('target');
        });
    });
});
