import lodash from "lodash";
import { useContext, useEffect } from "react";
import { RicosContext, SchemaContext } from "./../../../ricos-context/index.js";
import { getSelectedNodes } from "./../../../_shared/get-selected-nodes.js";
import { isSSR } from "./../../../wix-rich-content-common/index.js";
const useEditorSideEffects = ({
  editor,
  onUpdate,
  onSelectionUpdate
}) => {
  const schema = useContext(SchemaContext);
  const { content } = useContext(RicosContext);
  useEffect(() => {
    if (onUpdate) {
      editor.on(
        "update",
        lodash.debounce(
          () => {
            onUpdate({
              content: schema.getContent(),
              pmContent: schema.getTiptapContent()
            });
          },
          60,
          { leading: true }
        )
      );
    }
    editor.on(
      "selectionUpdate",
      lodash.debounce(
        () => {
          const selectedNodes = getSelectedNodes(editor);
          content.update(selectedNodes, editor);
          if (onSelectionUpdate) {
            onSelectionUpdate({
              selectedNodes,
              content: schema.getContent(),
              pmContent: schema.getTiptapContent()
            });
          }
        },
        60,
        { leading: true }
      )
    );
    if (!isSSR()) {
      window.tiptapEditor = editor;
    }
  }, []);
};
export {
  useEditorSideEffects
};
//# sourceMappingURL=use-editor-side-effects.js.map
