import { useState, useEffect, useCallback } from "react";
import { richTextDTOtoDAO, richTextDTOtoSelection } from "@makeswift/controls";
import { useIsInBuilder } from "../../../hooks/use-is-in-builder";
import deepEqual from "../../../../../utils/deepEqual";
const COMMIT_DEBOUNCE_DELAY = 500;
function useSyncWithBuilder(editor, text) {
  const [shouldCommit, setShouldCommit] = useState(true);
  const isInBuilder = useIsInBuilder();
  useEffect(() => {
    if (shouldCommit && text && isInBuilder) {
      const nextValue = richTextDTOtoDAO(text);
      const nextSelection = richTextDTOtoSelection(text);
      if (!deepEqual(editor.children, nextValue) || !deepEqual(editor.selection, nextSelection)) {
        editor.children = nextValue;
        editor.selection = nextSelection;
        editor.onChange();
      }
    }
  }, [editor, shouldCommit, text]);
  useEffect(() => {
    if (shouldCommit)
      return;
    const timeoutId = window.setTimeout(() => {
      setShouldCommit(true);
    }, COMMIT_DEBOUNCE_DELAY);
    return () => {
      window.clearTimeout(timeoutId);
    };
  }, [shouldCommit]);
  return useCallback(() => setShouldCommit(false), []);
}
export {
  useSyncWithBuilder
};
//# sourceMappingURL=useSyncWithBuilder.js.map