import { promises as fs } from 'fs';
import path from 'path';
export class NodeFileSystemAdapter {
    async mkdir(dirPath, options) {
        await fs.mkdir(dirPath, options);
    }
    async writeFile(filePath, data) {
        await fs.writeFile(filePath, data);
    }
    async readFile(filePath, encoding) {
        return await fs.readFile(filePath, encoding);
    }
    async readdir(dirPath) {
        return await fs.readdir(dirPath);
    }
    async unlink(filePath) {
        await fs.unlink(filePath);
    }
    async access(filePath) {
        await fs.access(filePath);
    }
}
export class InMemoryFileSystemAdapter {
    files = new Map();
    directories = new Set();
    async mkdir(dirPath, options) {
        if (options?.recursive) {
            // Handle both absolute and relative paths
            const normalizedPath = path.normalize(dirPath);
            const parts = normalizedPath.split(path.sep).filter(p => p);
            let currentPath = normalizedPath.startsWith(path.sep) ? path.sep : '';
            for (const part of parts) {
                currentPath = path.join(currentPath, part);
                this.directories.add(currentPath);
            }
        }
        else {
            this.directories.add(dirPath);
        }
    }
    async writeFile(filePath, data) {
        const dir = path.dirname(filePath);
        // Skip directory check for root or current directory
        if (dir !== '.' && dir !== '/' && dir !== '') {
            if (!this.directories.has(dir)) {
                throw new Error(`ENOENT: no such file or directory, open '${filePath}'`);
            }
        }
        this.files.set(filePath, data);
    }
    async readFile(filePath, encoding) {
        const content = this.files.get(filePath);
        if (content === undefined) {
            throw new Error(`ENOENT: no such file or directory, open '${filePath}'`);
        }
        return content;
    }
    async readdir(dirPath) {
        if (!this.directories.has(dirPath)) {
            throw new Error(`ENOENT: no such file or directory, scandir '${dirPath}'`);
        }
        const files = [];
        for (const [filePath] of this.files) {
            if (path.dirname(filePath) === dirPath) {
                files.push(path.basename(filePath));
            }
        }
        return files;
    }
    async unlink(filePath) {
        if (!this.files.has(filePath)) {
            throw new Error(`ENOENT: no such file or directory, unlink '${filePath}'`);
        }
        this.files.delete(filePath);
    }
    async access(filePath) {
        if (!this.files.has(filePath)) {
            throw new Error(`ENOENT: no such file or directory, access '${filePath}'`);
        }
    }
    // Test helper methods
    clear() {
        this.files.clear();
        this.directories.clear();
    }
    getFileCount() {
        return this.files.size;
    }
    hasFile(filePath) {
        return this.files.has(filePath);
    }
    hasDirectory(dirPath) {
        return this.directories.has(dirPath);
    }
}
//# sourceMappingURL=file-system-adapter.js.map