/**
 * Unified Temporary Directory Manager
 *
 * Manages temporary directories for CLI operations using hash-based naming.
 * This provides a single location for all temp files per operation, making
 * cleanup simple and Docker mounting straightforward.
 */
import { getHashServer } from '@walkeros/server-core';
import path from 'path';
import fs from 'fs-extra';
/**
 * Manages a unified temporary directory structure for CLI operations
 */
export class TempDirManager {
    hash;
    paths;
    /**
     * Create a new TempDirManager with hash-based directory naming
     *
     * @param configPath - Path to the config file (used in hash)
     * @param operation - Operation type ('bundle' | 'simulate')
     * @param timestamp - Optional timestamp for uniqueness (defaults to Date.now())
     */
    static async create(configPath, operation, timestamp) {
        const ts = timestamp || Date.now();
        const hash = await getHashServer(`${configPath}:${operation}:${ts}`, 12);
        return new TempDirManager(hash);
    }
    constructor(hash) {
        this.hash = hash;
        const root = path.join('.tmp', `walkeros-${hash}`);
        this.paths = {
            root,
            nodeModules: path.join(root, 'node_modules'),
            cache: path.join(root, 'cache'),
            bundle: path.join(root, 'bundle.js'),
            entry: path.join(root, 'entry.js'),
        };
    }
    /**
     * Initialize the temporary directory structure
     */
    async initialize() {
        await fs.ensureDir(this.paths.root);
        await fs.ensureDir(this.paths.nodeModules);
        await fs.ensureDir(this.paths.cache);
    }
    /**
     * Clean up the temporary directory
     */
    async cleanup() {
        await fs.remove(this.paths.root);
    }
    /**
     * Get the hash used for this temp directory
     */
    getHash() {
        return this.hash;
    }
    /**
     * Get a path within the temp directory
     */
    getPath(...parts) {
        return path.join(this.paths.root, ...parts);
    }
    /**
     * Check if the temp directory exists
     */
    async exists() {
        return fs.pathExists(this.paths.root);
    }
}
//# sourceMappingURL=temp-manager.js.map