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 { useMutation, useQueryClient } from "@tanstack/react-query";
import errorTracker from "~/utils/errorTracker";
import { useStateManagerContext } from "~/context/stateManager";
const REPORTS_ENDPOINT = `${import.meta.env.VITE_LEADS_REPORTS}`;
export function useLeadReports(params) {
    const queryClient = useQueryClient();
    const { sdkState } = useStateManagerContext();
    /**
     * Posts a Lead to the Leads Reports API.
     *
     * @returns A promise that resolves when the lead report is successfully posted.
     */
    const postLeadCapture = () => __awaiter(this, void 0, void 0, function* () {
        if (!params) {
            return;
        }
        const { lead: { addressLabel, location, listingUrl, reportType, contactInfo }, } = params;
        if (!addressLabel ||
            !location ||
            !(contactInfo === null || contactInfo === void 0 ? void 0 : contactInfo.firstName) ||
            !(contactInfo === null || contactInfo === void 0 ? void 0 : contactInfo.email) ||
            !(contactInfo === null || contactInfo === void 0 ? void 0 : contactInfo.lastName) ||
            (typeof location !== "string" && (!location.lat || !location.lng))) {
            return;
        }
        if (!sdkState.apiKey) {
            // Prevent calling reports-api without an API key
            throw new Error("API key is required to post lead capture report.");
        }
        const response = yield fetch(REPORTS_ENDPOINT, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                Authorization: `${sdkState.apiKey}`,
                "x-ll-sdk-name": "lead-capture",
            },
            body: JSON.stringify({
                address_label: addressLabel,
                location,
                listing_url: listingUrl,
                report_type: reportType,
                contact_info: {
                    first_name: contactInfo.firstName,
                    last_name: contactInfo.lastName,
                    email: contactInfo.email,
                    phone: contactInfo.phone,
                },
                custom_data: params.lead.customData,
            }),
        });
        if (!response.ok) {
            errorTracker.captureException(new Error(response.statusText));
            throw new Error("Failed to post lead capture report.");
        }
    });
    return {
        generate: useMutation({
            mutationKey: ["leadCapture", useLeadReports.name],
            mutationFn: postLeadCapture,
            onSuccess: () => {
                var _a, _b;
                (_a = queryClient.getQueryCache()) === null || _a === void 0 ? void 0 : _a.clear();
                (_b = params === null || params === void 0 ? void 0 : params.onMutationSuccess) === null || _b === void 0 ? void 0 : _b.call(params);
            },
            onError: (error) => {
                var _a;
                (_a = params === null || params === void 0 ? void 0 : params.onMutationError) === null || _a === void 0 ? void 0 : _a.call(params, error);
            },
        }),
    };
}
//# sourceMappingURL=index.js.map