export interface RGBColor {
  r: number
  g: number
  b: number
}

export interface LabColor {
  l: number
  a: number
  b: number
}

export interface Point {
  x: number
  y: number
}

export interface BoundingBox {
  x: number
  y: number
  width: number
  height: number
}

/** Triplets of y, inclusive xStart and inclusive xEnd. */
export interface RegionMask {
  width: number
  height: number
  runs: Uint32Array
}

export interface MosaicRegion {
  id: string
  color: RGBColor
  labColor: LabColor
  pixelCount: number
  boundingBox: BoundingBox
  labelPosition: Point
  userClickPosition?: Point
  groupId: string
  mask: RegionMask
}

export interface ColorGroup {
  id: string
  number: number
  color: RGBColor
  labColor: LabColor
  regionIds: string[]
  customColor?: boolean
}

export interface MosaicSettings {
  floodTolerance: number
  colorTolerance: number
  darkBoundaryThreshold: number
  connectivity: 4 | 8
  exportOverlay: boolean
  otherGroupsMode: 'normal' | 'dim' | 'hidden'
  useUserClickPositionForLabels: boolean
}

export type SerializedMosaicSettings = Omit<MosaicSettings, 'useUserClickPositionForLabels'> & {
  useUserClickPositionForLabels?: boolean
}

export interface ProjectDimensions {
  width: number
  height: number
}

export interface SerializedRegion extends Omit<MosaicRegion, 'mask'> {
  mask: { width: number; height: number; runs: number[] }
}

export interface ProjectFile {
  version: 1
  createdAt: string
  imageName: string
  imageDataUrl?: string
  dimensions: ProjectDimensions
  settings: SerializedMosaicSettings
  regions: SerializedRegion[]
  groups: ColorGroup[]
}

export interface ToastMessage {
  id: string
  type: 'info' | 'success' | 'warning' | 'error'
  text: string
}
