import { newSpecPage } from "@stencil/core/testing";
import { BpSdkStepperGroupContainer } from "./bp-sdk-stepper-group-container";
import { mockMapQuestionGroupAnswer, mockParentQuestionAnswerBuilder } from "../../mocks/mockMappedQuestionGroupAnswers";
import { mockAnswer } from "../../mocks/answer";
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-stepper-group-container></bp-sdk-stepper-group-container>',
        components: [BpSdkStepperGroupContainer],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    instance.questionGroup = mockMapQuestionGroupAnswer;
    instance.questionGroup.nestedGroupsWithAnswers.get = (key) => ({});
    return instance;
};
const mockNestedGroupHas = { has: (id) => false };
describe('bp-sdk-stepper-group-container', () => {
    it('renders', async () => {
        const page = await setupPage();
        setupInstance(page);
        await page.waitForChanges();
        const element = page.root.querySelector('div');
        expect(element).toBeTruthy();
        expect(page.root.querySelector('bp-sdk-external-stepper-group-container')).toBeFalsy();
    });
    it('should set the currentActiveStepper', async () => {
        const page = await setupPage();
        const instance = setupInstance(page);
        instance.questionGroup = mockMapQuestionGroupAnswer;
        instance.questionGroup.nestedGroupsWithAnswers.get = () => ({
            answers: [mockAnswer],
            nestedGroupsWithAnswers: mockNestedGroupHas,
        });
        const firstAnswer = mockParentQuestionAnswerBuilder();
        const secondAnswer = mockParentQuestionAnswerBuilder();
        const testGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { answers: [firstAnswer, secondAnswer] });
        instance.questionGroup = testGroup;
        await page.waitForChanges();
        // Show that the currentActiveStepper is the firstAnswers and previous step is not rendered
        expect(instance.currentActiveStepper).toEqual(0);
        expect(instance.previousStep).toBeNull();
        // On click, the second answer is current and previous step is rendered.
        instance.handleStepperClick(secondAnswer.id);
        expect(instance.currentActiveStepper).toEqual(1);
        expect(instance.previousStep).toEqual(firstAnswer);
        expect(instance.nextStep).toBeNull();
    });
    describe('disableButton', () => {
        const choiceAnswer = Object.assign(Object.assign({}, mockAnswer), { choice_id: null, value: null });
        const choiceIdsAnswer = Object.assign(Object.assign({}, mockAnswer), { choice_ids: [], value: null });
        const requiredAnswer = Object.assign(Object.assign({}, mockAnswer), { value: null });
        const unRequiredAnswer = Object.assign(Object.assign({}, mockAnswer), { required: false, value: null });
        it('should be false if there is a choice_id', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const testGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { answers: [mockAnswer], nestedGroupsWithAnswers: {
                    get: (key) => ({ answers: [choiceAnswer], nestedGroupsWithAnswers: mockNestedGroupHas }),
                } });
            instance.questionGroup = testGroup;
            await page.waitForChanges();
            expect(instance.currentActiveStepperId).toEqual(mockAnswer.id);
            expect(instance.buttonDisable).toBeTruthy();
            instance.questionGroup.nestedGroupsWithAnswers.get = (key) => ({ answers: [Object.assign(Object.assign({}, choiceAnswer), { choice_id: '123 ' })], nestedGroupsWithAnswers: mockNestedGroupHas });
            instance.onQuestionGroupChange(testGroup);
            expect(instance.buttonDisable).toBeFalsy();
        });
        it('should be false if there are choice_ids', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const testGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { answers: [mockAnswer], nestedGroupsWithAnswers: {
                    get: (key) => ({ answers: [choiceIdsAnswer], nestedGroupsWithAnswers: mockNestedGroupHas }),
                    has: (id) => false,
                } });
            instance.questionGroup = testGroup;
            await page.waitForChanges();
            expect(instance.currentActiveStepperId).toEqual(mockAnswer.id);
            expect(instance.buttonDisable).toBeTruthy();
            instance.questionGroup.nestedGroupsWithAnswers.get = (key) => ({
                answers: [Object.assign(Object.assign({}, choiceIdsAnswer), { choice_ids: ['123'] })],
                nestedGroupsWithAnswers: mockNestedGroupHas,
            });
            instance.onQuestionGroupChange(testGroup);
            expect(instance.buttonDisable).toBeFalsy();
        });
        it('should be false if there is a value', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const testGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { answers: [mockAnswer], nestedGroupsWithAnswers: {
                    get: (key) => ({ answers: [requiredAnswer], nestedGroupsWithAnswers: mockNestedGroupHas }),
                    has: (id) => false,
                } });
            instance.questionGroup = testGroup;
            await page.waitForChanges();
            expect(instance.currentActiveStepperId).toEqual(mockAnswer.id);
            expect(instance.buttonDisable).toBeTruthy();
            instance.questionGroup.nestedGroupsWithAnswers.get = (key) => ({ answers: [Object.assign(Object.assign({}, requiredAnswer), { value: '1234' })], nestedGroupsWithAnswers: mockNestedGroupHas });
            instance.onQuestionGroupChange(testGroup);
            expect(instance.buttonDisable).toBeFalsy();
        });
        it('should be false if answer is not required', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const testGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { answers: [mockAnswer], nestedGroupsWithAnswers: {
                    get: (key) => ({ answers: [unRequiredAnswer], nestedGroupsWithAnswers: mockNestedGroupHas }),
                    has: (id) => false,
                } });
            instance.questionGroup = testGroup;
            await page.waitForChanges();
            expect(instance.currentActiveStepperId).toEqual(mockAnswer.id);
            expect(instance.buttonDisable).toBeFalsy();
        });
        it('should be true only if all answers are valid', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            const testGroup = Object.assign(Object.assign({}, mockMapQuestionGroupAnswer), { answers: [mockAnswer], nestedGroupsWithAnswers: {
                    get: (key) => ({
                        answers: [choiceAnswer, choiceIdsAnswer, requiredAnswer, unRequiredAnswer],
                        nestedGroupsWithAnswers: mockNestedGroupHas,
                    }),
                    has: (id) => false,
                } });
            instance.questionGroup = testGroup;
            await page.waitForChanges();
            expect(instance.currentActiveStepperId).toEqual(mockAnswer.id);
            expect(instance.buttonDisable).toBeTruthy();
            instance.questionGroup.nestedGroupsWithAnswers.get = (key) => ({
                answers: [choiceAnswer, choiceIdsAnswer, Object.assign(Object.assign({}, requiredAnswer), { value: '1234' }), unRequiredAnswer],
                nestedGroupsWithAnswers: mockNestedGroupHas,
            });
            instance.onQuestionGroupChange(testGroup);
            expect(instance.buttonDisable).toBeTruthy();
            instance.questionGroup.nestedGroupsWithAnswers.get = (key) => ({
                answers: [choiceAnswer, Object.assign(Object.assign({}, choiceIdsAnswer), { choice_ids: ['123'] }), Object.assign(Object.assign({}, requiredAnswer), { value: '1234' }), unRequiredAnswer],
                nestedGroupsWithAnswers: mockNestedGroupHas,
            });
            instance.onQuestionGroupChange(testGroup);
            expect(instance.buttonDisable).toBeTruthy();
            instance.questionGroup.nestedGroupsWithAnswers.get = (key) => ({
                answers: [
                    Object.assign(Object.assign({}, choiceAnswer), { choice_id: '123' }),
                    Object.assign(Object.assign({}, choiceIdsAnswer), { choice_ids: ['123'] }),
                    Object.assign(Object.assign({}, requiredAnswer), { value: '1234' }),
                    unRequiredAnswer,
                ],
                nestedGroupsWithAnswers: mockNestedGroupHas,
            });
            instance.onQuestionGroupChange(testGroup);
            expect(instance.buttonDisable).toBeFalsy();
        });
    });
    it('renders external component if defined', async () => {
        jest.spyOn(customElements, 'whenDefined').mockReturnValueOnce(Promise.resolve());
        const page = await setupPage();
        await page.waitForChanges();
        expect(page.root.querySelector('bp-sdk-external-stepper-group-container')).toBeTruthy();
    });
});
