// no-auto-license-header
export const up = 3;
export function caller(levelUp = up) {
    var _a;
    const err = new Error();
    const stack = (_a = err.stack) === null || _a === void 0 ? void 0 : _a.split("\n")[levelUp];
    if (stack) {
        return getFile.bind(this)(stack);
    }
}
export function getFile(stack) {
    stack = stack.substr(stack.indexOf("at ") + 3);
    if (!stack.startsWith("file://")) {
        stack = stack.substr(stack.lastIndexOf("(") + 1);
    }
    const path = stack.split(":");
    let file = `${path[0]}:${path[1]}`;
    if (this === null || this === void 0 ? void 0 : this.cb) {
        const cb = this.cb;
        file = cb(file);
    }
    return file;
}
export function fromFileUrlWin32(url) {
    const urlObj = new URL(url);
    let path = decodeURIComponent(urlObj.pathname.replace(/\//g, "\\").replace(/%(?![0-9A-Fa-f]{2})/g, "%25")).replace(/^\\*([A-Za-z]:)(\\|$)/, "$1\\");
    if (urlObj.hostname !== "") {
        // Note: The `URL` implementation guarantees that the drive letter and
        // hostname are mutually exclusive. Otherwise it would not have been valid
        // to append the hostname and path like this.
        path = `\\\\${urlObj.hostname}${path}`;
    }
    return path;
}
export function fromFileUrlPosix(url) {
    const urlObject = new URL(url);
    return decodeURIComponent(urlObject.pathname.replace(/%(?![0-9A-Fa-f]{2})/g, "%25"));
}
