'use client';

import { Eye, EyeOff, Lock } from 'lucide-react';
import { useState } from 'react';

import { Input, type InputProps } from '@/components/ui/input';
import { Tooltip } from '@/components/ui/tooltip';

export function PasswordInput(props: Omit<InputProps, 'type' | 'leading' | 'trailing'>) {
  const [visible, setVisible] = useState(false);
  const label = visible ? 'Hide password' : 'Show password';
  return (
    <Input
      {...props}
      type={visible ? 'text' : 'password'}
      leading={<Lock />}
      trailing={
        <Tooltip label={label}>
          <button
            type="button"
            className="grid h-9 w-9 place-items-center rounded-md text-subtle-foreground transition-colors hover:bg-surface-muted hover:text-foreground"
            aria-pressed={visible}
            aria-label={label}
            onClick={() => setVisible((current) => !current)}
          >
            {visible ? <EyeOff className="h-[18px] w-[18px]" aria-hidden="true" /> : <Eye className="h-[18px] w-[18px]" aria-hidden="true" />}
          </button>
        </Tooltip>
      }
    />
  );
}
