import { useEffect } from "react";
import { useGetEditor } from "../Editor";

export const useEditor = () => {
  const { editor } = useGetEditor();

  const getContents = () => ({ contents: editor.current.getContents() });
  const setContents = (data) => editor.current.setContents(data);
  const insertHtml = (data) => editor.current.insertHTML(data);

  const save = (callback) => {
    const contents = getContents();
    editor.current.save();
    if (callback && typeof callback === "function") callback({ contents });
  };

  useEffect(() => {
    if (!editor.current) return;

    // add Events
    // editor.current.onImageUploadBefore = (data) => {
    //   console.log({ before: data });
    // };
  }, [editor]);

  return { editor, getContents, setContents, save, insertHtml };
};
