"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.resolveStringUrl = exports.resolveUrl = exports.isStringOrURL = void 0;
var _path = _interopRequireDefault(require("../../../shared/lib/isomorphic/path"));
var _warnOnce = require("../../../shared/lib/utils/warn-once");
function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function isStringOrURL(icon) {
    return typeof icon === "string" || icon instanceof URL;
}
function resolveUrl(url, metadataBase) {
    if (url instanceof URL) return url;
    if (!url) return null;
    try {
        // If we can construct a URL instance from url, ignore metadataBase
        const parsedUrl = new URL(url);
        return parsedUrl;
    } catch (_) {}
    if (!metadataBase) {
        if (process.env.NODE_ENV !== "production") {
            metadataBase = new URL(`http://localhost:${process.env.PORT || 3000}`);
            // Development mode warning
            (0, _warnOnce).warnOnce(`metadata.metadataBase is not set and fallbacks to "${metadataBase.origin}", please specify it in root layout to resolve absolute urls.`);
        } else {
            throw new Error(`metadata.metadataBase needs to be provided for resolving absolute URL: ${url}`);
        }
    }
    // Handle relative or absolute paths
    const basePath = metadataBase.pathname || "/";
    const joinedPath = _path.default.join(basePath, url);
    return new URL(joinedPath, metadataBase);
}
// Return a string url without trailing slash
const resolveStringUrl = (url)=>{
    const href = typeof url === "string" ? url : url.toString();
    return href.endsWith("/") ? href.slice(0, -1) : href;
};
exports.isStringOrURL = isStringOrURL;
exports.resolveUrl = resolveUrl;
exports.resolveStringUrl = resolveStringUrl;

//# sourceMappingURL=resolve-url.js.map