import type { Token } from '../core/types';
import type { Breakpoint } from '../breakpoints/types';
import type { ContextProfile, FontSpec, MeasureWidthImpl } from '../measure/types';
import type { Lexicons, ScoringConfig, ScoreTerm } from '../scoring/types';

export type LineMode = 'AUTO' | 'FIXED';

export type SearchAppConfig = {
  /** TopK（文档建议 App=10） */
  topK: number;
  /** 超长阈值（用于 TOO_LONG） */
  tooLongThresholds: { EN: number; TC: number };
};

export type SearchMeasureInput = {
  contextProfile: ContextProfile;
  fontSpec: Partial<FontSpec> | null | undefined;
  measureWidthImpl?: MeasureWidthImpl;
};

export type SearchScoringInput = {
  config: ScoringConfig;
  lexicons: Lexicons;
  debug?: boolean;
};

export type SearchAppInput = {
  tokens: Token[];
  lang: 'TC' | 'EN';
  breakpoints: Breakpoint[];
  availableWidth: number;
  maxLines: number;
  lineMode: LineMode;
  measure: SearchMeasureInput;
  scoring: SearchScoringInput;
  config?: Partial<SearchAppConfig>;
};

export type BestLayout = {
  breaks: number[];
  lines: string[];
  wrappedText: string;
  meta?: {
    breaks: number[];
    scoreTopTerms?: ScoreTerm[];
    score?: number;
  };
};

export type SearchFailureReason = 'TOO_LONG' | 'WIDTH_UNKNOWN' | 'MEASURE_FAILED' | 'NO_CANDIDATE';

export type SearchAppResult =
  | { ok: true; bestLayout: BestLayout }
  | { ok: false; reason: SearchFailureReason; meta?: { tokenCount: number } };

export type PartialLayout = {
  pos: number;
  breaks: number[];
  lines: Array<{
    start: number;
    end: number;
    text: string;
    width: number;
    tokenCount: number;
    charCount: number;
  }>;
  score: number;
  tieKey: number[];
  scoreTopTerms?: ScoreTerm[];
};

