/**
 * PhoneInput widget component with formatting.
 *
 * ⚠️ AUTO-GENERATED BY PRISM - DO NOT EDIT
 */

import type { ChangeEvent } from 'react';
import type { JSX } from 'react';
import type { WidgetBaseProps } from '../types';

export function PhoneInput({
  name,
  value,
  onChange,
  label,
  description,
  placeholder = '+1 (555) 123-4567',
  required,
  disabled,
  error,
  className,
}: WidgetBaseProps<string>): JSX.Element {
  const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
    // Allow only digits, spaces, parentheses, dashes, and plus sign
    const cleaned = e.target.value.replace(/[^\d\s()\-+]/g, '');
    onChange(cleaned);
  };

  return (
    <div className={className}>
      {label && (
        <label htmlFor={name} className="label">
          {label}
          {required && <span className="text-red-500 ml-1">*</span>}
        </label>
      )}
      <input
        id={name}
        type="tel"
        name={name}
        value={value ?? ''}
        onChange={handleChange}
        placeholder={placeholder}
        disabled={disabled}
        required={required}
        className={error ? 'input-error' : 'input'}
        aria-invalid={!!error}
        aria-describedby={description ? `${name}-desc` : error ? `${name}-error` : undefined}
        autoComplete="tel"
      />
      {description && !error && (
        <p id={`${name}-desc`} className="mt-1.5 text-sm text-nordic-500">{description}</p>
      )}
      {error && (
        <p id={`${name}-error`} className="mt-1.5 text-sm text-red-600">
          {error}
        </p>
      )}
    </div>
  );
}
