import { NotesT } from "@/types/notes";

export type CommentT = {
  show: boolean;
  data: NotesT;
};

type CommentIdIncludesT = {
  commentIndex: number;
  dataId: number;
};

export const noteCommentStruct: Array<CommentT> = [];
const commentIdIncludes: Array<CommentIdIncludesT> = [];

export function useCommentHandler() {
  function insertNode(data: NotesT, comment: Array<CommentT>) {
    // let checkIfPresent = commentIdIncludes.find((dt) => dt.dataId === data.id);

    function nestStruct(replies: NotesT[]): CommentT[] {
      let repliesArr: CommentT[] = [];
      for (let i of replies) {
        if (i.replies.length) {
          repliesArr.push({
            show: false,
            data: {
              ...i,
              replies: [...(nestStruct(i.replies) as any)],
            },
          });
        } else {
          repliesArr.push({
            show: false,
            data: i,
          });
        }
      }
      return repliesArr;
    }

    // if (!checkIfPresent) {
    let noteCont: CommentT[] = [];
    if (data.replies.length) {
      let a = nestStruct(data.replies);
      noteCont = [...a];
      comment.push({
        show: false,
        data: {
          ...data,
          replies: [...noteCont] as any,
        },
      });
    } else {
      comment.push({
        show: false,
        data: {
          ...data,
        },
      });
    }
    // commentIdIncludes.push({
    //   commentIndex: commentIdIncludes.length,
    //   dataId: data.id,
    // });
    // } else {
    //   let { dataId, commentIndex } = checkIfPresent;

    //   for (let i = 0; i < comment[commentIndex].data.replies.length; i++) {
    // insertNode(
    //   comment[commentIndex].data.replies[i],
    //   comment[commentIndex].data.replies
    // );
    //   }
    // }
  }
  // const outputCommentData = [];
  // function addingNodes(data: NotesT) {
  //   const com = [];
  //   if (data.replies.length) {
  //     let replies;
  //     for (let i of data.replies) {
  //       replies =  addingNodes(i);
  //     }
  //     com.push({
  //       show:false,
  //       data:{
  //         ...data,
  //         replies:[...replies]
  //       }
  //     })
  //   }else{
  //     com.push({
  //       show:false,
  //       data:data,
  //     })
  //   }

  //   return com;
  // }
  return { insertNode };
}
