import { t } from '#cli/i18n'
import { clamp } from '../../utils/math'

export function getConfigDialogStrings() {
  return {
    title: t('configDialog.title'),
    hint: t('configDialog.hint'),
    inputClearTip: t('common.inputClearTip'),
    apply: t('common.apply'),
    cancel: t('common.cancel'),
    contextWindowTitle: t('configDialog.contextWindowTitle'),
    contextWindowLocked: t('configDialog.contextWindowLocked'),
    contextWindowPlaceholder: t('configDialog.contextWindowPlaceholder'),
    maxTokensTitle: t('configDialog.maxTokensTitle'),
    maxTokensPlaceholder: t('configDialog.maxTokensPlaceholder'),
    temperatureTitle: t('configDialog.temperatureTitle'),
    temperaturePlaceholder: t('configDialog.temperaturePlaceholder'),
    temperatureValidationError: t('configDialog.temperatureValidationError'),
    interleavedThinkingTitle: t('configDialog.interleavedThinkingTitle'),
    interleavedThinkingLocked: t('configDialog.interleavedThinkingLocked'),
    interleavedThinkingOpen: t('configDialog.interleavedThinkingOpen'),
    interleavedThinkingClose: t('configDialog.interleavedThinkingClose'),
    shortcutsTitle: t('configDialog.shortcutsTitle'),
    shortcutCommandsLabel: t('configDialog.shortcutCommandsLabel'),
    shortcutTimelineLabel: t('configDialog.shortcutTimelineLabel'),
    shortcutNewSessionLabel: t('configDialog.shortcutNewSessionLabel'),
    shortcutsPlaceholder: t('configDialog.shortcutsPlaceholder'),
    shortcutsHint: t('configDialog.shortcutsHint'),
  } as const
}

export type ConfigDialogLayout = Readonly<{
  dialogW: number
  dialogH: number
  innerW: number
  innerH: number
  contentH: number

  showHint: boolean

  fieldBoxH: number
  inputH: number

  colGap: number
  contextWindowW: number
  maxTokensW: number
  temperatureW: number

  samplingY: number
  maxTokensTemperatureY: number

  interleavedThinkingY: number
  interleavedThinkingH: number
  interleavedInputH: number

  showShortcuts: boolean
  shortcutsY: number
  shortcutsH: number
  shortcutsHintY: number
}>

/**
 * Pure layout computation for the config dialog. This is intentionally independent
 * of theme/style so it can be reused and tested in isolation.
 */
export function computeConfigDialogLayout(
  input: Readonly<{ w: number; h: number }>,
): ConfigDialogLayout {
  const dialogW = Math.max(44, Math.floor(input.w))
  const dialogH = Math.max(14, Math.floor(input.h))
  const innerW = Math.max(0, dialogW - 4)
  const innerH = Math.max(0, dialogH - 4)
  const contentH = Math.max(0, innerH - 1)

  const maxFieldBoxH = 4
  const minFieldBoxH = 3
  const baseFieldCount = 3
  const fieldBoxH =
    contentH >= maxFieldBoxH * baseFieldCount + 1 ? maxFieldBoxH : minFieldBoxH
  const inputH = Math.max(1, fieldBoxH - 2)

  const colGap = fieldBoxH === minFieldBoxH ? 1 : 2

  // Row 1: Context Window (full width)
  const contextWindowW = innerW

  // Row 2: Max Output Tokens + Temperature side by side
  const maxTokensW = clamp(
    Math.floor(innerW * 0.5),
    16,
    Math.max(16, innerW - (16 + colGap)),
  )
  const temperatureW = Math.max(0, innerW - maxTokensW - colGap)

  const interleavedThinkingH = (() => {
    const full = Math.max(fieldBoxH, 4)
    const compact = fieldBoxH
    const minContentNeeded = fieldBoxH * 2 + full
    return contentH < minContentNeeded ? compact : full
  })()
  const interleavedInputH = Math.max(1, interleavedThinkingH - 2)

  const showHint = contentH >= fieldBoxH * 2 + interleavedThinkingH + 1

  let y = showHint ? 1 : 0

  const samplingY = y
  y += fieldBoxH

  const maxTokensTemperatureY = y
  y += fieldBoxH

  const interleavedThinkingY = y
  y += interleavedThinkingH

  const shortcutsH = Math.max(fieldBoxH, 5)
  let showShortcuts = false
  let shortcutsY = -1
  if (y + shortcutsH <= contentH) {
    showShortcuts = true
    shortcutsY = y
    y += shortcutsH
  }

  const shortcutsHintY = showShortcuts && shortcutsH >= 5 ? 3 : -1

  return {
    dialogW,
    dialogH,
    innerW,
    innerH,
    contentH,
    showHint,
    fieldBoxH,
    inputH,
    colGap,
    contextWindowW,
    maxTokensW,
    temperatureW,
    samplingY,
    maxTokensTemperatureY,
    interleavedThinkingY,
    interleavedThinkingH,
    interleavedInputH,
    showShortcuts,
    shortcutsY,
    shortcutsH,
    shortcutsHintY,
  }
}

/**
 * Toggle logic for the "Interleaved Thinking" multi-select widget.
 *
 * The underlying UI emits either a single index or multiple indices, and it is
 * possible for both "open" and "close" to be selected at once; in that case we
 * keep the old "toggle" behavior for backwards-compatibility.
 */
export function nextInterleavedThinkingValue(
  current: boolean,
  v: number | number[],
): boolean {
  const indices = Array.isArray(v) ? v : [v]
  const hasOpen = indices.includes(0)
  const hasClose = indices.includes(1)
  if (hasOpen && !hasClose) return true
  if (hasClose && !hasOpen) return false
  if (hasOpen && hasClose) return !current
  // In multiple-select mode the UI can emit an empty array when the user presses
  // Space on the currently-selected row (toggling it off). Treat that gesture as
  // a "toggle" for this two-state setting so Space always flips the value.
  return !current
}
