import { isUndefined } from '@transferwise/neptune-validation';
import { useEffect } from 'react';

const useConditionalListener = ({
  attachListener,
  callback,
  eventType,
  parent
}) => {
  useEffect(() => {
    if (attachListener && !isUndefined(parent)) {
      parent.addEventListener(eventType, callback, true);
    }
    return () => {
      if (!isUndefined(parent)) {
        parent.removeEventListener(eventType, callback, true);
      }
    };
  }, [attachListener, callback, eventType, parent]);
};

export { useConditionalListener };
//# sourceMappingURL=useConditionalListener.mjs.map
