import { t } from '#cli/i18n'

export type DimSdkCommandPaletteCommand = Readonly<{
  label: string
  detail: string
  run: () => void
}>

export function createDimSdkBaseCommands(
  opts: Readonly<{
    openSessionsDialog: () => void
    openTimeline?: () => void
    openTrace?: () => void
    createNewSession: () => void
    openHelp?: () => void
    openConfig: () => void
    openLanguage?: () => void
    openToolApprovals: () => void
    openToolSettings?: () => void
    openMcpSettings?: () => void
    openPlugins?: () => void
    openConnect: () => void
    openModels: () => void
    includeSettings?: boolean
    includeNewSession?: boolean
    includeHelp?: boolean
  }>,
): readonly DimSdkCommandPaletteCommand[] {
  const sessionCommands: DimSdkCommandPaletteCommand[] = []
  const providerCommands: DimSdkCommandPaletteCommand[] = []
  const toolCommands: DimSdkCommandPaletteCommand[] = []
  const preferenceCommands: DimSdkCommandPaletteCommand[] = []
  const helpCommands: DimSdkCommandPaletteCommand[] = []

  sessionCommands.push({
    label: t('commands.sessions.label'),
    detail: t('commands.sessions.detail'),
    run: opts.openSessionsDialog,
  })
  if (opts.openTimeline) {
    sessionCommands.push({
      label: t('commands.timeline.label'),
      detail: t('commands.timeline.detail'),
      run: opts.openTimeline,
    })
  }
  if (opts.openTrace) {
    sessionCommands.push({
      label: t('commands.trace.label'),
      detail: t('commands.trace.detail'),
      run: opts.openTrace,
    })
  }
  if (opts.includeNewSession !== false) {
    sessionCommands.push({
      label: t('commands.newSession.label'),
      detail: t('commands.newSession.detail'),
      run: opts.createNewSession,
    })
  }

  providerCommands.push(
    {
      label: t('commands.connectProvider.label'),
      detail: t('commands.connectProvider.detail'),
      run: opts.openConnect,
    },
    {
      label: t('commands.models.label'),
      detail: t('commands.models.detail'),
      run: opts.openModels,
    },
  )

  toolCommands.push({
    label: t('commands.toolApprovals.label'),
    detail: t('commands.toolApprovals.detail'),
    run: opts.openToolApprovals,
  })
  if (opts.openToolSettings) {
    toolCommands.push({
      label: t('commands.toolSettings.label'),
      detail: t('commands.toolSettings.detail'),
      run: opts.openToolSettings,
    })
  }
  if (opts.openMcpSettings) {
    toolCommands.push({
      label: t('commands.mcpHub.label'),
      detail: t('commands.mcpHub.detail'),
      run: opts.openMcpSettings,
    })
  }
  if (opts.openPlugins) {
    toolCommands.push({
      label: t('commands.plugins.label'),
      detail: t('commands.plugins.detail'),
      run: opts.openPlugins,
    })
  }

  if (opts.includeSettings !== false) {
    preferenceCommands.push({
      label: t('commands.settings.label'),
      detail: t('commands.settings.detail'),
      run: opts.openConfig,
    })
  }
  if (opts.openLanguage) {
    preferenceCommands.push({
      label: t('commands.language.label'),
      detail: t('commands.language.detail'),
      run: opts.openLanguage,
    })
  }

  if (opts.includeHelp !== false && opts.openHelp) {
    helpCommands.push({
      label: t('commands.help.label'),
      detail: t('commands.help.detail'),
      run: opts.openHelp,
    })
  }

  return [
    ...sessionCommands,
    ...providerCommands,
    ...toolCommands,
    ...preferenceCommands,
    ...helpCommands,
  ]
}
