import { FormField } from '../../form/FormField/FormField';
import { PhoneField, PhoneFieldProps } from '../../form/PhoneField/PhoneField';
import { assertEmptyObject } from '../../utils/assertEmptyObject';
import { makeTestId } from '../../utils/makeTestId';
import { CommonFormikProps } from '../common/types';
import { useFormikFieldsProps } from '../hooks/useFormikFieldsProps';

export interface FormikPhoneFieldProps
  extends CommonFormikProps<string>,
    Pick<PhoneFieldProps, 'placeholder'> {}

/**
 * Formik field that allows changing phone.
 *
 * ```tsx
 * import { FormikPhoneField } from 'ui-kit';
 *
 * <FormikPhoneField name="phone" label="Phone" />
 * ```
 */
export function FormikPhoneField(props: FormikPhoneFieldProps) {
  const { id, formFieldProps, controlProps } = useFormikFieldsProps<PhoneFieldProps>(props);

  const {
    className,
    descriptionMessageId,
    description,
    errorMessageId,
    error,
    label,
    requirement,
    onHintClick,
    hintText,
    testId,
    ...restFormFieldProps
  } = formFieldProps;
  assertEmptyObject(restFormFieldProps);

  const {
    value,
    onChange,
    onBlur,
    disabled,
    ariaLabel,
    ariaInvalid,
    ariaDescribedBy,
    ariaErrorMessage,
    placeholder,
    required,
    ariaRequired,
    ...restControlProps
  } = controlProps;
  assertEmptyObject(restControlProps);

  return (
    <FormField
      className={className}
      description={description}
      descriptionMessageId={descriptionMessageId}
      error={error}
      errorMessageId={errorMessageId}
      hintText={hintText}
      id={id}
      label={label}
      onHintClick={onHintClick}
      requirement={requirement}
      testId={testId}
    >
      <PhoneField
        ariaDescribedBy={ariaDescribedBy}
        ariaErrorMessage={ariaErrorMessage}
        ariaInvalid={ariaInvalid}
        ariaLabel={ariaLabel}
        ariaRequired={ariaRequired}
        disabled={disabled}
        id={id}
        onBlur={onBlur}
        onChange={onChange}
        placeholder={placeholder}
        required={required}
        testId={makeTestId(testId, 'phone-field')}
        value={value}
      />
    </FormField>
  );
}
