"use strict";
/**
 * Persistent install UUID for cross-session analytics correlation.
 *
 * Stored at ~/.amplitude-wizard/install.json. Used as Amplitude `device_id`
 * so pre-auth runs from the same install are stitched to the authenticated
 * identity once `identifyUser()` fires. Without this, every CLI invocation
 * is a fresh anonymous user until auth completes.
 *
 * IO failures are non-fatal: the caller falls back to a per-process UUID.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.INSTALL_FILE = exports.INSTALL_DIR = void 0;
exports.getOrCreateInstallId = getOrCreateInstallId;
const fs = __importStar(require("node:fs"));
const os = __importStar(require("node:os"));
const path = __importStar(require("node:path"));
const uuid_1 = require("uuid");
const zod_1 = require("zod");
const atomic_write_js_1 = require("./atomic-write.js");
const debug_js_1 = require("./debug.js");
exports.INSTALL_DIR = path.join(os.homedir(), '.amplitude-wizard');
exports.INSTALL_FILE = path.join(exports.INSTALL_DIR, 'install.json');
const InstallRecordSchema = zod_1.z.object({
    installId: zod_1.z.string().uuid(),
    createdAt: zod_1.z.string(),
});
function readInstallRecord(filePath) {
    try {
        const raw = fs.readFileSync(filePath, 'utf-8');
        const parsed = InstallRecordSchema.safeParse(JSON.parse(raw));
        return parsed.success ? parsed.data : undefined;
    }
    catch {
        return undefined;
    }
}
function writeInstallRecord(filePath, record) {
    fs.mkdirSync(path.dirname(filePath), { recursive: true, mode: 0o700 });
    (0, atomic_write_js_1.atomicWriteJSON)(filePath, record, 0o600);
}
/**
 * Load the persisted install UUID, or create and persist a new one.
 * Returns the UUID on success, or `undefined` if both read and write fail
 * (caller should fall back to a per-process UUID).
 */
function getOrCreateInstallId(filePath = exports.INSTALL_FILE) {
    const existing = readInstallRecord(filePath);
    if (existing) {
        return existing.installId;
    }
    const record = {
        installId: (0, uuid_1.v4)(),
        createdAt: new Date().toISOString(),
    };
    try {
        writeInstallRecord(filePath, record);
        return record.installId;
    }
    catch (err) {
        (0, debug_js_1.debug)('install-id: failed to persist', err);
        return undefined;
    }
}
