<template>
	<div class="conversation-list">
							<template v-for="entry in visibleConversationEntries" :key="entry.id">
								<message-item
									v-if="entry.type === 'message' && entry.message && !isDelegatedToolResultEntry(entry)"
									:message="toChatMessage(entry)"
								/>

								<delegated-result-message-card
									v-else-if="entry.type === 'message' && entry.message && isDelegatedToolResultEntry(entry)"
									:entry="entry"
									:message="toChatMessage(entry)"
									:subagent-result-headline="subagentResultHeadline"
									:subagent-result-count="subagentResultCount"
									:subagent-projection-diagnostics="subagentProjectionDiagnostics"
									:subagent-projection-diagnostics-display="subagentProjectionDiagnosticsDisplay"
									:subagent-projection-diagnostics-summary="subagentProjectionDiagnosticsSummary"
									:subagent-task-panels="subagentTaskPanels"
									:visible-task-evidence-summaries="visibleTaskEvidenceSummaries"
									:has-more-task-evidence-summaries="hasMoreTaskEvidenceSummaries"
									:toggle-subagent-evidence-expanded="toggleSubagentEvidenceExpanded"
									:task-panel-scope-key="taskPanelScopeKey"
									:is-subagent-evidence-expanded="isSubagentEvidenceExpanded"
									:evidence-kind-label="evidenceKindLabel"
									:subagent-tool-execution-count-label="subagentToolExecutionCountLabel"
									:subagent-tool-execution-label="subagentToolExecutionLabel"
									:subagent-tool-execution-meta="subagentToolExecutionMeta"
									:is-tool-execution-block-collapsed="isToolExecutionBlockCollapsed"
									:stringify-content="stringifyContent"
									:has-long-tool-execution-block="hasLongToolExecutionBlock"
									:toggle-tool-execution-block="toggleToolExecutionBlock"
									:is-tool-execution-block-expanded="isToolExecutionBlockExpanded"
									:visible-task-process-events="visibleTaskProcessEvents"
									:process-event-label="processEventLabel"
									:format-timestamp="formatTimestamp"
									:process-event-detail="processEventDetail"
									:process-event-payload-preview="processEventPayloadPreview"
									:is-process-event-payload-collapsed="isProcessEventPayloadCollapsed"
									:has-process-event-long-payload="hasProcessEventLongPayload"
									:toggle-process-event-payload-expanded="toggleProcessEventPayloadExpanded"
									:is-process-event-payload-expanded="isProcessEventPayloadExpanded"
									:has-more-task-process-events="hasMoreTaskProcessEvents"
									:is-subagent-process-expanded="isSubagentProcessExpanded"
									:toggle-subagent-process-expanded="toggleSubagentProcessExpanded"
									@continue="handleContinue"
								/>

								<branch-summary-card
									v-else-if="entry.type === 'branch_summary'"
									:entry="entry"
									:branch-source-label="branchSourceLabel"
									:branch-summary-text="branchSummaryText"
								/>

								<compaction-card
									v-else-if="entry.type === 'compaction'"
									:entry="entry"
									:compaction-summary-text="compactionSummaryText"
									:compaction-token-label="compactionTokenLabel"
									:compaction-boundary-label="compactionBoundaryLabel"
								/>

								<subagent-batch-card
									v-else-if="entry.type === 'subagent_batch'"
									:entry="entry"
									:batch-content="subagentBatchContent(entry)"
									:subagent-state="subagentStateForEntry(entry)"
									:subagent-batch-headline="subagentBatchHeadline"
									:subagent-status-label="subagentStatusLabel"
									:subagent-task-count="subagentTaskCount"
									:subagent-runtime-route-summary="subagentRuntimeRouteSummary"
									:subagent-task-preview="subagentTaskPreview"
									:subagent-event-label="subagentEventLabel"
									:stringify-content="stringifyContent"
								/>

								<subagent-result-card
									v-else-if="entry.type === 'subagent_result' || isDelegatedToolResultEntry(entry)"
									:entry="entry"
									:is-delegated-tool-result-entry="isDelegatedToolResultEntry"
									:subagent-result-headline="subagentResultHeadline"
									:subagent-result-count="subagentResultCount"
									:subagent-result-batch-label="subagentResultBatchLabel"
									:subagent-projection-diagnostics="subagentProjectionDiagnostics"
									:subagent-projection-diagnostics-display="subagentProjectionDiagnosticsDisplay"
									:subagent-projection-diagnostics-summary="subagentProjectionDiagnosticsSummary"
									:subagent-task-panels="subagentTaskPanels"
									:subagent-result-preview="subagentResultPreview"
									:subagent-evidence-summaries="subagentEvidenceSummaries"
									:has-more-evidence-summaries="hasMoreEvidenceSummaries"
									:visible-subagent-process-events="visibleSubagentProcessEvents"
									:subagent-process-events="subagentProcessEvents"
									:has-more-process-events="hasMoreProcessEvents"
									:visible-task-evidence-summaries="visibleTaskEvidenceSummaries"
									:has-more-task-evidence-summaries="hasMoreTaskEvidenceSummaries"
									:toggle-subagent-evidence-expanded="toggleSubagentEvidenceExpanded"
									:task-panel-scope-key="taskPanelScopeKey"
									:is-subagent-evidence-expanded="isSubagentEvidenceExpanded"
									:evidence-kind-label="evidenceKindLabel"
									:subagent-tool-execution-count-label="subagentToolExecutionCountLabel"
									:subagent-tool-execution-label="subagentToolExecutionLabel"
									:subagent-tool-execution-meta="subagentToolExecutionMeta"
									:is-tool-execution-block-collapsed="isToolExecutionBlockCollapsed"
									:stringify-content="stringifyContent"
									:has-long-tool-execution-block="hasLongToolExecutionBlock"
									:toggle-tool-execution-block="toggleToolExecutionBlock"
									:is-tool-execution-block-expanded="isToolExecutionBlockExpanded"
									:visible-task-process-events="visibleTaskProcessEvents"
									:process-event-label="processEventLabel"
									:format-timestamp="formatTimestamp"
									:process-event-detail="processEventDetail"
									:process-event-payload-preview="processEventPayloadPreview"
									:is-process-event-payload-collapsed="isProcessEventPayloadCollapsed"
									:has-process-event-long-payload="hasProcessEventLongPayload"
									:toggle-process-event-payload-expanded="toggleProcessEventPayloadExpanded"
									:is-process-event-payload-expanded="isProcessEventPayloadExpanded"
									:has-more-task-process-events="hasMoreTaskProcessEvents"
									:is-subagent-process-expanded="isSubagentProcessExpanded"
									:toggle-subagent-process-expanded="toggleSubagentProcessExpanded"
								/>

								<fallback-entry-card
									v-else
									:entry="entry"
									:entry-kind-label="entryKindLabel"
									:stringify-content="stringifyContent"
								/>
							</template>
	</div>

</template>

<script lang="ts" setup>
import MessageItem from '../message-item.vue';
import BranchSummaryCard from './BranchSummaryCard.vue';
import CompactionCard from './CompactionCard.vue';
import DelegatedResultMessageCard from './DelegatedResultMessageCard.vue';
import FallbackEntryCard from './FallbackEntryCard.vue';
import SubagentBatchCard from './SubagentBatchCard.vue';
import SubagentResultCard from './SubagentResultCard.vue';
import type {
	ChatMessage,
	SessionTreeEntry,
	SessionTreeSubagentBatchContent,
	SubagentEvidenceSummary,
	SubagentProcessEvent,
	SubagentProjectionDiagnostics,
	SubagentRuntimeState,
	SubagentTaskPanel,
	SubagentToolExecution,
} from '../../types/index.d';

type DiagnosticsDisplay = {
	projectionPathLabel: string;
	selectedSourceLabel: string;
	inputSourcesLabel: string;
	ignoredSourcesLabel: string;
};

defineProps<{
	visibleConversationEntries: SessionTreeEntry[];
	toChatMessage: (entry: SessionTreeEntry) => ChatMessage;
	isDelegatedToolResultEntry: (entry: SessionTreeEntry) => boolean;
	subagentResultHeadline: (entry: SessionTreeEntry) => string;
	subagentResultCount: (entry: SessionTreeEntry) => number;
	subagentResultBatchLabel: (entry: SessionTreeEntry) => string;
	subagentProjectionDiagnostics: (entry: SessionTreeEntry) => SubagentProjectionDiagnostics | null;
	subagentProjectionDiagnosticsDisplay: (entry: SessionTreeEntry) => DiagnosticsDisplay;
	subagentProjectionDiagnosticsSummary: (entry: SessionTreeEntry) => string;
	subagentTaskPanels: (entry: SessionTreeEntry) => SubagentTaskPanel[];
	visibleTaskEvidenceSummaries: (entry: SessionTreeEntry, panel: SubagentTaskPanel) => SubagentEvidenceSummary[];
	hasMoreTaskEvidenceSummaries: (panel: SubagentTaskPanel) => boolean;
	toggleSubagentEvidenceExpanded: (entryId: string) => void;
	taskPanelScopeKey: (entryId: string, panel: SubagentTaskPanel) => string;
	isSubagentEvidenceExpanded: (entryId: string) => boolean;
	evidenceKindLabel: (kind?: string) => string;
	subagentToolExecutionCountLabel: (panel: SubagentTaskPanel) => string;
	subagentToolExecutionLabel: (execution: SubagentToolExecution) => string;
	subagentToolExecutionMeta: (execution: SubagentToolExecution) => string[];
	isToolExecutionBlockCollapsed: (execution: SubagentToolExecution, field: 'args' | 'result' | 'error') => boolean;
	stringifyContent: (content: unknown) => string;
	hasLongToolExecutionBlock: (execution: SubagentToolExecution, field: 'args' | 'result' | 'error') => boolean;
	toggleToolExecutionBlock: (execution: SubagentToolExecution, field: 'args' | 'result' | 'error') => void;
	isToolExecutionBlockExpanded: (execution: SubagentToolExecution, field: 'args' | 'result' | 'error') => boolean;
	visibleTaskProcessEvents: (entry: SessionTreeEntry, panel: SubagentTaskPanel) => SubagentProcessEvent[];
	processEventLabel: (event: SubagentProcessEvent) => string;
	formatTimestamp: (timestamp?: string) => string;
	processEventDetail: (event: SubagentProcessEvent) => string;
	processEventPayloadPreview: (event: SubagentProcessEvent) => string;
	isProcessEventPayloadCollapsed: (entryId: string, index: number, event: SubagentProcessEvent) => boolean;
	hasProcessEventLongPayload: (event: SubagentProcessEvent) => boolean;
	toggleProcessEventPayloadExpanded: (entryId: string, index: number, event: SubagentProcessEvent) => void;
	isProcessEventPayloadExpanded: (entryId: string, index: number, event: SubagentProcessEvent) => boolean;
	hasMoreTaskProcessEvents: (panel: SubagentTaskPanel) => boolean;
	isSubagentProcessExpanded: (entryId: string) => boolean;
	toggleSubagentProcessExpanded: (entryId: string) => void;
	branchSourceLabel: (entry: SessionTreeEntry) => string;
	branchSummaryText: (entry: SessionTreeEntry) => string;
	compactionSummaryText: (entry: SessionTreeEntry) => string;
	compactionTokenLabel: (entry: SessionTreeEntry) => string;
	compactionBoundaryLabel: (entry: SessionTreeEntry) => string;
	subagentBatchContent: (entry: SessionTreeEntry) => SessionTreeSubagentBatchContent;
	subagentStateForEntry: (entry: SessionTreeEntry) => SubagentRuntimeState | null;
	subagentBatchHeadline: (entry: SessionTreeEntry) => string;
	subagentStatusLabel: (status?: string) => string;
	subagentTaskCount: (entry: SessionTreeEntry) => number;
	subagentRuntimeRouteSummary: (entry: SessionTreeEntry) => string;
	subagentTaskPreview: (entry: SessionTreeEntry) => string[];
	subagentEventLabel: (kind: 'started' | 'event' | 'completed') => string;
	subagentResultPreview: (entry: SessionTreeEntry) => string[];
	subagentEvidenceSummaries: (entry: SessionTreeEntry) => SubagentEvidenceSummary[];
	hasMoreEvidenceSummaries: (entry: SessionTreeEntry) => boolean;
	visibleSubagentProcessEvents: (entry: SessionTreeEntry) => SubagentProcessEvent[];
	subagentProcessEvents: (entry: SessionTreeEntry) => SubagentProcessEvent[];
	hasMoreProcessEvents: (entry: SessionTreeEntry) => boolean;
	entryKindLabel: (entry: SessionTreeEntry) => string;
}>();

const emit = defineEmits<{
	continue: [entryId: string];
}>();

function handleContinue(entryId: string) {
	emit('continue', entryId);
}
</script>
