import crypto from "crypto";
import { useCallback } from "react";
const placeholder = () => console.error(new Error("gtag not found"));
export const gtagTracking = () => {
    return globalThis.gtag ?? placeholder;
};
export const useGtag = () => {
    const gtag = gtagTracking();
    const track = useCallback((trackType, eventName, options) => gtag(trackType, eventName, options), [gtag]);
    const trackEvent = useCallback((eventName, options) => {
        // @ts-ignore
        return track("event", eventName, options);
    }, [track]);
    return { track, trackEvent };
};
export const useGtagTracking = () => {
    const gtag = gtagTracking();
    const trackEvent = useCallback(gtag, [gtag]);
    return trackEvent;
};
export const useGtagTrackUser = () => {
    const gtag = gtagTracking();
    const trackUser = useCallback((name) => {
        const hash = crypto.createHash("sha256").update(name).digest("hex");
        gtag("set", {
            user_id: hash,
        });
    }, [gtag]);
    return trackUser;
};
export const useGtagConfig = ( /* TODO config in args */) => {
    const gtag = gtagTracking();
    const trackConfig = useCallback(() => {
        gtag("config", "UA-136203948-1", {
            custom_map: {
                dimension1: "user_id",
                dimension2: "clientId",
                dimension4: "sessionId",
            },
            linker: {
                domains: [
                    "eosdt.com",
                    "blog.eosdt.com",
                    "equilibrium.io",
                    "knowledgebase.equilibrium.io",
                    "dsp.equilibrium.io",
                    "eos2dt.com",
                    "genshiro.equilibrium.io",
                ],
                accept_incoming: true,
            },
        });
    }, []);
    return trackConfig;
};
const amplitudeState = {
    instanceNames: new Set(),
};
export const addAmplitudeInstanceName = (instanceName) => {
    amplitudeState.instanceNames.add(instanceName);
};
export const getAmplitudeInstance = (instanceName) => {
    const client = globalThis.amplitude?.getInstance?.(instanceName);
    if (!client) {
        console.error(new Error("amplitude not found"));
        return;
    }
    addAmplitudeInstanceName(instanceName);
    return client;
};
const logEventByDefaultInstance = (...args) => {
    const client = getAmplitudeInstance();
    if (!client) {
        return undefined;
    }
    return client.logEvent(...args);
};
const logEventByAllInstances = (...args) => Array.from(amplitudeState.instanceNames).map((instanceName) => {
    const client = getAmplitudeInstance(instanceName);
    if (!client) {
        return undefined;
    }
    return client.logEvent(...args);
});
const logEvent = (...args) => {
    try {
        if (!amplitudeState.instanceNames.size) {
            return logEventByDefaultInstance(...args);
        }
        else {
            return logEventByAllInstances(...args);
        }
    }
    catch (e) {
        console.warn("amplitude not initialized, more:", e);
    }
};
const setUserPropertiesByDefaultInstance = (...args) => {
    const client = getAmplitudeInstance();
    if (!client) {
        return undefined;
    }
    return client.setUserProperties(...args);
};
const setUserPropertiesByAllInstances = (...args) => Array.from(amplitudeState.instanceNames).map((instanceName) => {
    const client = getAmplitudeInstance(instanceName);
    if (!client) {
        return undefined;
    }
    return client.setUserProperties(...args);
});
const setUserProperties = (...args) => {
    try {
        if (!amplitudeState.instanceNames.size) {
            return setUserPropertiesByDefaultInstance(...args);
        }
        else {
            return setUserPropertiesByAllInstances(...args);
        }
    }
    catch (e) {
        console.warn("amplitude not initialized, more:", e);
    }
};
export const useAmplitude = () => {
    return {
        logEvent,
        setUserProperties,
    };
};
//# sourceMappingURL=tracking.js.map