import { shallowMount } from "@vue/test-utils";
import { getMountOptions } from "@/helpers/unit-test-helper.js";
import serviceLocationReview from "@/layouts/payment-method/review-dropdown/review-sections/service-location-review/service-location-review";

import { AppointmentTypeStrings } from "@/constants/schedule-constants";

const cmsContent = {
    ServiceLocationTitleWidget: {
        Text: "Title Text",
    },
};

describe("Service Location Review Block", () => {
    it("Should render mobile address if mobile appointment", async () => {
        // Arrange
        const props = generateDefaultProps();

        const { wrapper } = setupMocks({
            propsData: props,
        });

        // Act
        await wrapper.vm.$nextTick();

        // Assert
        expect(wrapper.vm.displayContent).toEqual([
            "mobile address 1, mobile address 2, mobile city, MO 11111",
        ]);
    });

    it("Should render service location address if inshop appointment.", async () => {
        // Arrange
        let props = generateDefaultProps();

        props.serviceLocation.appointmentType = AppointmentTypeStrings.IN_SHOP;

        const { wrapper } = setupMocks({
            propsData: props,
        });

        // Act
        await wrapper.vm.$nextTick();

        // Assert
        expect(wrapper.vm.displayContent).toEqual([
            "service location address, service location city, SL 22222",
        ]);
    });

    it("Should render service location address if drop-off appointment", async () => {
        // Arrange
        let props = generateDefaultProps();

        props.serviceLocation.appointmentType = AppointmentTypeStrings.DROP_OFF;

        const { wrapper } = setupMocks({
            propsData: props,
        });

        // Act
        await wrapper.vm.$nextTick();

        // Assert
        expect(wrapper.vm.displayContent).toEqual([
            "service location address, service location city, SL 22222",
        ]);
    });

    it("Should not add comma or any text if address2 is null", async () => {
        // Arrange
        let props = generateDefaultProps();

        props.serviceLocation.address2 = null;

        const { wrapper } = setupMocks({
            propsData: props,
        });

        // Act
        await wrapper.vm.$nextTick();

        // Assert
        expect(wrapper.vm.displayContent).toEqual(["mobile address 1, mobile city, MO 11111"]);
    });
});

function generateDefaultProps() {
    return {
        cmsWidgetName: "ServiceLocationTitleWidget",
        serviceLocation: {
            address: "mobile address 1",
            address2: "mobile address 2",
            city: "mobile city",
            state: "MO",
            zipCode: "11111",
            zipCodeCtu: "",
            appointmentType: AppointmentTypeStrings.MOBILE,
            isVehicleProtected: false,
            provider: {
                providerNumber: "",
                address: {
                    streetAddress: "service location address",
                    city: "service location city",
                    state: "SL",
                    zipCode: "22222",
                    zipCodeCtu: "",
                },
            },
        },
    };
}

function setupMocks(customMountOptions) {
    const mountOptions = getMountOptions(customMountOptions);

    const mockMixin = {
        methods: {
            getCmsContent: jest.fn((widgetName, cmsFieldName) => {
                return cmsContent?.[widgetName]?.[cmsFieldName] ?? "";
            }),
        },
    };

    mountOptions.global.mixins = [mockMixin];

    const wrapper = shallowMount(serviceLocationReview, mountOptions);
    wrapper.vm.setCmsContent = jest.fn();
    return { wrapper };
}
