import { PostHog } from "posthog-node";
import { POST_HOG_CAPTURE_KEY, ZUPLO_USER_ID_ARGV_KEY } from "../constants.js";
import { machineId } from "../machine-id/lib.js";
import { getPackageManagerUsageProperties } from "./package-manager.js";
let _postHog;
function init() {
    if (process.env.ZUPLO_DO_NOT_TRACK) {
        return undefined;
    }
    _postHog = new PostHog(POST_HOG_CAPTURE_KEY, {
        host: "https://app.posthog.com",
        flushAt: 1,
        flushInterval: 1,
    });
    return _postHog;
}
export async function captureEvent({ argv, event, properties, groups, sendFeatureFlags, timestamp, disableGeoip, }) {
    if (_postHog === undefined) {
        init();
    }
    if (_postHog) {
        properties = {
            ...getPackageManagerUsageProperties(),
            ...(properties ?? {}),
        };
        properties.$set_once = properties.$set_once ?? {};
        if (argv[ZUPLO_USER_ID_ARGV_KEY] &&
            typeof argv[ZUPLO_USER_ID_ARGV_KEY] === "string") {
            properties.$set_once["user-id"] = argv[ZUPLO_USER_ID_ARGV_KEY];
        }
        if (argv.account && typeof argv.account === "string") {
            properties.$set_once["account"] = argv["account"];
        }
        if (argv.project && typeof argv.project === "string") {
            properties.$set_once["project"] = argv["project"];
        }
        if (process.env.CI) {
            properties["ci"] = process.env.CI;
        }
        _postHog?.capture({
            distinctId: argv[ZUPLO_USER_ID_ARGV_KEY] || machineId(),
            event,
            properties,
            groups,
            sendFeatureFlags,
            timestamp,
            disableGeoip,
        });
    }
}
export async function shutdownAnalytics() {
    return _postHog?.shutdown();
}
//# sourceMappingURL=lib.js.map