import { Badge, Group, HoverCard, SelectProps, Typography } from '@tidbcloud/uikit'
import { IconAlertCircle, IconCheck } from '@tidbcloud/uikit/icons'

import { CloudProviderConst } from 'dbaas/features/Project'

import { useCmekList } from './helper'

export const AzureAvailableCmekRegions = ['southeastasia']

export const CmekRegionRenderOption: SelectProps['renderOption'] = ({ option, checked }) => {
  const { data } = useCmekList()
  const cmekCreated = data?.cmeks?.some((item) => {
    const providerNameIdx = item.regionId.indexOf('-')
    return item.regionId.substring(providerNameIdx > -1 ? providerNameIdx + 1 : 0) === `${option.value}`
  })
  // @ts-ignore
  const unavailable = option.provider === CloudProviderConst.AZURE && !AzureAvailableCmekRegions.includes(option.value)

  return (
    <HoverCard disabled={!unavailable} width={400} position="right" withArrow>
      <HoverCard.Target>
        <Group justify="space-between" w="100%">
          <Group>
            <Typography variant="body-lg">{option.label}</Typography>
            {!unavailable && cmekCreated && (
              <Badge color="carbon.6" variant="outline">
                CMEK Created
              </Badge>
            )}
            {unavailable && (
              <Badge color="carbon.6" variant="outline">
                <Group gap={4} align="center">
                  unavailable
                  <IconAlertCircle size={14} />
                </Group>
              </Badge>
            )}
          </Group>
          {checked && <IconCheck size={16} strokeWidth={3} style={{ marginInlineStart: 'auto' }} />}
        </Group>
      </HoverCard.Target>
      <HoverCard.Dropdown>
        The cross-tenant customer-managed key (CMK) feature for Azure managed disks is currently in preview and only
        available in select Azure regions. Currently, only availability regions are supported. Learn more about{' '}
        <a href="https://learn.microsoft.com/en-us/azure/virtual-machines/disks-cross-tenant-customer-managed-keys?tabs=azure-portal#preview-regional-availability">
          Encrypt managed disks with cross-tenant customer-managed keys{' '}
        </a>
        .
      </HoverCard.Dropdown>
    </HoverCard>
  )
}
