'use client';

import '../style.scss';
import React from 'react';
import Loading from '@/app/component/Loading';
import { BoardResponse } from '@/types/response/forum/board';
import { PostResponse } from '@/types/response/forum/post';
import { CommentListResponse } from '@/types/response/forum/comment';
import { type CommentItem } from '@/types/forum/comment';
import WriteForm from './WriteForm';
import Item from './Item';

type Props = {
    _board: BoardResponse,
    _post: PostResponse,
    _data: CommentListResponse;
    loading: boolean;
    replyTargetID: number|null;
    onReply: (commentID: number) => void;
    onDelete: (commentID: number, parentID?: number) => void;
    onSuccess: (comment?: CommentItem) => void;
    canWriteReply?: boolean;
}

export default function List({ _board, _post, _data, loading, replyTargetID, onReply, onSuccess, onDelete, canWriteReply } : Props)
{
    return (
        <>
            {loading ? (
                <Loading type={2} />
            ) : (
                <>
                    {_data.total > 0 && (
                    <article className="comment-list">
                        <ol>
                            {_data.list.map((row) => {
                                return (
                                    <React.Fragment key={row.id}>
                                        <Item
                                            _board={_board}
                                            _post={_post}
                                            _comment={row}
                                            isReplying={replyTargetID === row.id}
                                            onReply={() => onReply(row.id)}
                                            onDelete={onDelete}
                                            onSuccess={onSuccess}
                                            canWriteReply={canWriteReply}
                                        >
                                            {/* row에 대한 답글 폼(토글) */}
                                            {canWriteReply !== false && replyTargetID === row.id && (
                                                <WriteForm
                                                    _board={_board}
                                                    _post={_post}
                                                    _comment={row}
                                                    onSuccess={onSuccess}
                                                    onCancel={() => onReply(row.id)}
                                                />
                                            )}
                                        </Item>

                                        {/* 대댓글 목록 */}
                                        {Array.isArray(row.children) && row.children.length > 0 && (
                                            <li>
                                                <ol className="pl-[72px]">
                                                    {row.children.map((ch) => (
                                                        <Item
                                                            key={ch.id}
                                                            _board={_board}
                                                            _post={_post}
                                                            _comment={ch}
                                                            onReply={() => onReply(ch.id)}
                                                            onDelete={onDelete}
                                                            onSuccess={onSuccess}
                                                            canWriteReply={canWriteReply}
                                                        >
                                                            {canWriteReply !== false && replyTargetID != null && replyTargetID === ch.id && (
                                                                <WriteForm
                                                                    _board={_board}
                                                                    _post={_post}
                                                                    _comment={ch}
                                                                    onSuccess={onSuccess}
                                                                    onCancel={() => onReply(ch.id)}
                                                                />
                                                            )}
                                                        </Item>
                                                    ))}
                                                </ol>
                                            </li>
                                        )}
                                    </React.Fragment>
                                )
                            })}
                        </ol>
                    </article>
                    )}
                </>
            )}
        </>
    );
}