import { b as u, e as d, j as f, T as y, J as p } from "./index-c65ca4a4.mjs";
const m = (l, i) => {
  let t = l.trim();
  if (!t)
    return i ? { save: "/", display: i } : { save: "", display: "" };
  if (p.isEmail(t))
    return { save: `mailto:${t}`, display: `mailto:${t}` };
  const s = t.match(/^#/);
  if (s)
    return { save: t, display: t };
  if (i || !t.startsWith("http://") && !t.startsWith("https://") && (t = `https://${t}`), !t.match(/^[a-zA-Z0-9-]+:/) && !t.match(/^(\/|\?)/))
    return { save: t, display: t };
  let e;
  try {
    e = new URL(t, i);
  } catch {
    return { save: t, display: t };
  }
  if (!i)
    return { save: e.toString(), display: e.toString() };
  const a = new URL(i);
  let r = e.pathname && e.pathname.indexOf(a.pathname) === 0;
  `${e.pathname}/` === a.pathname && (r = !0);
  const h = e.host === a.host;
  return !s && h && r && (t = t.replace(/^[a-zA-Z0-9-]+:/, ""), t = t.replace(/^\/\//, ""), t = t.replace(a.host, ""), t = t.replace(a.pathname, ""), e.pathname.slice(-1) !== "/" && (t = t.replace(a.pathname.slice(0, -1), "")), t.match(/^\//) || (t = `/${t}`), !t.match(/\/$/) && !t.match(/[.#?]/) && (t = `${t}/`)), t.match(/^(\/\/|#)/) ? { save: t, display: t } : { save: t, display: new URL(t, i).toString() };
}, g = ({ baseUrl: l, value: i, onChange: t, ...s }) => {
  const [e, a] = u("");
  d(() => {
    a(m(i || "", l).display);
  }, [i, l]);
  const r = () => {
    const { save: n, display: c } = m(e, l);
    a(c), t(n);
  }, h = (n) => {
    var c;
    e === l && setTimeout(() => n.target.setSelectionRange(n.target.value.length, n.target.value.length)), (c = s.onFocus) == null || c.call(s, n);
  }, o = (n) => {
    var c;
    e === l && ["Backspace", "Delete"].includes(n.key) && a(""), (c = s.onKeyDown) == null || c.call(s, n);
  };
  return /* @__PURE__ */ f.jsx(
    y,
    {
      ...s,
      value: e,
      onBlur: r,
      onChange: (n) => a(n.target.value),
      onFocus: h,
      onKeyDown: o
    }
  );
};
export {
  g as U
};
//# sourceMappingURL=URLTextField-a472a6c7.mjs.map
