"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VERSION_MANAGER_LAYOUT_DIRECTORIES = exports.DAY_SHIFT_VERSION_MANAGER_ROOT_ENV = void 0;
exports.resolveVersionManagerRoot = resolveVersionManagerRoot;
exports.initializeVersionManagerLayout = initializeVersionManagerLayout;
const node_fs_1 = __importDefault(require("node:fs"));
const node_os_1 = __importDefault(require("node:os"));
const node_path_1 = __importDefault(require("node:path"));
exports.DAY_SHIFT_VERSION_MANAGER_ROOT_ENV = "DAY_SHIFT_VERSION_MANAGER_ROOT";
exports.VERSION_MANAGER_LAYOUT_DIRECTORIES = [
    { id: "root", relativePath: "" },
    { id: "shims", relativePath: "shims" },
    { id: "versions", relativePath: "versions" },
    { id: "aliases", relativePath: "aliases" },
    { id: "installs", relativePath: "installs" },
    { id: "install-receipts", relativePath: node_path_1.default.join("installs", "receipts") },
    { id: "cache", relativePath: "cache" },
    { id: "cache-manifests", relativePath: node_path_1.default.join("cache", "manifests") },
    { id: "logs", relativePath: "logs" }
];
function resolveVersionManagerRoot(options = {}) {
    const env = options.env ?? process.env;
    const explicitRoot = env[exports.DAY_SHIFT_VERSION_MANAGER_ROOT_ENV]?.trim();
    if (explicitRoot !== undefined && explicitRoot !== "") {
        return node_path_1.default.resolve(explicitRoot);
    }
    const platform = options.platform ?? process.platform;
    const homeDirectory = options.homeDirectory ?? node_os_1.default.homedir();
    if (platform === "win32") {
        const appData = env.APPDATA?.trim() || node_path_1.default.win32.join(homeDirectory, "AppData", "Roaming");
        return node_path_1.default.win32.join(appData, "DayShift", "version-manager");
    }
    if (platform === "darwin") {
        return node_path_1.default.posix.join(homeDirectory, "Library", "Application Support", "day-shift", "version-manager");
    }
    const configHome = env.XDG_CONFIG_HOME?.trim() || node_path_1.default.posix.join(homeDirectory, ".config");
    return node_path_1.default.posix.join(configHome, "day-shift", "version-manager");
}
function initializeVersionManagerLayout(options = {}) {
    const versionManagerRoot = resolveVersionManagerRoot(options);
    const directories = exports.VERSION_MANAGER_LAYOUT_DIRECTORIES.map((directory) => {
        const directoryPath = directory.relativePath === ""
            ? versionManagerRoot
            : node_path_1.default.join(versionManagerRoot, directory.relativePath);
        const existed = node_fs_1.default.existsSync(directoryPath);
        node_fs_1.default.mkdirSync(directoryPath, { recursive: true });
        return {
            id: directory.id,
            path: directoryPath,
            existed
        };
    });
    return {
        versionManagerRoot,
        directories
    };
}
