import { shallowMount } from "@vue/test-utils";
import sierraWebchat from "./sierra-webchat.vue";

jest.mock("@/mixins/analytics-mixin");
// Mock router for analytics-mixin
jest.mock("@/router", () => ({
    currentRoute: {
        value: {
            name: "mockedPageName",
        },
    },
    default: {
        currentRoute: {
            value: {
                name: "mockedPageName",
            },
        },
    },
}));

describe("sierraWebchat.vue", () => {
    let wrapper;

    beforeEach(() => {
        wrapper = shallowMount(sierraWebchat);
        // Mock global window properties
        window.sierraChat = { openChatModal: jest.fn(), closeChatModal: jest.fn() };
        window.sierra = undefined;
        window.SierraChat = undefined;
        window.embedded_svc = {
            bootstrapEmbeddedService: jest.fn(),
            liveAgentAPI: { startChat: jest.fn() },
            settings: {},
        };
    });

    afterEach(() => {
        wrapper.unmount();
        jest.clearAllMocks();
        delete window.sierraChat;
        delete window.embedded_svc;
    });

    it("should mount the component", () => {
        expect(wrapper.exists()).toBe(true);
    });

    it("calls openSierraChatModal when launchSierraChat is called and script is loaded", () => {
        const spy = jest.spyOn(wrapper.vm, "openSierraChatModal");
        // Simulate script already loaded
        document.body.appendChild(document.createElement("script")).id = "sierra-chat-embed";
        wrapper.vm.launchSierraChat();
        expect(spy).toHaveBeenCalled();
        document.getElementById("sierra-chat-embed").remove();
    });

    it("calls closeSierraChat when invoked", () => {
        wrapper.vm.closeSierraChat();
        expect(window.sierraChat.closeChatModal).toHaveBeenCalled();
    });

    it("dispatches sierra-chat-closed on handleSierraOnClose", () => {
        const eventSpy = jest.spyOn(window, "dispatchEvent");
        wrapper.vm.createSierraConfig().onClose();
        expect(eventSpy).toHaveBeenCalledWith(
            expect.objectContaining({ type: "sierra-chat-closed" })
        );
    });

    it("dispatches sierra-chat-transfer on handleSierraOnTransfer", () => {
        const eventSpy = jest.spyOn(window, "dispatchEvent");
        const transfer = {
            data: { first_name: "A", last_name: "B", email: "a@b.com", chat_summary: "summary" },
        };
        wrapper.vm.createSierraConfig().onTransfer(transfer);
        expect(eventSpy).toHaveBeenCalledWith(
            expect.objectContaining({ type: "sierra-chat-transfer" })
        );
    });

    it("handles sierra-chat-transfer event and starts Salesforce chat with prepopulated fields", () => {
        const transfer = {
            data: {
                first_name: "A",
                last_name: "B",
                email: "a@b.com",
                chat_summary: "summary",
            },
        };
        const event = new CustomEvent("sierra-chat-transfer", { detail: transfer });
        window.dispatchEvent(event);
        expect(window.sierraChat.closeChatModal).toHaveBeenCalled();
        expect(window.embedded_svc.settings.prepopulatedPrechatFields).toEqual({
            FirstName: "A",
            LastName: "B",
            Email: "a@b.com",
            Subject: "summary",
        });
        expect(window.embedded_svc.liveAgentAPI.startChat).toHaveBeenCalled();
    });
});
