/**
 * Types for Paper/Adventure "Component" JSON — this is the format you get when a
 * MiniMessage string is parsed on the server and serialized with GsonComponentSerializer.
 * By the time it reaches your frontend, tags like <gradient>, <rainbow>, <color>, etc.
 * have already been resolved into a tree of styled text runs (this is why you don't see
 * "<red>" anywhere in the JSON — you see {"color":"red", ...} instead).
 */

export type ClickAction =
  | 'open_url'
  | 'open_file'
  | 'run_command'
  | 'suggest_command'
  | 'change_page'
  | 'copy_to_clipboard';

export interface MiniMessageClickEvent {
  action: ClickAction;
  value: string;
}

export type HoverAction = 'show_text' | 'show_item' | 'show_entity';

export interface MiniMessageHoverEvent {
  action: HoverAction;
  /** Modern (1.16+) format */
  value?: MiniMessageComponent | string;
  /** Legacy format some plugins/serializers still emit for show_text */
  contents?: MiniMessageComponent | string | Record<string, unknown>;
}

/**
 * A single node in the component tree. Every field is optional because children only need
 * to specify what they *override* — everything else is inherited from the parent (color,
 * bold, italic, underlined, strikethrough, obfuscated, font, clickEvent, hoverEvent, insertion).
 */
export interface MiniMessageComponent {
  /** Plain text component */
  text?: string;

  /** Translatable component (e.g. vanilla "chat.type.text") */
  translate?: string;
  /** Substitution arguments for `translate`, in order */
  with?: MiniMessageComponent[];
  /** Fallback text if no translation is registered for `translate` */
  fallback?: string;

  /** Keybind component, e.g. "key.jump" */
  keybind?: string;

  /** Score component (scoreboard value) */
  score?: { name: string; objective: string; value?: string };

  /** Selector component, e.g. "@a" */
  selector?: string;

  /** Named color ("red", "aqua", ...), hex ("#RRGGBB"), or "reset" */
  color?: string;
  bold?: boolean;
  italic?: boolean;
  underlined?: boolean;
  strikethrough?: boolean;
  obfuscated?: boolean;
  font?: string;

  /** Text inserted into chat input on shift-click (Minecraft-client-only concept) */
  insertion?: string;

  clickEvent?: MiniMessageClickEvent;
  hoverEvent?: MiniMessageHoverEvent;

  /** Child components, styled relative to this node */
  extra?: MiniMessageComponent[];
}

/** Fully resolved style at a given node, after inheriting from all ancestors */
export interface ResolvedStyle {
  color?: string;
  bold: boolean;
  italic: boolean;
  underlined: boolean;
  strikethrough: boolean;
  obfuscated: boolean;
  font?: string;
}

export interface MiniMessageClickPayload {
  action: ClickAction;
  value: string;
}
