// Components
import cart from "@/fmg-components/cart/cart.vue";

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

import store from "@/store";

global.$logger = {
    logInformation: jest.fn(),
    logWarning: jest.fn(),
    logError: jest.fn(),
    logCritical: jest.fn(),
};

describe("cart.vue", () => {
    describe("cartItem computeds", () => {
        test("if there are one or more front wipers on the order, a front wiper cart item should be added to the cart", () => {
            // Arrange
            const lineItems = {
                glassParts: [],
                supportingItems: [],
                vaps: [
                    {
                        cartItemType: "FRONT WIPERS",
                        description: 'WIPER BLADE STANDARD 18"',
                        kitPrice: 0,
                        laborAmount: 0,
                        partNumber: "WB18",
                        partType: "FRONT WIPER",
                        salesTax: 2.44,
                        sellingPrice: 32.49,
                    },
                    {
                        cartItemType: "FRONT WIPERS",
                        description: 'WIPER BLADE STANDARD 26"',
                        kitPrice: 0,
                        laborAmount: 0,
                        partNumber: "WB26",
                        partType: "FRONT WIPER",
                        salesTax: 2.44,
                        sellingPrice: 32.49,
                    },
                ],
                promos: [],
            };

            const availableVaps = [];

            // Act
            const { wrapper } = setupMocks({
                props: {
                    modelValue: lineItems,
                    availableVaps: availableVaps,
                },
            });

            // Assert
            expect(wrapper.vm.frontWipersCartItem).not.toBeNull();
            expect(wrapper.vm.frontWipersCartItem.subTotal).toEqual(64.98);
            expect(wrapper.vm.frontWipersCartItem.salesTax).toEqual(4.88);

            const found =
                wrapper.vm.cartItems.findIndex(
                    (cartItem) => cartItem == wrapper.vm.frontWipersCartItem
                ) >= 0;
            expect(found).toBe(true);
        });

        test("if there are one or more rear wipers on the order, a rear wiper cart item should be added to the cart", () => {
            // Arrange
            const lineItems = {
                glassParts: [],
                supportingItems: [],
                vaps: [
                    {
                        cartItemType: "REAR WIPERS",
                        description: 'REAR WIPER BLADE STANDARD 18"',
                        kitPrice: 0,
                        laborAmount: 0,
                        partNumber: "EWB18",
                        partType: "REAR WIPER",
                        salesTax: 2.44,
                        sellingPrice: 32.49,
                    },
                    {
                        cartItemType: "REAR WIPERS",
                        description: 'REAR WIPER BLADE STANDARD 26"',
                        kitPrice: 0,
                        laborAmount: 0,
                        partNumber: "RWB26",
                        partType: "REAR WIPER",
                        salesTax: 2.44,
                        sellingPrice: 32.49,
                    },
                ],
                promos: [],
            };

            const availableVaps = [];

            // Act
            const { wrapper } = setupMocks({
                props: {
                    modelValue: lineItems,
                    availableVaps: availableVaps,
                },
            });

            // Assert
            expect(wrapper.vm.rearWipersCartItem).not.toBeNull();
            expect(wrapper.vm.rearWipersCartItem.subTotal).toEqual(64.98);
            expect(wrapper.vm.rearWipersCartItem.salesTax).toEqual(4.88);

            const found =
                wrapper.vm.cartItems.findIndex(
                    (cartItem) => cartItem == wrapper.vm.rearWipersCartItem
                ) >= 0;
            expect(found).toBe(true);
        });

        test("if there is Rain Defense on the order, a rain defense cart item should be added to the cart", () => {
            // Arrange
            const lineItems = {
                glassParts: [],
                supportingItems: [],
                vaps: [
                    {
                        cartItemType: "RAIN DEFENSE",
                        description: null,
                        kitPrice: 0,
                        laborAmount: 0,
                        listPrice: "44.99",
                        partNumber: "RAIN DEFENSE",
                        partType: "RAIN DEFENSE",
                        salesTax: 3.37,
                        sellingPrice: 44.99,
                    },
                ],
                promos: [],
            };

            const availableVaps = [];

            // Act
            const { wrapper } = setupMocks({
                props: {
                    modelValue: lineItems,
                    availableVaps: availableVaps,
                },
            });

            // Assert
            expect(wrapper.vm.rainDefenseCartItem).not.toBeNull();
            expect(wrapper.vm.rainDefenseCartItem.subTotal).toEqual(44.99);
            expect(wrapper.vm.rainDefenseCartItem.salesTax).toEqual(3.37);

            const found =
                wrapper.vm.cartItems.findIndex(
                    (cartItem) => cartItem == wrapper.vm.rainDefenseCartItem
                ) >= 0;
            expect(found).toBe(true);
        });

        // test("if there are glass parts on the order, a glass part cart item should be added to the cart but should not be displayed", () => {
        //     // Arrange
        //     const windshieldWithChildParts = {
        //         canSafeliteRecalibrate: true,
        //         childParts: [
        //             {
        //                 kitPrice: 0,
        //                 laborAmount: 23.55,
        //                 partNumber: "GGG FW4896",
        //                 salesTax: 1.77,
        //                 sellingPrice: 0,
        //             },
        //         ],
        //         color: "Green Tint",
        //         description:
        //             "solar, soundproofing, lane keep assist, lane departure warning system, w/adaptive cruise control",
        //         id: "db22fd44-10dd-456f-979b-ff88cf68cca6",
        //         kitPrice: 0,
        //         laborAmount: 60,
        //         partNumber: "FW04896GTYN",
        //         partType: "WINDSHIELD",
        //         recalibrationType: "STATIC",
        //         requiresCapabilityQuestions: false,
        //         requiresRecalibration: true,
        //         salesTax: 63.86,
        //         sellingPrice: 791.46,
        //     };

        //     const lineItems = {
        //         glassParts: [windshieldWithChildParts],
        //         supportingItems: [],
        //         vaps: [],
        //         promos: [],
        //     };

        //     const availableVaps = [];

        //     // Act
        //     const { wrapper } = setupMocks({
        //         props: {
        //             modelValue: lineItems,
        //             availableVaps: availableVaps,
        //         },
        //     });

        //     // Assert
        //     expect(wrapper.vm.glassPartCartItems[0]).not.toBeNull();
        //     expect(wrapper.vm.glassPartCartItems[0].subTotal).toEqual(875.01);
        //     expect(wrapper.vm.glassPartCartItems[0].salesTax).toEqual(65.63);
        //     expect(wrapper.vm.glassPartCartItems[0].isDisplayed).toEqual(false);

        //     const found =
        //         wrapper.vm.cartItems.findIndex(
        //             (cartItem) => cartItem == wrapper.vm.glassPartsCartItem
        //         ) >= 0;

        //     expect(found).toBe(true);
        // });

        // Recycle Fee Cart Item
        test("if there is recycling fee on the order, a recycle fee cart item should be added to the cart", () => {
            // Arrange
            const lineItems = {
                glassParts: [],
                supportingItems: [
                    {
                        description: null,
                        id: "9ff98501-03a4-432b-884e-e4e28f884a2f",
                        kitPrice: 0,
                        laborAmount: 34.98,
                        partNumber: "RECYCLE FEE",
                        partType: "REPLACE FEE",
                        salesTax: 2.62,
                        sellingPrice: 0,
                    },
                ],
                vaps: [],
                promos: [],
            };

            const availableVaps = [];

            // Act
            const { wrapper } = setupMocks({
                props: {
                    modelValue: lineItems,
                    availableVaps: availableVaps,
                },
            });

            // Assert
            expect(wrapper.vm.recycleFeeCartItem).not.toBeNull();
            expect(wrapper.vm.recycleFeeCartItem.subTotal).toEqual(34.98);
            expect(wrapper.vm.recycleFeeCartItem.salesTax).toEqual(2.62);

            const found =
                wrapper.vm.cartItems.findIndex(
                    (cartItem) => cartItem == wrapper.vm.recycleFeeCartItem
                ) >= 0;
            expect(found).toBe(true);
        });

        // Mobile Fee Cart Item
        test("if there is mobile fee on the order, a mobile fee cart item should be added to the cart", () => {
            // Arrange
            store.getters.order.isMSRFeeApplicable = false;
            const lineItems = {
                glassParts: [],
                supportingItems: [
                    {
                        description: null,
                        id: "9ff98501-03a4-432b-884e-e4e28f884a2f",
                        kitPrice: 0,
                        laborAmount: 34.98,
                        partNumber: "MOBILE FEE",
                        partType: "MOBILE FEE",
                        salesTax: 2.62,
                        sellingPrice: 0,
                    },
                ],
                vaps: [],
                promos: [],
            };

            const availableVaps = [];

            // Act
            const { wrapper } = setupMocks({
                props: {
                    modelValue: lineItems,
                    availableVaps: availableVaps,
                },
            });

            // Assert
            expect(wrapper.vm.mobileFeeCartItem).not.toBeNull();
            expect(wrapper.vm.mobileFeeCartItem.subTotal).toEqual(34.98);
            expect(wrapper.vm.mobileFeeCartItem.salesTax).toEqual(2.62);

            const found =
                wrapper.vm.cartItems.findIndex(
                    (cartItem) => cartItem == wrapper.vm.mobileFeeCartItem
                ) >= 0;
            expect(found).toBe(true);
        });

        // Service package discount Fee Cart Item
        test("only if there is service package discount fee for the package, a service package discount cart item should be added to the cart", () => {
            // Arrange
            const lineItems = {
                glassParts: [],
                supportingItems: [
                    {
                        description: null,
                        id: "bc00294e-6baa-403e-866e-52c267187a15",
                        kitPrice: 0,
                        laborAmount: 0,
                        partNumber: "DISC CASHSAVE70",
                        partType: "SERVICE PACKAGE DISCOUNT",
                        salesTax: null,
                        sellingPrice: -70,
                    },
                ],
                vaps: [],
                promos: [],
            };

            const availableVaps = [];

            // Act
            const { wrapper } = setupMocks({
                props: {
                    modelValue: lineItems,
                    availableVaps: availableVaps,
                },
            });
            // Assert
            expect(wrapper.vm.servicePackageDiscountCartItem).toBeNull();
        });

        // Other Supporting Items Cart Item
        test("if there are other supporting items on the order, an other supporting items cart item should be added to the cart but should not be displayed", () => {
            // Arrange
            const recalibrationLineItem = {
                cartItemType: "SUPPORTING ITEMS",
                description: null,
                id: "8ab6ef9b-8620-430a-8a92-29f7d4bb8b00",
                kitPrice: 0,
                laborAmount: 450,
                partNumber: "RECAL STATIC",
                partType: "RECALIBRATION",
                salesTax: 33.75,
                sellingPrice: 0,
            };

            const lineItems = {
                glassParts: [],
                supportingItems: [recalibrationLineItem],
                vaps: [],
                promos: [],
            };

            const availableVaps = [];

            // Act
            const { wrapper } = setupMocks({
                props: {
                    modelValue: lineItems,
                    availableVaps: availableVaps,
                },
            });

            // Assert
            expect(wrapper.vm.otherSupportingItemsCartItem).not.toBeNull();
            expect(wrapper.vm.otherSupportingItemsCartItem.subTotal).toEqual(450);
            expect(wrapper.vm.otherSupportingItemsCartItem.salesTax).toEqual(33.75);
            expect(wrapper.vm.otherSupportingItemsCartItem.isDisplayed).toEqual(false);

            const found =
                wrapper.vm.cartItems.findIndex(
                    (cartItem) => cartItem == wrapper.vm.otherSupportingItemsCartItem
                ) >= 0;
            expect(found).toBe(true);
        });
    });
});

function setupMocks({ options, props }) {
    const mountOptions = getMountOptions({
        ...options,
    });

    const mockBaseMixin = {
        methods: {
            getTierOnePackagePrice: jest.fn(),
            filterOutFees: jest.fn(),
            getCmsContent: jest.fn(),
        },
    };

    if (props) mountOptions.propsData = props;

    mountOptions.global.mixins = [mockBaseMixin];
    mountOptions.global.mocks["$store"] = store;

    const wrapper = shallowMount(cart, mountOptions);

    return { wrapper };
}
