import { Transforms } from "slate";
import { Slate } from "@makeswift/controls";
import { unwrapInline } from "../BlockPlugin/unwrapInline";
import { wrapInline } from "../BlockPlugin/wrapInline";
import { filterForSubtreeRoots } from "../BlockPlugin/utils/filterForSubtreeRoots";
import { isLinkEntry } from "./types";
import { getLinksAndTextInSelection } from "./getValue";
const onChange = (editor, value) => {
  if (value == null)
    return unwrapInline(editor, Slate.InlineType.Link);
  const roots = filterForSubtreeRoots(getLinksAndTextInSelection(editor));
  const root = roots.at(0);
  if (roots.length === 1 && root != null && isLinkEntry(root)) {
    Transforms.setNodes(
      editor,
      {
        link: value
      },
      { at: root[1] }
    );
  } else {
    unwrapInline(editor, Slate.InlineType.Link);
    wrapInline(editor, { type: Slate.InlineType.Link, link: value, children: [] });
  }
};
export {
  onChange
};
//# sourceMappingURL=onChange.js.map