/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import * as fs from 'node:fs';
import { randomUUID } from 'node:crypto';
import * as path from 'node:path';
import { Storage } from '../config/storage.js';
export class InstallationManager {
    getInstallationIdPath() {
        return Storage.getInstallationIdPath();
    }
    readInstallationIdFromFile() {
        const installationIdFile = this.getInstallationIdPath();
        if (fs.existsSync(installationIdFile)) {
            const installationid = fs
                .readFileSync(installationIdFile, 'utf-8')
                .trim();
            return installationid || null;
        }
        return null;
    }
    writeInstallationIdToFile(installationId) {
        const installationIdFile = this.getInstallationIdPath();
        const dir = path.dirname(installationIdFile);
        fs.mkdirSync(dir, { recursive: true });
        fs.writeFileSync(installationIdFile, installationId, 'utf-8');
    }
    /**
     * Retrieves the installation ID from a file, creating it if it doesn't exist.
     * This ID is used for unique user installation tracking.
     * @returns A UUID string for the user.
     */
    getInstallationId() {
        try {
            let installationId = this.readInstallationIdFromFile();
            if (!installationId) {
                installationId = randomUUID();
                this.writeInstallationIdToFile(installationId);
            }
            return installationId;
        }
        catch (error) {
            console.error('Error accessing installation ID file, generating ephemeral ID:', error);
            return '123456789';
        }
    }
}
//# sourceMappingURL=installationManager.js.map