import { readFile } from "node:fs/promises";
import { join } from "node:path";
import * as Sentry from "@sentry/node";
import * as jose from "jose";
import { ZUPLO_AUTH_FILE_NAME } from "../constants.js";
import { logger } from "../logger.js";
import { machineId } from "../machine-id/lib.js";
import { ZUPLO_XDG_STATE_HOME } from "../xdg/lib.js";
export async function identify(argv) {
    if (process.env.ZUPLO_DO_NOT_TRACK) {
        logger.trace("Sentry traces will be sent without user identification.");
    }
    else {
        let userId;
        try {
            const rawAuth = await readFile(join(ZUPLO_XDG_STATE_HOME, ZUPLO_AUTH_FILE_NAME), "utf-8");
            const authJson = JSON.parse(rawAuth);
            const decoded = jose.decodeJwt(authJson.access_token);
            userId = decoded.sub;
        }
        catch {
            userId = machineId();
        }
        Sentry.setUser({ id: machineId() });
        Sentry.setTag("user-id", userId);
        if (argv.account && typeof argv.account === "string") {
            Sentry.setTag("account", argv.account);
        }
        if (argv.project && typeof argv.project === "string") {
            Sentry.setTag("project", argv.project);
        }
        argv["zuplo-user-id"] = userId;
    }
}
//# sourceMappingURL=user-identification.js.map