import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import RandomString from './randomString.js';
export default class TempDirManager {
    static instance;
    tempDirRoot;
    constructor() {
        this.tempDirRoot = path.join(os.tmpdir(), `cloudscripts-${process.pid}-${RandomString.get()}`);
    }
    static dispose() {
        this.getInstance().dispose();
    }
    static get(subpath = "") {
        return this.getInstance().get(subpath);
    }
    static getInstance() {
        if (!TempDirManager.instance) {
            TempDirManager.instance = new TempDirManager();
        }
        return TempDirManager.instance;
    }
    dispose() {
        if (fs.existsSync(this.tempDirRoot)) {
            fs.rmSync(this.tempDirRoot, { force: true, recursive: true });
        }
    }
    get(subpath = "") {
        const tempPath = path.join(this.tempDirRoot, subpath);
        if (!fs.existsSync(tempPath)) {
            fs.mkdirSync(tempPath, { recursive: true });
        }
        return tempPath;
    }
}
