import React, {useState} from 'react'; import {ChevronDown, ChevronUp, Plus, Trash2} from 'lucide-react'; import {Incident, PlotPoint} from '@/lib/types/book'; import {ActChapter, ChapterListProps} from '@/lib/types/chapter'; import SelectBox, {SelectBoxProps} from '@/components/form/SelectBox'; import ActChapterItem from './ActChapter'; import InputField from '@/components/form/InputField'; import TextInput from '@/components/form/TextInput'; import IconButton from '@/components/ui/IconButton'; import Collapse from '@/components/ui/Collapse'; import {useTranslations} from '@/lib/i18n'; interface ActPlotPointsProps { plotPoints: PlotPoint[]; incidents: Incident[]; actId: number; mainChapters: ChapterListProps[]; newPlotPointTitle: string; setNewPlotPointTitle: (title: string) => void; selectedIncidentId: string; setSelectedIncidentId: (id: string) => void; onAddPlotPoint: (actId: number) => Promise; onDeletePlotPoint: (actId: number, plotPointId: string) => Promise; onLinkChapter: (actId: number, chapterId: string, plotPointId: string) => Promise; onUpdateChapterSummary: (chapterId: string, summary: string, plotPointId: string) => void; onUnlinkChapter: (chapterInfoId: string, chapterId: string, plotPointId: string) => Promise; sectionKey: string; isExpanded: boolean; onToggleSection: (sectionKey: string) => void; } export default function ActPlotPoints({ plotPoints, incidents, actId, mainChapters, newPlotPointTitle, setNewPlotPointTitle, selectedIncidentId, setSelectedIncidentId, onAddPlotPoint, onDeletePlotPoint, onLinkChapter, onUpdateChapterSummary, onUnlinkChapter, sectionKey, isExpanded, onToggleSection, }: ActPlotPointsProps) { const t = useTranslations('actComponent'); const [expandedItems, setExpandedItems] = useState<{ [key: string]: boolean }>({}); const [selectedChapterId, setSelectedChapterId] = useState(''); function toggleItem(itemKey: string): void { setExpandedItems(prev => ({ ...prev, [itemKey]: !prev[itemKey], })); } function getIncidentData(): SelectBoxProps[] { return incidents.map((incident: Incident): SelectBoxProps => ({ value: incident.incidentId, label: incident.title, })); } return (
{plotPoints && plotPoints.length > 0 ? ( plotPoints.map(function (item: PlotPoint): React.JSX.Element { const itemKey: string = `plotpoint_${item.plotPointId}`; const isItemExpanded: boolean = expandedItems[itemKey]; const linkedIncident: Incident | undefined = incidents.find( function (inc: Incident): boolean { return inc.incidentId === item.linkedIncidentId; } ); return (
{isItemExpanded && (
{item.chapters && item.chapters.length > 0 ? ( item.chapters.map(function (chapter: ActChapter): React.JSX.Element { return ( { return onUnlinkChapter(chapterInfoId, chapterId, item.plotPointId); }} /> ); }) ) : (

{t('noLinkedChapter')}

)} ): void { setSelectedChapterId(e.target.value); }} data={mainChapters.map(function (chapter: ChapterListProps): SelectBoxProps { return { label: `${chapter.chapterOrder}. ${chapter.title}`, value: chapter.chapterId, }; })} defaultValue={selectedChapterId} placeholder={t('selectChapterPlaceholder')} /> } addButtonCallBack={function (): Promise { return onLinkChapter(actId, selectedChapterId, item.plotPointId); }} isAddButtonDisabled={!selectedChapterId} />
)}
); }) ) : (

{t('noPlotPointAdded')}

)}
): void { setNewPlotPointTitle(e.target.value); }} placeholder={t('newPlotPointPlaceholder')} /> ): void { setSelectedIncidentId(e.target.value); }} data={getIncidentData()} /> } actionIcon={Plus} addButtonCallBack={function (): Promise { return onAddPlotPoint(actId); }} isAddButtonDisabled={newPlotPointTitle.trim() === ''} />
); }