import type { ComponentChildren } from 'preact';
import { Field } from '../core/Field';
import { IconInput, type IconInputProps } from '../core/IconInput';

export type IconFieldProps = Omit<IconInputProps, 'value' | 'onInput' | 'onChange'> & {
  label?: string;
  hint?: string;
  right?: ComponentChildren;
  value?: string;
  fieldClassName?: string;
  inputClassName?: string;
  required?: boolean;
  onValueChange?: (value: string) => void;
};

export function IconField({
  label,
  hint,
  right,
  value,
  fieldClassName,
  inputClassName,
  onValueChange,
  required,
  compact,
  ...props
}: IconFieldProps) {
  return (
    <Field
      label={label}
      hint={hint}
      right={right as any}
      required={Boolean(required)}
      className={fieldClassName}
      compact={compact}
    >
      <IconInput
        {...props}
        required={required}
        aria-required={required ? true : undefined}
        compact={compact}
        value={value ?? ''}
        className={inputClassName}
        onInput={(event: any) => onValueChange?.(String(event?.currentTarget?.value ?? ''))}
        onChange={(event: any) => onValueChange?.(String(event?.currentTarget?.value ?? ''))}
      />
    </Field>
  );
}
