import { QuestionGroupType } from "@boldpenguin/sdk-types";
import { newSpecPage } from "@stencil/core/testing";
import { mockQuestionBuilder } from "../../mocks/answer";
import { mockMapQuestionGroupAnswer } from "../../mocks/mockMappedQuestionGroupAnswers";
import { BpSdkAddressGroupContainer } from "./bp-sdk-address-group-container";
const addressValidationOptions = {
    includePartialQuestionCodes: ['_address_'],
    excludePartialQuestionCodes: ['_vehicle_'],
};
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-address-group-container></bp-sdk-address-group-container>',
        components: [BpSdkAddressGroupContainer],
    });
};
describe('bp-sdk-address-group-container', () => {
    describe('render', () => {
        it('renders', async () => {
            const page = await setupPage();
            const instance = page.rootInstance;
            instance.answers = [];
            await page.waitForChanges();
            const element = page.body.querySelector('bp-sdk-address-group-container');
            expect(element).toBeTruthy();
        });
        it('renders external component if defined', async () => {
            const whenDefinedMock = jest.spyOn(customElements, 'whenDefined').mockResolvedValueOnce({});
            const page = await setupPage();
            await page.waitForChanges();
            const element = page.root.querySelector('div');
            expect(element).toBeFalsy();
            whenDefinedMock.mockRestore();
        });
    });
    describe('mapStateToProps', () => {
        it('returns enableAddressValidation', async () => {
            const page = await setupPage();
            await page.waitForChanges();
            const instance = page.rootInstance;
            const mockState = {
                application: {
                    form: {
                        locked: true,
                        owner_id: '4',
                    },
                    questionSets: {
                        list: [
                            {
                                question_groups: [
                                    {
                                        id: '1234567890',
                                    },
                                ],
                            },
                        ],
                    },
                },
                auth: { userId: '4' },
                viewState: { addressValidation: { shouldOverrideValidationTrigger: false, validations: {} }, features: { list: [] } },
                config: {
                    options: { enableAddressValidation: true },
                },
            };
            expect(instance.mapStateToProps(mockState).enableAddressValidation).toBe(true);
        });
        it('returns addressValidationOptions', async () => {
            const page = await setupPage();
            await page.waitForChanges();
            const instance = page.rootInstance;
            const mockState = {
                application: {
                    form: {
                        locked: true,
                        owner_id: '4',
                    },
                    questionSets: {
                        list: [
                            {
                                question_groups: [
                                    {
                                        id: '1234567890',
                                    },
                                ],
                            },
                        ],
                    },
                },
                auth: { userId: '4' },
                viewState: { addressValidation: { shouldOverrideValidationTrigger: false, validations: {} }, features: { list: [] } },
                config: {
                    options: { addressValidationOptions },
                },
            };
            expect(instance.mapStateToProps(mockState).addressValidationOptions).toBe(addressValidationOptions);
        });
    });
    describe('useAddressValidation', () => {
        describe('when question group type is validated address', () => {
            it('should be false when enableAddressValidation is false', async () => {
                const page = await setupPage();
                const instance = page.rootInstance;
                instance.enableAddressValidation = false;
                instance.isAddressValidationFeatureEnabled = true;
                instance.questionGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { group_type: QuestionGroupType.ValidatedAddress });
                await page.waitForChanges();
                expect(instance.useAddressValidation).toBe(false);
            });
            it('should be true when enableAddressValidation is true', async () => {
                const page = await setupPage();
                const instance = page.rootInstance;
                instance.enableAddressValidation = true;
                instance.isAddressValidationFeatureEnabled = true;
                instance.questionGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { group_type: QuestionGroupType.ValidatedAddress });
                await page.waitForChanges();
                expect(instance.useAddressValidation).toBe(true);
            });
        });
        describe('when question group type is address', () => {
            it('should be false when enableAddressValidation is false', async () => {
                const page = await setupPage();
                const instance = page.rootInstance;
                instance.isAddressValidationFeatureEnabled = true;
                instance.enableAddressValidation = false;
                instance.questionGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { group_type: QuestionGroupType.Address, parentQuestionAnswer: {
                        question: Object.assign(Object.assign({}, mockQuestionBuilder()), { code: 'my_address_1_anchor' }),
                    } });
                await page.waitForChanges();
                expect(instance.useAddressValidation).toBe(false);
            });
            it.each([
                [undefined],
                [
                    {
                        includePartialQuestionCodes: [],
                        excludePartialQuestionCodes: [],
                    },
                ],
            ])('should be false when no address validation options are configured', async (options) => {
                const page = await setupPage();
                const instance = page.rootInstance;
                instance.enableAddressValidation = true;
                instance.isAddressValidationFeatureEnabled = true;
                instance.addressValidationOptions = options;
                instance.questionGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { group_type: QuestionGroupType.Address, parentQuestionAnswer: {
                        question: Object.assign(Object.assign({}, mockQuestionBuilder()), { code: 'my_address_1_anchor' }),
                    } });
                await page.waitForChanges();
                expect(instance.useAddressValidation).toBe(false);
            });
            it('should be false when parent question code is not included', async () => {
                const page = await setupPage();
                const instance = page.rootInstance;
                instance.enableAddressValidation = true;
                instance.isAddressValidationFeatureEnabled = true;
                instance.addressValidationOptions = addressValidationOptions;
                instance.questionGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { group_type: QuestionGroupType.Address, parentQuestionAnswer: {
                        question: Object.assign(Object.assign({}, mockQuestionBuilder()), { code: 'my_bubbles_1_anchor' }),
                    } });
                await page.waitForChanges();
                expect(instance.useAddressValidation).toBe(false);
            });
            it('should be false when parent question code is excluded', async () => {
                const page = await setupPage();
                const instance = page.rootInstance;
                instance.enableAddressValidation = true;
                instance.isAddressValidationFeatureEnabled = true;
                instance.addressValidationOptions = addressValidationOptions;
                instance.questionGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { group_type: QuestionGroupType.Address, parentQuestionAnswer: {
                        question: Object.assign(Object.assign({}, mockQuestionBuilder()), { code: 'my_vehicle_address_1_anchor' }),
                    } });
                await page.waitForChanges();
                expect(instance.useAddressValidation).toBe(false);
            });
            it('should be true when parent question code is included and not excluded', async () => {
                const page = await setupPage();
                const instance = page.rootInstance;
                instance.enableAddressValidation = true;
                instance.isAddressValidationFeatureEnabled = true;
                instance.addressValidationOptions = addressValidationOptions;
                instance.questionGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { group_type: QuestionGroupType.Address, parentQuestionAnswer: {
                        question: Object.assign(Object.assign({}, mockQuestionBuilder()), { code: 'my_address_1_anchor' }),
                    } });
                await page.waitForChanges();
                expect(instance.useAddressValidation).toBe(true);
            });
            it('should be false when address validation feature is not enabled', async () => {
                const page = await setupPage();
                const instance = page.rootInstance;
                instance.enableAddressValidation = true;
                instance.isAddressValidationFeatureEnabled = false;
                instance.addressValidationOptions = addressValidationOptions;
                instance.questionGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { group_type: QuestionGroupType.Address, parentQuestionAnswer: {
                        question: Object.assign(Object.assign({}, mockQuestionBuilder()), { code: 'my_address_1_anchor' }),
                    } });
                await page.waitForChanges();
                expect(instance.useAddressValidation).toBe(false);
            });
        });
    });
});
