var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import React from "react";
import { describe, beforeEach, it, expect, vi } from "vitest";
import { renderHook, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useLeadReports } from "./index";
const queryClient = new QueryClient();
const wrapper = ({ children }) => (React.createElement(QueryClientProvider, { client: queryClient }, children));
const mockSDKState = vi.fn();
vi.mock("~/context/stateManager", () => ({
    useStateManagerContext: () => mockSDKState(),
}));
const fetchMock = vi.fn(() => Promise.resolve({
    status: 201,
    statusText: "",
    ok: true,
    redirected: false,
    bodyUsed: false,
    type: "cors",
    url: "https://reports-api-qa.locallogic.co/v1/leads-reports",
}));
global.fetch = fetchMock;
describe("useLeadReports", () => {
    beforeEach(() => {
        vi.resetAllMocks();
    });
    it("should return the correct initial state", () => __awaiter(void 0, void 0, void 0, function* () {
        mockSDKState.mockReturnValue({
            sdkState: {
                apiKey: "test-api-key",
                globalOptions: {},
                options: {},
                location: { lat: 40.7128, lng: -74.006 },
            },
            setSdkState: vi.fn(),
            onChange: vi.fn(),
            followerNode: undefined,
        });
        const { result } = renderHook(() => useLeadReports({
            lead: {
                addressLabel: "123 Main St",
                location: { lat: 40.7128, lng: -74.006 },
                listingUrl: "http://example.com",
                reportType: "nmr",
                contactInfo: {
                    firstName: "Jane",
                    lastName: "Doe",
                    email: "jane.doe@example.com",
                    privacyPolicy: true,
                },
            },
        }), { wrapper });
        yield waitFor(() => {
            result.current.generate.mutate();
        });
        expect(result.current.generate.isPending).toBe(false);
        expect(result.current.generate.isPaused).toBe(false);
        expect(result.current.generate.data).toBeUndefined();
    }));
    it("should post lead to backend reports", () => __awaiter(void 0, void 0, void 0, function* () {
        mockSDKState.mockReturnValue({
            sdkState: {
                apiKey: "test-api-key",
                globalOptions: {},
                options: {},
                location: { lat: 40.7128, lng: -74.006 },
            },
            setSdkState: vi.fn(),
            onChange: vi.fn(),
            followerNode: undefined,
        });
        fetchMock.mockReturnValueOnce(Promise.resolve({
            status: 201,
            statusText: "",
            ok: true,
            redirected: false,
            bodyUsed: false,
            type: "cors",
            url: "https://reports-api-qa.locallogic.co/v1/leads-reports",
        }));
        const { result } = renderHook(() => useLeadReports({
            lead: {
                addressLabel: "123 Main St",
                location: { lat: 45.5282164, lng: -73.5978527 },
                listingUrl: "http://example.com",
                reportType: "nmr",
                contactInfo: {
                    firstName: "Jane",
                    lastName: "Doe",
                    email: "jane.doe@example.com",
                    phone: "123-456-7890",
                    privacyPolicy: true,
                },
            },
        }), { wrapper });
        yield waitFor(() => {
            result.current.generate.mutate();
        });
        yield waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(1));
        expect(result.current.generate.isSuccess).toBe(true);
        expect(fetchMock).toHaveBeenCalledWith(`${import.meta.env.VITE_LEADS_REPORTS}`, expect.objectContaining({
            method: "POST",
            headers: expect.objectContaining({
                Authorization: "test-api-key",
                "Content-Type": "application/json",
                "x-ll-sdk-name": "lead-capture",
            }),
        }));
    }));
    it("should throw an error if there's no apiKey", () => __awaiter(void 0, void 0, void 0, function* () {
        var _a;
        mockSDKState.mockReturnValue({
            sdkState: {
                apiKey: "",
                globalOptions: {},
                options: {},
                location: { lat: 40.7128, lng: -74.006 },
            },
            setSdkState: vi.fn(),
            onChange: vi.fn(),
            followerNode: undefined,
        });
        fetchMock.mockReturnValueOnce(Promise.resolve({
            status: 201,
            statusText: "",
            ok: true,
            redirected: false,
            bodyUsed: false,
            type: "cors",
            url: "https://reports-api-qa.locallogic.co/v1/leads-reports",
        }));
        const { result } = renderHook(() => useLeadReports({
            lead: {
                addressLabel: "123 Main St",
                location: { lat: 45.5282164, lng: -73.5978527 },
                listingUrl: "http://example.com",
                reportType: "nmr",
                contactInfo: {
                    firstName: "Jane",
                    lastName: "Doe",
                    email: "jane.doe@example.com",
                    phone: "123-456-7890",
                    privacyPolicy: true,
                },
            },
        }), { wrapper });
        yield waitFor(() => {
            result.current.generate.mutate();
        });
        yield waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(0));
        expect(result.current.generate.isError).toBe(true);
        expect((_a = result.current.generate.error) === null || _a === void 0 ? void 0 : _a.message).toEqual("API key is required to post lead capture report.");
    }));
});
//# sourceMappingURL=index.test.js.map