// Components
import customerDetails from "@/layouts/payment-method/payment-method.vue";

// Supporting Files
import { shallowMount } from "@vue/test-utils";
import { getMountOptions } from "@/helpers/unit-test-helper.js";
import store from "@/store";
import { experimentSettings } from "@/constants/experiments";

// Mock our module for promises.
jest.mock("@/helpers/layout-helper.js", () => ({
    settleAllPromises: jest.fn(),
}));

jest.mock("@/helpers/heritage-integration/order-helper.js", () => ({
    submitWorkOrder: jest.fn(),
}));

jest.mock("@/helpers/pricing-helper.js", () => ({
    getAmountDue: jest.fn(),
}));

let piaDisabledFlag = false;

describe("payment-method.vue", () => {
    describe("navigation", () => {
        test("if the back button is clicked, navigate back", async () => {
            // Arrange
            const { wrapper } = setupMocks();

            // Act
            await wrapper.vm.backButtonAction();

            // Assert
            expect(wrapper.vm.$router.navigateWithoutSaving).toHaveBeenCalled();
        });

        test("if the continue button is clicked, navigate forward", async () => {
            // Arrange
            const { wrapper } = setupMocks();

            // Act
            await wrapper.vm.forwardButtonAction();

            // Assert
            expect(wrapper.vm.$router.navigateWithoutSaving).toHaveBeenCalled();
        });
    });
});

function setupMocks() {
    store.getters = {
        damage: {},
        lineItems: {
            glassParts: [],
            supportingItems: [],
            vaps: [],
            promos: [],
        },
        order: {
            payment: {
                isPia: false,
                insuranceCoverage: {
                    isVerified: true,
                },
            },
            lineItems: {
                glassParts: [],
                supportingItems: [],
                vaps: [],
                promos: [],
            },
            policy: {
                currentDeductible: 123,
                isNoComp: false,
                isItac: false,
            },
            serviceLocation: {
                appointmentType: "Mobile",
                address: "123 Test Ave",
                city: "Anytown",
                state: "OH",
                zipCode: "00000",
            },
        },
        policy: {
            currentDeductible: 123,
        },
        payment: {
            isInsurance: false,
        },
    };

    const mountOptions = getMountOptions({
        route: { query: {}, params: {} },
        router: {
            navigate: jest.fn(),
            navigateWithSaving: jest.fn(),
            navigateWithoutSaving: jest.fn(),
        },
        store: {
            getters: store.getters,
        },
    });

    const mockMixin = {
        methods: {
            getSettingValue: jest.fn((settingName) => {
                if (!piaDisabledFlag) {
                    if (settingName === experimentSettings.PIA_EXPERIENCE) {
                        return "PIA Optional";
                    }
                    if (settingName === experimentSettings.SUBMIT_ORDER_ENABLE_PIA) {
                        return "true";
                    }
                }

                return "false";
            }),
            hasSettingEqualTo: jest.fn((settingName) => {
                if (settingName === experimentSettings.DISPLAY_AFTERPAY_BREAKOUT_DISPLAY) {
                    return true;
                }
                return false;
            }),
            getCmsContent: jest.fn().mockImplementation(() => {
                return "test text";
            }),
        },
    };

    mountOptions.global.mixins = [mockMixin];

    const wrapper = shallowMount(customerDetails, mountOptions);

    wrapper.vm.$refs.loadingModal.showModal = jest.fn();

    return { wrapper };
}
