/* lib/terminal/commands/library/mkdir - A utility for recursively creating directories in the file system. */ import error from "../../utilities/error.js";
import node from "../../utilities/node.js";
import vars from "../../utilities/vars.js";
// makes specified directory structures in the local file system
const mkdir = function terminal_commands_library_mkdir(dir, callback) {
    let ind = 0;
    const dirs = dir.split(vars.path.sep), title = "Make Directory", len = dirs.length, errorHandler = function terminal_commands_library_mkdir_errorHandler(errorInstance, statInstance, errorCallback) {
        if (errorInstance !== null) {
            if (errorInstance.code === "ENOENT") {
                errorCallback();
                return;
            }
            if (vars.settings.verbose === true) {
                error([
                    "Error making a directory."
                ], errorInstance);
            }
            return;
        }
        if (statInstance.isDirectory() === true) {
            if (ind < len) {
                recursiveStat();
            } else {
                callback(title, [
                    `Directory already exists: ${dir}`
                ], false);
            }
            return;
        }
        const type = statInstance.isFile() === true ? "file" : statInstance.isSymbolicLink() === true ? "symbolic link" : statInstance.isCharacterDevice() === true ? "character device" : statInstance.isFIFO() === true ? "FIFO" : statInstance.isSocket() === true ? "socket" : "unknown file system object";
        callback(title, [
            JSON.stringify(new Error(`Destination directory, '${vars.text.cyan + dir + vars.text.none}', is a ${type}.`))
        ], true);
        return;
    }, recursiveStat = function terminal_commands_library_mkdir_recursiveStat() {
        ind = ind + 1;
        const target = dirs.slice(0, ind).join(vars.path.sep);
        node.fs.stat(target, function terminal_commands_library_mkdir_recursiveStat_callback(errA, statA) {
            errorHandler(errA, statA, function terminal_commands_library_mkdir_recursiveStat_callback_errorHandler() {
                node.fs.mkdir(target, function terminal_commands_library_mkdir_recursiveStat_callback_errorHandler_makeDir(errB) {
                    if (errB !== null && vars.settings.verbose === true && errB.toString().indexOf("file already exists") < 0) {
                        callback(title, [
                            JSON.stringify(errB)
                        ], true);
                    } else if (ind < len) {
                        terminal_commands_library_mkdir_recursiveStat();
                    } else {
                        callback(title, [
                            `Directory created: ${dir}`
                        ], false);
                    }
                });
            });
        });
    };
    if (dirs[0] === "") {
        ind = ind + 1;
    }
    node.fs.stat(dir, function terminal_commands_library_mkdir_stat(statError, stats) {
        if (statError === null) {
            if (stats.isDirectory() === true) {
                callback(title, [
                    `Directory already exists: ${dir}`
                ], false);
            } else {
                errorHandler(null, stats, null);
            }
        } else {
            recursiveStat();
        }
    });
};
export default mkdir;
