import { type DialogProps } from '@radix-ui/react-dialog'
import { MagnifyingGlassIcon } from '@radix-ui/react-icons'

import { Command as CommandPalettePrimitive } from 'cmdk'
import * as React from 'react'

import { Dialog, DialogContent } from '@components/ui/dialog/dialog'
import { cn } from '@lib/utils'

const CommandPalette = React.forwardRef<
  React.ElementRef<typeof CommandPalettePrimitive>,
  React.ComponentPropsWithoutRef<typeof CommandPalettePrimitive>
>(({ className, ...props }, ref) => (
  <CommandPalettePrimitive
    ref={ref}
    className={cn(
      'flex h-full w-full flex-col overflow-hidden rounded-md bg-popover text-popover-foreground',
      className,
    )}
    {...props}
  />
))
CommandPalette.displayName = CommandPalettePrimitive.displayName

type CommandPaletteDialogProps = DialogProps

const CommandPaletteDialog = ({ children, ...props }: CommandPaletteDialogProps) => {
  return (
    <Dialog {...props}>
      <DialogContent className="overflow-hidden p-0">
        <CommandPalette 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}
        </CommandPalette>
      </DialogContent>
    </Dialog>
  )
}

const CommandPaletteInput = React.forwardRef<
  React.ElementRef<typeof CommandPalettePrimitive.Input>,
  React.ComponentPropsWithoutRef<typeof CommandPalettePrimitive.Input>
>(({ className, ...props }, ref) => (
  // eslint-disable-next-line react/no-unknown-property
  <div className="flex items-center border-b px-3" cmdk-input-wrapper="">
    <MagnifyingGlassIcon className="mr-2 h-4 w-4 shrink-0 opacity-50" />
    <CommandPalettePrimitive.Input
      ref={ref}
      className={cn(
        'flex h-10 w-full rounded-md bg-transparent py-3 text-sm outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50',
        className,
      )}
      {...props}
    />
  </div>
))

CommandPaletteInput.displayName = CommandPalettePrimitive.Input.displayName

const CommandPaletteList = React.forwardRef<
  React.ElementRef<typeof CommandPalettePrimitive.List>,
  React.ComponentPropsWithoutRef<typeof CommandPalettePrimitive.List>
>(({ className, ...props }, ref) => (
  <CommandPalettePrimitive.List
    ref={ref}
    className={cn('max-h-[300px] overflow-y-auto overflow-x-hidden', className)}
    {...props}
  />
))

CommandPaletteList.displayName = CommandPalettePrimitive.List.displayName

const CommandPaletteEmpty = React.forwardRef<
  React.ElementRef<typeof CommandPalettePrimitive.Empty>,
  React.ComponentPropsWithoutRef<typeof CommandPalettePrimitive.Empty>
>((props, ref) => <CommandPalettePrimitive.Empty ref={ref} className="py-6 text-center text-sm" {...props} />)

CommandPaletteEmpty.displayName = CommandPalettePrimitive.Empty.displayName

const CommandPaletteGroup = React.forwardRef<
  React.ElementRef<typeof CommandPalettePrimitive.Group>,
  React.ComponentPropsWithoutRef<typeof CommandPalettePrimitive.Group>
>(({ className, ...props }, ref) => (
  <CommandPalettePrimitive.Group
    ref={ref}
    className={cn(
      'overflow-hidden p-1 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}
  />
))

CommandPaletteGroup.displayName = CommandPalettePrimitive.Group.displayName

const CommandPaletteSeparator = React.forwardRef<
  React.ElementRef<typeof CommandPalettePrimitive.Separator>,
  React.ComponentPropsWithoutRef<typeof CommandPalettePrimitive.Separator>
>(({ className, ...props }, ref) => (
  <CommandPalettePrimitive.Separator ref={ref} className={cn('-mx-1 h-px bg-border', className)} {...props} />
))
CommandPaletteSeparator.displayName = CommandPalettePrimitive.Separator.displayName

const CommandPaletteItem = React.forwardRef<
  React.ElementRef<typeof CommandPalettePrimitive.Item>,
  React.ComponentPropsWithoutRef<typeof CommandPalettePrimitive.Item>
>(({ className, ...props }, ref) => (
  <CommandPalettePrimitive.Item
    ref={ref}
    className={cn(
      'relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none data-[disabled=true]:pointer-events-none data-[selected=true]:bg-accent data-[selected=true]:text-accent-foreground data-[disabled=true]:opacity-50',
      className,
    )}
    {...props}
  />
))

CommandPaletteItem.displayName = CommandPalettePrimitive.Item.displayName

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

export {
  CommandPalette,
  CommandPaletteDialog,
  CommandPaletteInput,
  CommandPaletteList,
  CommandPaletteEmpty,
  CommandPaletteGroup,
  CommandPaletteItem,
  CommandPaletteShortcut,
  CommandPaletteSeparator,
}
