import { type DialogProps } from '@radix-ui/react-dialog'
import { Command as CommandPrimitive } from 'cmdk'
import type * as React from 'react'
import { SvgSearch } from '../../icons'
import { cn } from '../../utils'
import { Modal as Dialog, ModalContent as DialogContent } from '../Modal'
import { typographyVariants } from '../Typography'

type CommandProps = React.ComponentPropsWithoutRef<typeof CommandPrimitive> & {
  ref?: React.Ref<React.ElementRef<typeof CommandPrimitive>>
}

const Command = ({ className, ref, ...props }: CommandProps) => (
  <CommandPrimitive
    ref={ref}
    className={cn(
      'flex h-full w-full flex-col overflow-hidden rounded bg-background',
      className,
    )}
    {...props}
  />
)
Command.displayName = CommandPrimitive.displayName

const CommandDialog = ({ children, ...props }: DialogProps) => {
  return (
    <Dialog {...props}>
      <DialogContent className="overflow-hidden p-0 shadow-lg">
        <Command className="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-group]]:px-2 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
          {children}
        </Command>
      </DialogContent>
    </Dialog>
  )
}

type CommandInputProps = React.ComponentPropsWithoutRef<
  typeof CommandPrimitive.Input
> & {
  ref?: React.Ref<React.ElementRef<typeof CommandPrimitive.Input>>
}

const CommandInput = ({ className, ref, ...props }: CommandInputProps) => (
  <div
    className="flex items-center border-b border-input-border-active px-3"
    // eslint-disable-next-line react/no-unknown-property
    cmdk-input-wrapper=""
  >
    <SvgSearch className="mr-2 h-4 w-4 shrink-0 text-input-muted-foreground" />
    <CommandPrimitive.Input
      ref={ref}
      className={cn(
        typographyVariants({ variant: 'body-s' }),
        'flex h-11 w-full rounded-md border-none bg-transparent px-0 py-3 text-foreground outline-none placeholder:not-italic placeholder:text-input-muted-foreground focus:border-none focus:shadow-none focus:outline-none focus-visible:shadow-none disabled:cursor-not-allowed disabled:opacity-50',
        className,
      )}
      {...props}
    />
  </div>
)

CommandInput.displayName = CommandPrimitive.Input.displayName

type CommandListProps = React.ComponentPropsWithoutRef<
  typeof CommandPrimitive.List
> & {
  ref?: React.Ref<React.ElementRef<typeof CommandPrimitive.List>>
}

const CommandList = ({ className, ref, ...props }: CommandListProps) => (
  <CommandPrimitive.List
    ref={ref}
    className={cn('max-h-[300px] overflow-y-auto overflow-x-hidden', className)}
    {...props}
  />
)

CommandList.displayName = CommandPrimitive.List.displayName

type CommandEmptyProps = React.ComponentPropsWithoutRef<
  typeof CommandPrimitive.Empty
> & {
  ref?: React.Ref<React.ElementRef<typeof CommandPrimitive.Empty>>
}

const CommandEmpty = ({ className, ref, ...props }: CommandEmptyProps) => (
  <CommandPrimitive.Empty
    ref={ref}
    className={cn(
      typographyVariants({ variant: 'body-s', color: 'muted' }),
      'py-6 text-center',
      className,
    )}
    {...props}
  />
)

CommandEmpty.displayName = CommandPrimitive.Empty.displayName

type CommandGroupProps = React.ComponentPropsWithoutRef<
  typeof CommandPrimitive.Group
> & {
  ref?: React.Ref<React.ElementRef<typeof CommandPrimitive.Group>>
}

const CommandGroup = ({ className, ref, ...props }: CommandGroupProps) => (
  <CommandPrimitive.Group
    ref={ref}
    className={cn(
      'overflow-hidden text-foreground [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1.5 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground',
      className,
    )}
    {...props}
  />
)

CommandGroup.displayName = CommandPrimitive.Group.displayName

type CommandSeparatorProps = React.ComponentPropsWithoutRef<
  typeof CommandPrimitive.Separator
> & {
  ref?: React.Ref<React.ElementRef<typeof CommandPrimitive.Separator>>
}

const CommandSeparator = ({
  className,
  ref,
  ...props
}: CommandSeparatorProps) => (
  <CommandPrimitive.Separator
    ref={ref}
    className={cn('-mx-1 h-px bg-input-border-active', className)}
    {...props}
  />
)
CommandSeparator.displayName = CommandPrimitive.Separator.displayName

type CommandItemProps = React.ComponentPropsWithoutRef<
  typeof CommandPrimitive.Item
> & {
  ref?: React.Ref<React.ElementRef<typeof CommandPrimitive.Item>>
}

const CommandItem = ({ className, ref, ...props }: CommandItemProps) => (
  <CommandPrimitive.Item
    ref={ref}
    className={cn(
      typographyVariants({ variant: 'body-s' }),
      'relative flex w-full cursor-default select-none items-center gap-2 px-4 py-3 text-foreground outline-none',
      'data-[selected=true]:text-accent-foreground data-[selected=true]:bg-accent',
      'data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50',
      '[&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0',
      '[&:not([data-selected=true])_svg]:text-muted-foreground',
      className,
    )}
    {...props}
  />
)

CommandItem.displayName = CommandPrimitive.Item.displayName

const CommandShortcut = ({
  className,
  ...props
}: React.HTMLAttributes<HTMLSpanElement>) => {
  return (
    <span
      className={cn(
        'ml-auto text-xs tracking-widest text-muted-foreground',
        className,
      )}
      {...props}
    />
  )
}
CommandShortcut.displayName = 'CommandShortcut'

export {
  Command,
  CommandDialog,
  CommandInput,
  CommandList,
  CommandEmpty,
  CommandGroup,
  CommandItem,
  CommandShortcut,
  CommandSeparator,
}
