import { readFile } from "node:fs/promises";
import { join } from "node:path";
import * as jose from "jose";
import { setUserContext } from "../analytics/lib.js";
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("ZUPLO_DO_NOT_TRACK is set; analytics and error tracking are disabled.");
    }
    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();
        }
        setUserContext({
            distinctId: typeof userId === "string" ? userId : machineId(),
            account: typeof argv.account === "string" ? argv.account : undefined,
            project: typeof argv.project === "string" ? argv.project : undefined,
        });
        argv["zuplo-user-id"] = userId;
    }
}
//# sourceMappingURL=user-identification.js.map