import { Node, mergeAttributes, ReactNodeViewRenderer } from "@tiptap/react";
import type { NodeViewProps } from "@tiptap/react";
import { NoteLinkBlockView } from "./note-link-block-view";

export interface NoteLinkOptions {
  parentNoteId: string | null;
  parentTeamId: string | null;
  parentFolderId: string | null;
  parentProjectId: string | null;
}

export const NoteLinkBlock = Node.create<NoteLinkOptions>({
  name: "noteLink",
  group: "block",
  atom: true,
  draggable: true,

  addOptions() {
    return {
      parentNoteId: null,
      parentTeamId: null,
      parentFolderId: null,
      parentProjectId: null,
    };
  },

  addAttributes() {
    return {
      noteId: { default: null },
      title: { default: "Untitled" },
    };
  },

  parseHTML() {
    return [{ tag: 'div[data-type="note-link"]' }];
  },

  renderHTML({ HTMLAttributes }) {
    return ["div", mergeAttributes(HTMLAttributes, { "data-type": "note-link" })];
  },

  addNodeView() {
    return ReactNodeViewRenderer((props: NodeViewProps) => (
      <NoteLinkBlockView {...props} options={this.options} />
    ));
  },
});
