import { Color } from '@lib/color'

export type Model = {
  rows: number
  cols: number
  colWidths?: number[]
  rowHeights?: number[]
  merged?: MergeRange[]
  cells: Cell[]
}

export type MergeRange = { r0: number; c0: number; r1: number; c1: number }

export type CellValue = string | number | null

export type HorizontalAlign = 'left' | 'center' | 'right'
export type VerticalAlign = 'top' | 'center' | 'bottom'
export type Cell = {
  r: number
  c: number
  value: CellValue
  rowSpan?: number
  colSpan?: number
  formula?: string // e.g. "=SUM(R[-1]C[-1]:R[-1]C[1])"
  binding?: Binding | null

  fmt?: {
    /** Raw Excel-like format string, e.g. 0.0%;(0.0%);"--" */
    numberFormat?: string
    normalized?: NormalizedFormat
  }

  align?: {
    horizontal: HorizontalAlign | null
    vertical: VerticalAlign | null
    wrapText?: boolean
  }

  font?: {
    name?: string
    size?: number
    bold?: boolean
    italic?: boolean
    underline?: boolean
    color?: Color
  }

  fill?: { color?: Color }

  borders?: {
    top?: { style?: 'solid' | 'dashed' | 'dotted'; color?: Color } | null
    right?: { style?: 'solid' | 'dashed' | 'dotted'; color?: Color } | null
    bottom?: { style?: 'solid' | 'dashed' | 'dotted'; color?: Color } | null
    left?: { style?: 'solid' | 'dashed' | 'dotted'; color?: Color } | null
  }
}

export type NormalizedFormat = {
  kind?: 'text' | 'decimal' | 'percent' | 'currency'
  decimals?: number
  useGrouping?: boolean
  negativeStyle?: 'minus' | 'parentheses'
  zeroPlaceholder?: string
  currency?: string
}

export type Binding = { kind: 'block'; name: string; rOff?: number; cOff?: number }

export type DataBlocks = Record<string, CellValue | CellValue[] | CellValue[][]>
