"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const types_1 = require("../types");
const promises_1 = require("node:fs/promises");
const Connection_1 = __importDefault(require("../Connection"));
const URI_1 = require("../utils/URI");
const node_path_1 = require("node:path");
const path_1 = require("../utils/path");
const App_1 = __importDefault(require("../App"));
async function default_1(name, parent) {
    let { id, path } = (0, URI_1.parseURI)(parent);
    try {
        if (id == types_1.LocalFileSystemID) {
            await (0, promises_1.mkdir)((0, node_path_1.join)(path, name), { recursive: true });
        }
        else {
            const parts = (0, path_1.split)(name);
            const conn = Connection_1.default.get(id);
            for (let i = 0; i < parts.length; i++) {
                path = (0, node_path_1.join)(path, parts[i]);
                try {
                    await conn.mkdir(path);
                }
                catch (e) {
                    if (i == parts.length - 1) {
                        throw e;
                    }
                }
            }
        }
    }
    catch (error) {
        App_1.default.onError({ type: types_1.FailureType.RemoteError, id, message: 'message' in error ? error.message : String(error) });
    }
    if (!(0, URI_1.isLocal)(parent)) {
        App_1.default.remoteWatcher.refresh(parent);
    }
}
