"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GLOBAL_CONFIG_FILE = exports.GLOBAL_CONFIG_DIR = void 0;
exports.readGlobalConfig = readGlobalConfig;
exports.readGlobalConfigSync = readGlobalConfigSync;
exports.updateGlobalConfig = updateGlobalConfig;
exports.getOrCreateInstallationId = getOrCreateInstallationId;
const fs_1 = require("fs");
const promises_1 = require("fs/promises");
const node_crypto_1 = require("node:crypto");
const os_1 = require("os");
const path_1 = require("path");
const zod_1 = require("zod");
exports.GLOBAL_CONFIG_DIR = process.env.AGENTCORE_CONFIG_DIR ?? (0, path_1.join)((0, os_1.homedir)(), '.agentcore');
exports.GLOBAL_CONFIG_FILE = (0, path_1.join)(exports.GLOBAL_CONFIG_DIR, 'config.json');
const GlobalConfigSchema = zod_1.z
    .object({
    installationId: zod_1.z.string().optional().catch(undefined),
    uvDefaultIndex: zod_1.z.string().optional().catch(undefined),
    uvIndex: zod_1.z.string().optional().catch(undefined),
    disableTransactionSearch: zod_1.z.boolean().optional().catch(undefined),
    transactionSearchIndexPercentage: zod_1.z.number().int().min(0).max(100).optional().catch(undefined),
    telemetry: zod_1.z
        .object({
        enabled: zod_1.z.boolean().optional().catch(undefined),
        endpoint: zod_1.z.string().optional().catch(undefined),
        audit: zod_1.z.boolean().optional().catch(undefined),
    })
        .optional()
        .catch(undefined),
})
    .passthrough();
async function readGlobalConfig(configFile = exports.GLOBAL_CONFIG_FILE) {
    try {
        const data = await (0, promises_1.readFile)(configFile, 'utf-8');
        return GlobalConfigSchema.parse(JSON.parse(data));
    }
    catch {
        return {};
    }
}
function readGlobalConfigSync(configFile = exports.GLOBAL_CONFIG_FILE) {
    try {
        const data = (0, fs_1.readFileSync)(configFile, 'utf-8');
        return GlobalConfigSchema.parse(JSON.parse(data));
    }
    catch {
        return {};
    }
}
async function updateGlobalConfig(partial, configDir = exports.GLOBAL_CONFIG_DIR, configFile = exports.GLOBAL_CONFIG_FILE) {
    try {
        const existing = await readGlobalConfig(configFile);
        const merged = mergeConfig(existing, partial);
        await (0, promises_1.mkdir)(configDir, { recursive: true });
        await (0, promises_1.writeFile)(configFile, JSON.stringify(merged, null, 2), 'utf-8');
        return true;
    }
    catch {
        return false;
    }
}
function mergeConfig(target, source) {
    return {
        ...target,
        ...source,
        ...(source.telemetry !== undefined && {
            telemetry: { ...target.telemetry, ...source.telemetry },
        }),
    };
}
/**
 * Returns the installationId, generating one if it doesn't exist yet.
 * `created: true` means this is the first run (ID was just generated).
 *
 * Note: concurrent first-run invocations may each generate a different ID;
 * the last write wins. This is acceptable — the ID only needs to be stable
 * after the first successful write, and CLI invocations are typically sequential.
 */
async function getOrCreateInstallationId(configDir = exports.GLOBAL_CONFIG_DIR, configFile = exports.GLOBAL_CONFIG_FILE) {
    const config = await readGlobalConfig(configFile);
    if (config.installationId) {
        return { id: config.installationId, created: false };
    }
    const id = (0, node_crypto_1.randomUUID)();
    await updateGlobalConfig({ installationId: id }, configDir, configFile);
    return { id, created: true };
}
//# sourceMappingURL=global-config.js.map