/**
 * Shared types for DOCX template filling (docxtemplater + docx-preview + html2pdf).
 */

export type DataRecord = Record<string, string | number | boolean | DataRecord[] | null | undefined>;

export interface TemplatePlaceholder {
  kind: 'scalar' | 'loop';
  name: string;
  /** Inner field names for `{#block}…{/block}` */
  innerTags?: string[];
}

export type FillProgressPhase =
  | 'parsing-template'
  | 'parsing-data'
  | 'rendering'
  | 'converting-to-pdf'
  | 'done'
  | 'error';

export interface FillProgressEvent {
  phase: FillProgressPhase;
  error?: Error;
  page?: number;
  totalPages?: number;
}

export interface ParsedData {
  format: 'json' | 'csv' | 'xlsx';
  records: DataRecord[];
}

export interface RenderErrorDetails {
  tag?: string;
  id?: string;
  explanation?: string;
}

export class RenderError extends Error {
  details?: RenderErrorDetails[];

  constructor(message: string, details?: RenderErrorDetails[]) {
    super(message);
    this.name = 'RenderError';
    this.details = details;
  }
}
