import { useContext } from 'react';
import { isPossiblePhoneNumber } from 'libphonenumber-js';
import { useRecoilState, useRecoilValue } from 'recoil';

import { useRecordFieldInput } from '@/object-record/record-field/hooks/useRecordFieldInput';
import { FieldPhoneValue } from '@/object-record/record-field/types/FieldMetadata';
import { isFieldDisplayedAsPhone } from '@/object-record/record-field/types/guards/isFieldDisplayedAsPhone';
import { recordStoreFamilySelector } from '@/object-record/record-store/states/selectors/recordStoreFamilySelector';
import { FieldMetadataType } from '~/generated-metadata/graphql';

import { FieldContext } from '../../contexts/FieldContext';
import { usePersistField } from '../../hooks/usePersistField';
import { assertFieldMetadata } from '../../types/guards/assertFieldMetadata';
import { isFieldPhone } from '../../types/guards/isFieldPhone';

export const usePhoneField = () => {
  const { recordId, fieldDefinition, hotkeyScope } = useContext(FieldContext);

  try {
    // TODO: temporary - remove when 'Phone' field in 'Person' object
    // is migrated to use FieldMetadataType.Phone as type.
    assertFieldMetadata(
      FieldMetadataType.Text,
      isFieldDisplayedAsPhone,
      fieldDefinition,
    );
  } catch {
    assertFieldMetadata(FieldMetadataType.Phone, isFieldPhone, fieldDefinition);
  }

  const fieldName = fieldDefinition.metadata.fieldName;

  const [fieldValue, setFieldValue] = useRecoilState<string>(
    recordStoreFamilySelector({
      recordId,
      fieldName: fieldName,
    }),
  );

  const persistField = usePersistField();

  const persistPhoneField = (newPhoneValue: string) => {
    if (!isPossiblePhoneNumber(newPhoneValue) && newPhoneValue !== '') return;

    persistField(newPhoneValue);
  };
  const { setDraftValue, getDraftValueSelector } =
    useRecordFieldInput<FieldPhoneValue>(`${recordId}-${fieldName}`);

  const draftValue = useRecoilValue(getDraftValueSelector());

  return {
    fieldDefinition,
    fieldValue,
    setFieldValue,
    draftValue,
    setDraftValue,
    hotkeyScope,
    persistPhoneField,
  };
};
