class i extends Error {
}
function h(t, {
  throwOnError: r = !0,
  returnRaw: a = !1,
  // 根据 URL 的规范，protocol 以 : 结尾
  allowProtocols: l = ["https:", "http:"],
  // host 中可能会携带端口号，需要使用 hostname 来获取干净的域名
  allowDomains: n = void 0
} = {}) {
  n === void 0 && (n = window.blocklet?.domainAliases || [], n.push(window.location.href));
  try {
    let o;
    t.startsWith("/") ? o = window.location.origin : t.startsWith("./") && (o = window.location.href);
    const e = new URL(t, o), s = n && n.map((c) => {
      try {
        return new URL(c).hostname;
      } catch {
        return new URL(`https://${c}`).hostname;
      }
    });
    if (l !== null && !l.includes(e.protocol)) {
      if (console.error(`Invalid protocol: ${e.protocol}`), r) throw new i(`Invalid protocol: ${e.protocol}`);
      return null;
    }
    if (s !== null && !s.includes(e.hostname)) {
      if (console.error(`Invalid host: ${e.hostname}`), r) throw new i(`Invalid host: ${e.hostname}`);
      return null;
    }
    return a ? t : e.href;
  } catch (o) {
    if (o instanceof i) {
      if (r) throw o;
      return null;
    }
    if (console.error(`Failed to convert url: ${t}`), r) throw new Error(`Failed to convert url: ${t}`);
    return null;
  }
}
export {
  h as getSafeUrl
};
