import type { Token } from '../core/types';
import type { ContextProfile, FontSpec, MeasureWidthImpl, TextWrapContext } from '../measure/types';

export type OverflowMode = 'ELLIPSIS' | 'CLIP' | 'SYSTEM_DEFAULT';
export type FallbackType = 'NONE' | 'RELAX_RULES' | 'SYSTEM_DEFAULT';
export type OverflowType = 'NONE' | 'ELLIPSIS' | 'CLIP';

export type OverflowReason = 'NO_CANDIDATE' | 'WIDTH_UNKNOWN' | 'TOO_LONG' | 'WIDOW' | 'PARTICLE' | string;

export type PartialLayoutLine = { start: number; end: number; text?: string };
export type PartialLayoutInput = { breaks: number[]; lines: PartialLayoutLine[] };

export type OverflowMeasure = {
  context: TextWrapContext;
  contextProfile: ContextProfile;
  fontSpec: Partial<FontSpec> | null | undefined;
  measureWidthImpl?: MeasureWidthImpl;
  widgetEnableMeasure?: boolean;
};

export type ApplyOverflowFallbackInput = {
  tokens: Token[];
  lang: 'TC' | 'EN';
  context: TextWrapContext;
  availableWidth: number;
  maxLines: number;
  overflowMode: OverflowMode;
  ellipsisToken: string;
  reason: OverflowReason;
  partialLayout?: PartialLayoutInput | null;
  measure?: OverflowMeasure;
  /** TC 标点集合（用于清理 `，…`） */
  tcPunctuations?: string[];
};

export type WrapTextMeta = {
  fallback_type: FallbackType;
  overflow_type: OverflowType;
  reason: OverflowReason;
};

export type ApplyOverflowFallbackResult = {
  lines: string[];
  wrappedText: string;
  meta: WrapTextMeta;
};

