// no-auto-license-header | @generated (pre-commit doesn't support two headers)
// jsr-private-module
/*
The MIT License (MIT)

Copyright (c) 2020 Alexandre Piel
Copyright 2018-2022 the Deno authors.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import "../_dnt.polyfills.js";
export const up = 3;
// deno-lint-ignore no-explicit-any
export function caller(levelUp = up) {
    const err = new Error();
    const stack = err.stack?.split("\n")[levelUp];
    if (stack) {
        return getFile.bind(this)(stack);
    }
}
// deno-lint-ignore no-explicit-any
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?.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"));
}
