"use strict";
/*
 * @adonisjs/drive
 *
 * (c) AdonisJS
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.FakeDrive = void 0;
const Exceptions_1 = require("../Exceptions");
/**
 * An implementation of the fake drive
 */
class FakeDrive {
    constructor() {
        /**
         * Reference to registered fakes
         */
        this.fakes = new Map();
    }
    /**
     * Find a file for the given path exists. Searched
     * across all the faked disk
     */
    async exists(location) {
        for (let [, fake] of this.fakes) {
            const exists = await fake.exists(location);
            if (exists) {
                return true;
            }
        }
        return false;
    }
    /**
     * Get the contents of the file as buffer
     */
    async get(location) {
        for (let [, fake] of this.fakes) {
            const exists = await fake.exists(location);
            if (exists) {
                return fake.get(location);
            }
        }
        throw Exceptions_1.CannotReadFileException.invoke(location, new Error('File not found'));
    }
    /**
     * Get the contents of the file as buffer
     */
    async bytes(location) {
        const contents = await this.get(location);
        return contents.length;
    }
    /**
     * Access the faked driver
     */
    use(disk) {
        return this.fakes.get(disk);
    }
    /**
     * Find if a disk has been faked
     */
    isFaked(disk) {
        return this.fakes.has(disk);
    }
    /**
     * Restore a fake
     */
    restore(disk) {
        this.fakes.delete(disk);
    }
}
exports.FakeDrive = FakeDrive;
