import type {
  DuffelCardFormActions,
  UseDuffelCardFormActionsHook,
} from '../../index';
import React from 'react';

/**
 * This hook abstracts the ref for convenience and readability.
 * Add `ref` to the `DuffelCardForm` `ref` prop and call the functions to trigger the actions you'd like.
 */
export function useDuffelCardFormActions(): UseDuffelCardFormActionsHook {
  const ref = React.useRef<DuffelCardFormActions>(null);

  return {
    ref,
    saveCard: () => {
      if (ref.current) {
        ref.current.saveCard();
      } else {
        console.warn(
          'Attempted to save card, but ref is null.  Have you added the ref prop to the DuffelCardForm component?'
        );
      }
    },
    createCardForTemporaryUse: () => {
      if (ref.current) {
        ref.current.createCardForTemporaryUse();
      } else {
        console.warn(
          'Attempted to create card for temporary use, but ref is null. Have you added the ref prop to the DuffelCardForm component?'
        );
      }
    },
  };
}
