import * as React from 'react'
import * as RSelect from '@radix-ui/react-select'
import { cn, surface } from './cva'

export const Select = RSelect.Root
export const SelectValue = RSelect.Value
export const SelectGroup = RSelect.Group
export const SelectLabel = RSelect.Label
export const SelectSeparator = RSelect.Separator

function ChevronDownIcon() {
  return (
    <svg className="h-4 w-4" viewBox="0 0 16 16" fill="none" aria-hidden>
      <path
        d="M4 6L8 10L12 6"
        stroke="currentColor"
        strokeWidth="2"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  )
}

function ChevronUpIcon() {
  return (
    <svg className="h-4 w-4" viewBox="0 0 16 16" fill="none" aria-hidden>
      <path
        d="M12 10L8 6L4 10"
        stroke="currentColor"
        strokeWidth="2"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  )
}

export const SelectTrigger = React.forwardRef<
  React.ElementRef<typeof RSelect.Trigger>,
  React.ComponentPropsWithoutRef<typeof RSelect.Trigger>
>(({ className, children, ...props }, ref) => (
  <RSelect.Trigger
    ref={ref}
    className={cn(
      surface({ tone: 'surface', radius: 'md' }),
      'flex min-h-[var(--mod-touch-min)] items-center justify-between gap-2 border border-border px-3 py-2 focus-visible:outline-[var(--mod-focus-ring-width)] focus-visible:outline-[var(--mod-focus-ring-color)] focus-visible:outline-offset-[var(--mod-focus-ring-offset)] disabled:cursor-not-allowed disabled:opacity-[var(--mod-opacity-disabled)]',
      className,
    )}
    {...props}
  >
    {children}
    <RSelect.Icon className="text-fg-muted" aria-hidden>
      <ChevronDownIcon />
    </RSelect.Icon>
  </RSelect.Trigger>
))
SelectTrigger.displayName = 'SelectTrigger'

export const SelectScrollUpButton = React.forwardRef<
  React.ElementRef<typeof RSelect.ScrollUpButton>,
  React.ComponentPropsWithoutRef<typeof RSelect.ScrollUpButton>
>(({ className, ...props }, ref) => (
  <RSelect.ScrollUpButton
    ref={ref}
    className={cn('flex cursor-default items-center justify-center py-1 text-fg-muted', className)}
    {...props}
  >
    <ChevronUpIcon />
  </RSelect.ScrollUpButton>
))
SelectScrollUpButton.displayName = 'SelectScrollUpButton'

export const SelectScrollDownButton = React.forwardRef<
  React.ElementRef<typeof RSelect.ScrollDownButton>,
  React.ComponentPropsWithoutRef<typeof RSelect.ScrollDownButton>
>(({ className, ...props }, ref) => (
  <RSelect.ScrollDownButton
    ref={ref}
    className={cn('flex cursor-default items-center justify-center py-1 text-fg-muted', className)}
    {...props}
  >
    <ChevronDownIcon />
  </RSelect.ScrollDownButton>
))
SelectScrollDownButton.displayName = 'SelectScrollDownButton'

export const SelectContent = React.forwardRef<
  React.ElementRef<typeof RSelect.Content>,
  React.ComponentPropsWithoutRef<typeof RSelect.Content>
>(({ className, children, position = 'popper', ...props }, ref) => (
  <RSelect.Portal>
    <RSelect.Content
      ref={ref}
      position={position}
      className={cn(
        surface({ tone: 'surface', radius: 'lg' }),
        'relative z-50 max-h-96 min-w-32 overflow-hidden border border-border shadow-[var(--mod-shadow-md)]',
        className,
      )}
      {...props}
    >
      <SelectScrollUpButton />
      <RSelect.Viewport className="p-1">{children}</RSelect.Viewport>
      <SelectScrollDownButton />
    </RSelect.Content>
  </RSelect.Portal>
))
SelectContent.displayName = 'SelectContent'

export const SelectItem = React.forwardRef<
  React.ElementRef<typeof RSelect.Item>,
  React.ComponentPropsWithoutRef<typeof RSelect.Item>
>(({ className, children, ...props }, ref) => (
  <RSelect.Item
    ref={ref}
    className={cn(
      'relative flex min-h-[var(--mod-touch-min)] w-full cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm text-[var(--mod-color-accent-11)] outline-none focus:bg-[var(--mod-color-accent-4)] active:bg-[var(--mod-color-accent-5)] data-[disabled]:opacity-[var(--mod-opacity-disabled)]',
      className,
    )}
    {...props}
  >
    <span className="absolute left-2 inline-flex items-center justify-center">
      <RSelect.ItemIndicator aria-hidden>✓</RSelect.ItemIndicator>
    </span>
    <RSelect.ItemText>{children}</RSelect.ItemText>
  </RSelect.Item>
))
SelectItem.displayName = 'SelectItem'
