import { useCallback as i } from "react";
import f from "../utils/normaliseUrl.js";
function h(t, r) {
  const a = i(
    (c) => {
      if (!t) return;
      const { url: s, displayText: e } = c;
      if (!s.trim())
        return;
      const n = f(s);
      if (r)
        e.trim() !== "" && e.trim() !== r.text ? t.chain().focus().extendMarkRange("link").deleteSelection().insertContent({
          type: "text",
          text: e.trim(),
          marks: [{ type: "link", attrs: { href: n } }]
        }).run() : t.chain().focus().extendMarkRange("link").setLink({ href: n }).run();
      else if (t.state.selection.empty) {
        const u = e.trim() || n;
        t.chain().focus().insertContent({
          type: "text",
          text: u,
          marks: [{ type: "link", attrs: { href: n } }]
        }).run();
      } else
        t.chain().focus().setLink({ href: n }).run();
    },
    [t, r]
  ), l = i(() => {
    t && t.chain().focus().extendMarkRange("link").unsetLink().run();
  }, [t]);
  return { handleLink: a, handleUnlink: l };
}
export {
  h as default
};
//# sourceMappingURL=useHtmlEditorLinkAction.js.map
