import { QuestionGroupType, QuestionReferenceType, VehicleFlowTypes } from "@boldpenguin/sdk-types";
import { newSpecPage } from "@stencil/core/testing";
import { BpSdkVehicleGroup } from "./bp-sdk-vehicle-group";
const ymmChoice = { id: 'ymm', value: 'Year / Make / Model' };
const vinChoice = { id: 'vin', value: 'VIN' };
const trailerChoice = { id: 'trailer', value: 'Trailer' };
const setupPage = async () => {
    return await newSpecPage({
        html: '<bp-sdk-vehicle-group></bp-sdk-vehicle-group>',
        components: [BpSdkVehicleGroup],
    });
};
const setupInstance = (page) => {
    const instance = page.rootInstance;
    instance.questionGroup = {
        groupId: 'testQuestionGroupId',
        answers: [
            {
                id: 'asdf',
                answer_pool_id: '123',
                question: { choices: [ymmChoice, vinChoice, trailerChoice], reference_type: QuestionReferenceType.CommercialAutoFlow },
            },
        ],
        label: 'label',
        nestedGroupsWithAnswers: {},
        poolId: '123',
        parentQuestionAnswer: {},
        group_type: QuestionGroupType.Vehicle,
    };
    return instance;
};
describe('bp-sdk-vehicle-group', () => {
    describe('renders', () => {
        it('vanilla', async () => {
            const page = await setupPage();
            await page.waitForChanges();
            const element = page.root.querySelector('div');
            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('correct vehicle interface', () => {
            it(`case 'none'`, async () => {
                const page = await setupPage();
                const instance = setupInstance(page);
                expect(instance.selectedCAFlow).toEqual('none');
            });
            it(`case 'ymm'`, async () => {
                const page = await setupPage();
                const instance = setupInstance(page);
                instance.questionGroup = Object.assign(Object.assign({}, instance.questionGroup), { answers: [Object.assign(Object.assign({}, instance.questionGroup.answers[0]), { choice_id: ymmChoice.id })] });
                await page.waitForChanges();
                expect(instance.selectedCAFlow).toEqual('ymm');
            });
            it(`case 'vin'`, async () => {
                const page = await setupPage();
                const instance = setupInstance(page);
                instance.questionGroup = Object.assign(Object.assign({}, instance.questionGroup), { answers: [Object.assign(Object.assign({}, instance.questionGroup.answers[0]), { choice_id: vinChoice.id })] });
                await page.waitForChanges();
                expect(instance.selectedCAFlow).toEqual('vin');
            });
            it(`case 'trailer'`, async () => {
                const page = await setupPage();
                const instance = setupInstance(page);
                instance.questionGroup = Object.assign(Object.assign({}, instance.questionGroup), { answers: [Object.assign(Object.assign({}, instance.questionGroup.answers[0]), { choice_id: trailerChoice.id })] });
                await page.waitForChanges();
                expect(instance.selectedCAFlow).toEqual('trailer');
            });
        });
    });
    it('should get the flow answer', async () => {
        const page = await setupPage();
        const instance = setupInstance(page);
        expect(instance.flowAnswer.id).toBe('asdf');
    });
    describe('saveButtonClick', () => {
        it('emits ymm if not trailer', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance.saveVehicle, 'emit').mockImplementation(() => new CustomEvent(''));
            instance.selectedCAFlow = 'ymm';
            instance.saveButtonClick();
            expect(instance.saveVehicle.emit).toBeCalledTimes(1);
            expect(instance.saveVehicle.emit).toBeCalledWith(VehicleFlowTypes.ymm);
        });
        it('emits VIN if vin', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance.saveVehicle, 'emit').mockImplementation(() => new CustomEvent(''));
            instance.selectedCAFlow = 'vin';
            instance.saveButtonClick();
            expect(instance.saveVehicle.emit).toBeCalledTimes(1);
            expect(instance.saveVehicle.emit).toBeCalledWith(VehicleFlowTypes.vin);
        });
        it('emits Trailers if trailer', async () => {
            const page = await setupPage();
            const instance = setupInstance(page);
            jest.spyOn(instance.saveVehicle, 'emit').mockImplementation(() => new CustomEvent(''));
            instance.selectedCAFlow = 'trailer';
            instance.saveButtonClick();
            expect(instance.saveVehicle.emit).toBeCalledTimes(1);
            expect(instance.saveVehicle.emit).toBeCalledWith(VehicleFlowTypes.trailers);
        });
    });
});
