import React, { useContext, useEffect, useMemo, useState } from "react";
import { TrackingPipelineEvents, AnalyticsEvents, } from "./types";
import { useTracking } from "~/tracking";
import { useStateManagerContext } from "~/context/stateManager";
import { useGeographies } from "~/features/clientLibrary/useGeographies";
import { useLocation } from "~/hooks/useLocation";
import useEffectOnce from "~/hooks/useEffectOnce";
const LeadCaptureContext = React.createContext({});
const useLeadCapture = () => useContext(LeadCaptureContext);
const LeadCaptureProvider = ({ children }) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
    const { sdkState } = useStateManagerContext();
    const tracking = useTracking();
    const [selectedReport, setSelectedReport] = useState(null);
    useEffectOnce(() => {
        tracking.postEvent({ eventType: TrackingPipelineEvents.SDK_VIEWED });
        tracking.trackAnalyticsEvent({
            eventName: AnalyticsEvents.SDK_VIEWED,
        });
    });
    const { isLoading: isGeographyLoading, data: geographyData } = useGeographies({
        geogID: (_a = sdkState.options) === null || _a === void 0 ? void 0 : _a.geographyId,
    });
    const geography = useMemo(() => {
        const geographiesArray = Object.values(geographyData || {});
        return geographiesArray[0];
    }, [isGeographyLoading, geographyData]);
    const { isLoading: isUseLocationLoading, data: locationData } = useLocation({
        lat: typeof ((_b = sdkState.options) === null || _b === void 0 ? void 0 : _b.geographyId) === "undefined"
            ? (_c = sdkState.options) === null || _c === void 0 ? void 0 : _c.lat
            : geography === null || geography === void 0 ? void 0 : geography.centroid.lat,
        lng: typeof ((_d = sdkState.options) === null || _d === void 0 ? void 0 : _d.geographyId) === "undefined"
            ? (_e = sdkState.options) === null || _e === void 0 ? void 0 : _e.lng
            : geography === null || geography === void 0 ? void 0 : geography.centroid.lng,
    });
    const [contactInfo, setContactInfo] = useState({
        firstName: "",
        lastName: "",
        email: "",
        phone: "",
        privacyPolicy: false,
    });
    // USA but non-California: pre-check privacy policy
    useEffect(() => {
        if (typeof locationData !== "undefined" &&
            locationData.country === "USA" &&
            !!locationData.state &&
            locationData.state !== "California") {
            setContactInfo((prev) => (Object.assign(Object.assign({}, prev), { privacyPolicy: true })));
        }
    }, [locationData === null || locationData === void 0 ? void 0 : locationData.state]);
    const addressLabel = useMemo(() => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
        if (((_a = sdkState.options) === null || _a === void 0 ? void 0 : _a.geographyId) &&
            typeof geographyData !== "undefined" &&
            !isGeographyLoading) {
            const geographiesArray = Object.values(geographyData || {});
            let geogName = ((_b = geographiesArray[0]) === null || _b === void 0 ? void 0 : _b.name.en) || "";
            if ((_d = (_c = geographiesArray[0]) === null || _c === void 0 ? void 0 : _c.city) === null || _d === void 0 ? void 0 : _d.en) {
                geogName = `${geogName}, ${(_e = geographiesArray[0]) === null || _e === void 0 ? void 0 : _e.city.en}`;
            }
            if ((_g = (_f = geographiesArray[0]) === null || _f === void 0 ? void 0 : _f.sub_country) === null || _g === void 0 ? void 0 : _g.en) {
                geogName = `${geogName}, ${(_h = geographiesArray[0]) === null || _h === void 0 ? void 0 : _h.sub_country.en}`;
            }
            return geogName;
        }
        // addressLabel can be an empty string because component defaults to a translation placeholder
        return (_k = (_j = sdkState.options) === null || _j === void 0 ? void 0 : _j.addressLabel) !== null && _k !== void 0 ? _k : "";
    }, [
        (_f = sdkState.options) === null || _f === void 0 ? void 0 : _f.addressLabel,
        (_g = sdkState.options) === null || _g === void 0 ? void 0 : _g.geographyId,
        isGeographyLoading,
        geographyData,
    ]);
    const contextValue = useMemo(() => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
        return ({
            isLoading: isUseLocationLoading,
            selectedReport,
            setSelectedReport,
            contactInfo,
            setContactInfo,
            isGeographyBased: ((_a = sdkState.options) === null || _a === void 0 ? void 0 : _a.geographyId)
                ? "geographyId"
                : "lat/lng",
            consentText: (_c = (_b = sdkState.options) === null || _b === void 0 ? void 0 : _b.consentText) !== null && _c !== void 0 ? _c : "",
            consentLinks: (_e = (_d = sdkState.options) === null || _d === void 0 ? void 0 : _d.consentLinks) !== null && _e !== void 0 ? _e : undefined,
            customData: (_g = (_f = sdkState.options) === null || _f === void 0 ? void 0 : _f.customData) !== null && _g !== void 0 ? _g : undefined,
            country: locationData === null || locationData === void 0 ? void 0 : locationData.country,
            state: locationData === null || locationData === void 0 ? void 0 : locationData.state,
            addressLabel,
            // If both a geog_id and lat/lng are provided, then the lat/lng should be ignored.
            location: ((_h = sdkState === null || sdkState === void 0 ? void 0 : sdkState.options) === null || _h === void 0 ? void 0 : _h.geographyId)
                ? (_j = sdkState === null || sdkState === void 0 ? void 0 : sdkState.options) === null || _j === void 0 ? void 0 : _j.geographyId
                : { lat: (_k = sdkState === null || sdkState === void 0 ? void 0 : sdkState.options) === null || _k === void 0 ? void 0 : _k.lat, lng: (_l = sdkState === null || sdkState === void 0 ? void 0 : sdkState.options) === null || _l === void 0 ? void 0 : _l.lng },
            listingUrl: ((_m = sdkState === null || sdkState === void 0 ? void 0 : sdkState.location) === null || _m === void 0 ? void 0 : _m.href) || document.referrer,
            hideAttribution: (_o = sdkState.options) === null || _o === void 0 ? void 0 : _o.hideAttribution,
        });
    }, [
        isUseLocationLoading,
        selectedReport,
        setSelectedReport,
        contactInfo,
        setContactInfo,
        (_h = sdkState.options) === null || _h === void 0 ? void 0 : _h.geographyId,
        (_j = sdkState.options) === null || _j === void 0 ? void 0 : _j.consentText,
        (_k = sdkState.options) === null || _k === void 0 ? void 0 : _k.consentLinks,
        (_l = sdkState.options) === null || _l === void 0 ? void 0 : _l.customData,
        (_m = sdkState.options) === null || _m === void 0 ? void 0 : _m.addressLabel,
        (_o = sdkState.options) === null || _o === void 0 ? void 0 : _o.lat,
        (_p = sdkState.options) === null || _p === void 0 ? void 0 : _p.lng,
        locationData === null || locationData === void 0 ? void 0 : locationData.country,
        locationData === null || locationData === void 0 ? void 0 : locationData.state,
        addressLabel,
        (_q = sdkState === null || sdkState === void 0 ? void 0 : sdkState.location) === null || _q === void 0 ? void 0 : _q.href,
        (_r = sdkState.options) === null || _r === void 0 ? void 0 : _r.hideAttribution,
    ]);
    return (React.createElement(LeadCaptureContext.Provider, { value: contextValue }, children));
};
export { useLeadCapture, LeadCaptureProvider };
//# sourceMappingURL=context.js.map