"use strict";
/**
 * Module resolution for Nexus imports: local paths, `.modules/`, and stdlib.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MODULES_DIRNAME = void 0;
exports.findStdlibDir = findStdlibDir;
exports.resolveModulePath = resolveModulePath;
exports.readModuleSource = readModuleSource;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
exports.MODULES_DIRNAME = ".modules";
function findStdlibDir(fromDir) {
    const candidates = [
        path.join(fromDir, "stdlib"),
        path.join(fromDir, "..", "stdlib"),
    ];
    let dir = fromDir;
    for (let i = 0; i < 8; i++) {
        candidates.push(path.join(dir, "stdlib"));
        // vscode-nexus/stdlib when rooted at extension
        candidates.push(path.join(dir, "vscode-nexus", "stdlib"));
        const parent = path.dirname(dir);
        if (parent === dir) {
            break;
        }
        dir = parent;
    }
    for (const c of candidates) {
        try {
            const abs = path.resolve(c);
            if (fs.statSync(abs).isDirectory()) {
                return abs;
            }
        }
        catch {
            // continue
        }
    }
    return "";
}
function expandModuleCandidates(candidate) {
    const ext = path.extname(candidate);
    if (ext === ".nex") {
        return [candidate];
    }
    return [
        `${candidate}.nex`,
        path.join(candidate, "mod.nex"),
        path.join(candidate, "main.nex"),
    ];
}
/**
 * Resolve an import path against the importer directory, app root,
 * `.modules/`, and stdlib.
 */
function resolveModulePath(importPath, options) {
    const trimmed = importPath.trim().replace(/\\/g, "/");
    if (!trimmed) {
        return null;
    }
    const modulesDir = options.modulesDir ?? path.join(options.rootDir, exports.MODULES_DIRNAME);
    const stdlibDir = options.stdlibDir !== undefined
        ? options.stdlibDir
        : findStdlibDir(options.rootDir);
    const candidates = [];
    if (path.isAbsolute(trimmed)) {
        candidates.push(trimmed);
    }
    else {
        const base = options.fromDir || options.rootDir;
        candidates.push(path.join(base, trimmed));
        candidates.push(path.join(options.rootDir, trimmed));
        candidates.push(path.join(modulesDir, trimmed));
        if (stdlibDir) {
            candidates.push(path.join(stdlibDir, trimmed));
            if (trimmed.startsWith("std/") || trimmed.startsWith("std" + path.sep)) {
                candidates.push(path.join(stdlibDir, trimmed.slice(4)));
            }
        }
    }
    const tried = new Set();
    for (const c of candidates) {
        for (const full of expandModuleCandidates(c)) {
            const cleaned = path.normalize(full);
            if (tried.has(cleaned)) {
                continue;
            }
            tried.add(cleaned);
            try {
                if (fs.statSync(cleaned).isFile()) {
                    return cleaned;
                }
            }
            catch {
                // continue
            }
        }
    }
    return null;
}
function readModuleSource(absolutePath) {
    return fs.readFileSync(absolutePath, "utf8");
}
//# sourceMappingURL=modules.js.map