import { useContext, useMemo } from "react";
import { EditorContext, useDevPmNodeAttrs } from "./../ricos-context/index.js";
import { createThrottle } from "./../wix-rich-content-common/index.js";
import { findNodeById } from "./../tiptap-editor/helpers/index.js";
const useNodeAttrs = (nodeId, nodeType) => {
  const { adapter } = useContext(EditorContext);
  const devPmNodeAttrs = useDevPmNodeAttrs();
  const patchPmNodeAttrs = useMemo(
    () => (
      // @ts-ignore $TSFixMe
      createThrottle((attrs, options) => {
        adapter.tiptapEditor.commands.updateNodeAttrsById(nodeId, attrs, options);
      })
    ),
    []
  );
  if (devPmNodeAttrs) {
    return {
      pmNodeAttrs: devPmNodeAttrs,
      patchPmNodeAttrs: () => {
      }
    };
  }
  const tr = adapter.tiptapEditor.view.state.tr;
  const nodeWithPos = findNodeById(tr, nodeId);
  const { node } = nodeWithPos?.[0] ?? {};
  if (!node || node.type.name !== nodeType) {
    return { pmNodeAttrs: void 0, patchPmNodeAttrs: void 0 };
  }
  return {
    pmNodeAttrs: node.attrs,
    patchPmNodeAttrs
  };
};
export {
  useNodeAttrs
};
//# sourceMappingURL=use-node-attrs.js.map
