/**
 * Exit-app confirmation dialog.
 *
 * Triggered by Ctrl+C / SIGINT when the current session is busy.
 */

import { t } from '#cli/i18n'
import { TDialog, TText } from '@simon_he/vue-tui'
import { defineComponent, h } from 'vue'
import { useUiTheme } from '../theme'

export const DimSdkExitAppDialog = defineComponent({
  name: 'DimSdkExitAppDialog',
  props: {
    modelValue: { type: Boolean, required: true },
    reason: { type: String, default: '' },
    w: { type: Number, default: 56 },
    h: { type: Number, default: 11 },
  },
  emits: {
    'update:modelValue': (_v: boolean) => true,
    exit: () => true,
  },
  setup(props, { emit }) {
    const ui = useUiTheme()
    const dialogZ = 1600

    return () => {
      if (!props.modelValue) return null

      const dialogW = Math.max(40, Math.floor(props.w))
      const dialogH = Math.max(8, Math.floor(props.h))
      const innerW = Math.max(0, dialogW - 4)
      const innerH = Math.max(0, dialogH - 4)
      const bodyStyle = ui.style(['dialog.chrome'])
      const titleStyle = ui.style(['dialog.chrome', 'status.warning'])
      const textStyle = ui.style(['dialog.chrome', 'text.primary'])
      const mutedStyle = ui.style(['dialog.chrome', 'text.muted'])

      const reason = String(props.reason ?? '').trim()
      return h(
        TDialog as any,
        {
          modelValue: props.modelValue,
          'onUpdate:modelValue': (v: boolean) => emit('update:modelValue', v),
          onKeydownCapture: (e: any) => {
            if (e?.key === 'Escape') {
              e.preventDefault?.()
              e.stopPropagation?.()
              emit('update:modelValue', false)
              return
            }
            if (
              typeof e?.key === 'string' &&
              e.key.length === 1 &&
              !e.ctrlKey &&
              !e.metaKey &&
              !e.altKey
            ) {
              e.preventDefault?.()
              e.stopPropagation?.()
            }
          },
          w: dialogW,
          h: dialogH,
          title: t('exitAppDialog.title'),
          placement: 'center',
          teleport: true,
          zIndex: dialogZ,
          backdrop: false,
          padding: 1,
          style: ui.style(['dialog.chrome']),
          buttons: [
            { label: t('common.cancel'), value: 'cancel' },
            {
              label: t('common.exit'),
              value: 'exit',
              kind: 'primary',
              default: true,
            },
          ],
          onConfirm: (btn: any) => {
            if (btn?.value === 'exit') emit('exit')
            emit('update:modelValue', false)
          },
          onClose: () => emit('update:modelValue', false),
        },
        () => [
          h(TText, {
            x: 0,
            y: 0,
            w: innerW,
            h: innerH,
            value: '',
            style: bodyStyle,
          }),
          h(TText, {
            x: 0,
            y: 0,
            w: innerW,
            wrap: true,
            value: t('exitAppDialog.confirm'),
            style: titleStyle,
          }),
          h(TText, {
            x: 0,
            y: 2,
            w: innerW,
            wrap: true,
            value: t('exitAppDialog.warning'),
            style: mutedStyle,
          }),
          reason
            ? h(TText, {
                x: 0,
                y: 4,
                w: innerW,
                wrap: true,
                value: t('exitAppDialog.reasonPrefix', { reason }),
                style: textStyle,
              })
            : null,
        ],
      )
    }
  },
})
