import { clamp } from './math'

/**
 * Standardized dialog size presets.
 *
 * All dialogs in the CLI should use one of these presets to ensure
 * consistent sizing across the application.
 *
 * | Preset | Typical use                              |
 * | ------ | ---------------------------------------- |
 * | sm     | Confirmations, simple selects, rename    |
 * | md     | Approvals, tool settings, single forms   |
 * | lg     | Config, sessions, path picker, lists     |
 * | xl     | MCP settings, OAuth, full-featured forms |
 */

export type DialogSizePreset = 'sm' | 'md' | 'lg' | 'xl'

export interface DialogSize {
  w: number
  h: number
}

/**
 * Compute responsive dialog dimensions for a given preset.
 *
 * @param preset  - Size tier (sm / md / lg / xl)
 * @param contentW - Available content width (columns)
 * @param rows     - Available terminal rows
 */
export function dialogSize(
  preset: DialogSizePreset,
  contentW: number,
  rows: number,
): DialogSize {
  switch (preset) {
    case 'sm':
      return {
        w: clamp(Math.floor(contentW * 0.45), 40, 64),
        h: clamp(10, 8, Math.max(8, rows - 8)),
      }
    case 'md':
      return {
        w: clamp(Math.floor(contentW * 0.55), 48, 78),
        h: clamp(14, 11, Math.max(11, rows - 6)),
      }
    case 'lg':
      return {
        w: clamp(Math.floor(contentW * 0.7), 56, 86),
        h: clamp(20, 14, Math.max(14, rows - 4)),
      }
    case 'xl':
      return {
        w: clamp(Math.floor(contentW * 0.78), 60, 96),
        h: clamp(24, 18, Math.max(18, rows - 4)),
      }
  }
}
