"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.withPublicPath = void 0;
const url_1 = require("url");
const path_1 = __importDefault(require("path"));
const withPublicPath = (str, base) => {
    // The use of an absolute URL without a protocol is technically legal,
    // however it cannot be parsed as a URL instance.
    // Just return it.
    // e.g. str is //example.com/foo.js
    if (str.startsWith('//')) {
        return str;
    }
    // Only absolute url with hostname & protocol can be parsed into URL instance.
    // e.g. str is https://example.com/foo.js
    try {
        return new url_1.URL(str).toString();
    }
    catch { }
    // Or it should be a relative path.
    // Let's join the publicPath.
    // e.g. str is ./foo.js
    try {
        // `base` is a url with hostname & protocol.
        // e.g. base is https://example.com/static
        const url = new url_1.URL(base);
        url.pathname = path_1.default.posix.resolve(url.pathname, str);
        return url.toString();
    }
    catch {
        // without hostname & protocol.
        // e.g. base is /
        return path_1.default.posix.resolve(base, str);
    }
};
exports.withPublicPath = withPublicPath;
