"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRelative = exports.getModulePath = exports.isGlobalPath = exports.getGlobalPath = exports.getGlobalReference = void 0;
const Null_1 = __importDefault(require("../Null"));
const Undefined_1 = __importDefault(require("../Undefined"));
const Type_1 = __importDefault(require("../Type"));
const Property_1 = __importDefault(require("../Property"));
const Integer_1 = __importDefault(require("../Integer"));
const Any_1 = __importDefault(require("../Any"));
// // absolute path related functions
// import np from "path"
const ast_1 = require("./ast");
const common_1 = require("./common");
// const ROOT_CHARACTER = ":"
const PATH_SEPARATOR = "/";
// const EXPORT_SEPARATOR = "#"
// const DEFAULT_EXPORT = "default"
const ROOT = "";
const ionGlobals = new Set([
    Undefined_1.default.name,
    Null_1.default.name,
    Type_1.default.name,
    Property_1.default.name,
    Integer_1.default.name,
    Any_1.default.name,
]);
const globalPrefix = "global:";
function getGlobalReference(node) {
    let { location } = node;
    // check if name is exported by ionscript runtime, 
    if (ionGlobals.has(node.name)) {
        return new ast_1.MemberExpression({
            object: new ast_1.Reference({ location, name: common_1.runtimeModuleName }),
            property: new ast_1.Identifier({ location, name: node.name }),
        });
    }
    return node.patch({ path: getGlobalPath(node.name) });
}
exports.getGlobalReference = getGlobalReference;
function getGlobalPath(name) {
    //  check first and see if this name is exported by our runtime module
    //  if so, we import it from there.
    return `${globalPrefix}${name}`;
}
exports.getGlobalPath = getGlobalPath;
function isGlobalPath(path) {
    return path.startsWith(globalPrefix);
}
exports.isGlobalPath = isGlobalPath;
function getModulePath(moduleName, name, location) {
    if (location) {
        return `${moduleName}#${location.start.line}:${location.start.column}:${name}`;
    }
    else {
        return `${moduleName}#${name}`;
    }
}
exports.getModulePath = getModulePath;
// //  path format is
// //  path/to/module#export
// //  url/to/module.js
// //  path/to/module#default => default export
// //  path/to/module => same as * export
function isRelative(path) {
    return path.startsWith(".");
}
function isAbsolute(path) {
    return !isRelative(path);
}
function getParent(path) {
    let steps = split(path);
    if (steps.length <= 1) {
        return ROOT;
    }
    return join(...steps.slice(0, -1));
}
function split(path) {
    return path.split(PATH_SEPARATOR);
}
function join(...steps) {
    return steps.filter(s => s).join(PATH_SEPARATOR);
}
function getRelative(from, to) {
    var _a;
    if (isAbsolute(to)) {
        return to;
    }
    let steps = split(to);
    from = getParent(from);
    while (from && ((_a = steps[0]) === null || _a === void 0 ? void 0 : _a.startsWith("."))) {
        if (steps[0] === "..") { // if it's just "." then we already got parent above
            from = getParent(from);
        }
        steps.shift();
    }
    return join(from, ...steps);
}
exports.getRelative = getRelative;
