'use client'

import { type ComponentProps, useState } from 'react'
import { LockKeyholeIcon, EyeOffIcon, EyeIcon } from 'lucide-react'
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from './ui/input-group'

interface PasswordInputProps {
  withIcon?: boolean
}

export const PasswordInput = ({ withIcon, ...props }: PasswordInputProps & Omit<ComponentProps<'input'>, 'type'>) => {
  const [show, setShow] = useState(false)

  return (
    <InputGroup>
      <InputGroupInput {...props} type={show ? 'text' : 'password'} />
      {withIcon && (
        <InputGroupAddon>
          <LockKeyholeIcon />
        </InputGroupAddon>
      )}
      <InputGroupAddon align='inline-end'>
        <InputGroupButton onClick={() => setShow(prev => !prev)}>
          {show ? <EyeOffIcon /> : <EyeIcon />}
        </InputGroupButton>
      </InputGroupAddon>
    </InputGroup>
  )
}