import type { RawData } from "../../types"

export type MatchColumnsProps<T extends string> = {
  data: RawData[]
  headerValues: RawData
  onContinue: (data: any[], rawData: RawData[], columns: Columns<T>, globalSelections?: GlobalSelections, useNewValidation?: boolean) => void
  onBack?: () => void
  initialGlobalSelections?: GlobalSelections
  /**
   * Previously-matched columns to restore when navigating back to this step.
   * When provided, columns seed from these instead of re-deriving + auto-mapping,
   * so the user's mappings/value-mappings/ignored/AI-supplemental state survive back-nav.
   */
  initialColumns?: Columns<T>
}

export type GlobalSelections = {
  supplier?: string
  company?: string
  line?: string
  subline?: string
  costIsTotalCost?: boolean
}

export enum ColumnType {
  empty,
  ignored,
  matched,
  matchedCheckbox,
  matchedSelect,
  matchedSelectOptions,
  matchedMultiInput,
  matchedMultiSelect,
  aiSupplemental,
}

export type MatchedOptions<T> = {
  entry: string
  value: T
}

type EmptyColumn = { type: ColumnType.empty; index: number; header: string }
type IgnoredColumn = { type: ColumnType.ignored; index: number; header: string }
type MatchedColumn<T> = { type: ColumnType.matched; index: number; header: string; value: T }
type MatchedSwitchColumn<T> = { type: ColumnType.matchedCheckbox; index: number; header: string; value: T }
export type MatchedSelectColumn<T> = {
  type: ColumnType.matchedSelect
  index: number
  header: string
  value: T
  matchedOptions: Partial<MatchedOptions<T>>[]
}
export type MatchedSelectOptionsColumn<T> = {
  type: ColumnType.matchedSelectOptions
  index: number
  header: string
  value: T
  matchedOptions: MatchedOptions<T>[]
}
export type MatchedMultiInputColumn<T> = {
  type: ColumnType.matchedMultiInput
  index: number
  header: string
  value: T
}
export type MatchedMultiSelectColumn<T> = {
  type: ColumnType.matchedMultiSelect
  index: number
  header: string
  value: T
  matchedOptions: MatchedOptions<T>[]
}
type AiSupplementalColumn = { type: ColumnType.aiSupplemental; index: number; header: string }

export type Column<T extends string> =
  | EmptyColumn
  | IgnoredColumn
  | MatchedColumn<T>
  | MatchedSwitchColumn<T>
  | MatchedSelectColumn<T>
  | MatchedSelectOptionsColumn<T>
  | MatchedMultiInputColumn<T>
  | MatchedMultiSelectColumn<T>
  | AiSupplementalColumn

export type Columns<T extends string> = Column<T>[]

