import type { TextSegment } from './utils';
import type { Card } from '$lib/config';

// Re-export TextSegment for convenience
export type { TextSegment };

export type LineType =
	| 'command' | 'output' | 'prompt' | 'error' | 'success' | 'info' | 'warning'
	| 'image' | 'blank' | 'header' | 'button' | 'divider' | 'link'
	| 'card' | 'progress' | 'accordion' | 'table' | 'tooltip' | 'cardgrid'
	| 'input' | 'textarea' | 'checkbox' | 'radio' | 'select' | 'toggle'
	| 'group';

// Option type for radio and select components
export interface FormOption {
	value: string;
	label: string;
	icon?: string;
	disabled?: boolean;
}

// Base interface for all terminal lines
export interface BaseTerminalLine {
	type: LineType;
	content: string;
	id?: string;
	inline?: boolean;
	display?: 'flex' | 'block' | 'grid' | 'inline';
	delay?: number;
	style?: 'primary' | 'secondary' | 'accent' | 'warning' | 'error';
}

// Specific line types
export interface CommandLine extends BaseTerminalLine {
	type: 'command';
}

export interface OutputLine extends BaseTerminalLine {
	type: 'output';
}

export interface PromptLine extends BaseTerminalLine {
	type: 'prompt';
}

export interface ErrorLine extends BaseTerminalLine {
	type: 'error';
}

export interface SuccessLine extends BaseTerminalLine {
	type: 'success';
}

export interface InfoLine extends BaseTerminalLine {
	type: 'info';
}

export interface WarningLine extends BaseTerminalLine {
	type: 'warning';
}

export interface BlankLine extends BaseTerminalLine {
	type: 'blank';
}

export interface DividerLine extends BaseTerminalLine {
	type: 'divider';
}

export interface HeaderLine extends BaseTerminalLine {
	type: 'header';
}

export interface ButtonLine extends BaseTerminalLine {
	type: 'button';
	action?: () => void;
	href?: string;
	icon?: string;
	external?: boolean;
}

export interface LinkLine extends BaseTerminalLine {
	type: 'link';
	href: string;
	external?: boolean;
	icon?: string;
}

export interface ImageLine extends BaseTerminalLine {
	type: 'image';
	image: string;
	imageAlt?: string;
	imageWidth?: number;
}

export interface CardLine extends BaseTerminalLine {
	type: 'card';
	cardTitle?: string;
	cardFooter?: string;
	icon?: string;
	image?: string;
	imageAlt?: string;
	children?: TerminalLine[];
}

export interface ProgressLine extends BaseTerminalLine {
	type: 'progress';
	progress: number; // 0-100
	progressLabel?: string;
}

export interface AccordionLine extends BaseTerminalLine {
	type: 'accordion';
	accordionItems?: { title: string; content: string }[];
	children?: TerminalLine[];
	accordionOpen?: boolean;
}

export interface TableLine extends BaseTerminalLine {
	type: 'table';
	tableHeaders: string[];
	tableRows: string[][];
}

export interface TooltipLine extends BaseTerminalLine {
	type: 'tooltip';
	tooltipText: string;
	tooltipPosition?: 'top' | 'bottom' | 'left' | 'right';
}

export interface CardGridLine extends BaseTerminalLine {
	type: 'cardgrid';
	cards: Card[];
}

export interface InputLine extends BaseTerminalLine {
	type: 'input';
	inputPlaceholder?: string;
	inputType?: 'text' | 'email' | 'password' | 'number' | 'url' | 'tel' | 'search';
	inputDisabled?: boolean;
	inputError?: boolean;
	inputErrorMessage?: string;
	inputPrefix?: string;
	inputSuffix?: string;
	icon?: string;
}

export interface TextareaLine extends BaseTerminalLine {
	type: 'textarea';
	inputPlaceholder?: string;
	textareaRows?: number;
	textareaMaxLength?: number;
	inputDisabled?: boolean;
	inputError?: boolean;
	inputErrorMessage?: string;
	icon?: string;
}

export interface CheckboxLine extends BaseTerminalLine {
	type: 'checkbox';
	checkboxIndeterminate?: boolean;
	inputDisabled?: boolean;
	icon?: string;
}

export interface RadioLine extends BaseTerminalLine {
	type: 'radio';
	radioOptions: FormOption[];
	radioHorizontal?: boolean;
	inputDisabled?: boolean;
	icon?: string;
}

export interface SelectLine extends BaseTerminalLine {
	type: 'select';
	selectOptions: FormOption[];
	selectSearchable?: boolean;
	inputPlaceholder?: string;
	inputDisabled?: boolean;
	inputError?: boolean;
	inputErrorMessage?: string;
	icon?: string;
}

export interface ToggleLine extends BaseTerminalLine {
	type: 'toggle';
	toggleOnLabel?: string;
	toggleOffLabel?: string;
	toggleShowLabels?: boolean;
	inputDisabled?: boolean;
	icon?: string;
}

export interface GroupLine extends BaseTerminalLine {
	type: 'group';
	children: TerminalLine[];
	groupDirection?: 'row' | 'column';
	groupAlign?: 'start' | 'center' | 'end';
	groupGap?: string;
}

// Discriminated union of all line types
export type TerminalLine =
	| CommandLine | OutputLine | PromptLine | ErrorLine | SuccessLine | InfoLine | WarningLine
	| BlankLine | DividerLine | HeaderLine | ButtonLine | LinkLine | ImageLine
	| CardLine | ProgressLine | AccordionLine | TableLine | TooltipLine | CardGridLine
	| InputLine | TextareaLine | CheckboxLine | RadioLine | SelectLine | ToggleLine
	| GroupLine;

// Terminal API for reactive manipulation
export interface TerminalAPI {
	/** Clear all lines from the terminal */
	clear: () => void;
	/** Write a new line to the terminal (appends) */
	write: (line: TerminalLine) => void;
	/** Write multiple lines to the terminal (appends) */
	writeLines: (lines: TerminalLine[]) => void;
	/** Update a specific line by index */
	update: (index: number, line: Partial<TerminalLine>) => void;
	/** Update a line's content by index */
	updateContent: (index: number, content: string) => void;
	/** Insert a line at a specific index */
	insert: (index: number, line: TerminalLine) => void;
	/** Remove a line by index */
	remove: (index: number) => void;
	/** Remove lines by range */
	removeRange: (startIndex: number, count: number) => void;
	/** Replace all lines */
	setLines: (lines: TerminalLine[]) => void;
	/** Get current line count */
	getLineCount: () => number;
	/** Get a line by index */
	getLine: (index: number) => TerminalLine | undefined;
	/** Get all lines */
	getLines: () => TerminalLine[];
	/** Find line index by id */
	findById: (id: string) => number;
	/** Update a line by its id */
	updateById: (id: string, line: Partial<TerminalLine>) => void;
	/** Remove a line by its id */
	removeById: (id: string) => void;
	/** Scroll to bottom */
	scrollToBottom: () => void;
	/** Scroll to a specific line index */
	scrollToLine: (index: number) => void;
	/** Check if terminal is currently animating */
	isAnimating: () => boolean;
	/** Skip current animation */
	skip: () => void;
	/** Restart the typing animation */
	restart: () => void;
}

// Pre-parsed line with segments ready for rendering
export interface ParsedLine {
	line: TerminalLine;
	segments: TextSegment[];
	plainText: string;  // Plain text without color codes for length calculation
}

// Display state for a line during typing animation
export interface DisplayedLine {
	parsed: ParsedLine;
	charIndex: number;  // How many plain-text characters to show
	complete: boolean;
	showImage: boolean;
}
