import React from 'react';
import PropTypes from 'prop-types';
import URI from 'urijs';
import { prop } from 'ramda';

import DefaultPasswordRequestForm from './PasswordRequestForm';
import DefaultPasswordResponseForm from './PasswordResponseForm';

/* Interface */
const propTypes = {
  /** URL search parameters e.g. `?field=value`. */
  search: PropTypes.string.isRequired,

  /** Event handler called when reset is successful. */
  onPasswordReset: PropTypes.func.isRequired,
};

/**
 * Component for password reset
 * Uses URL search parameters to identify whether reset is already in progress
 * @param {Object} props - React component props.
 * @returns {Object} React element.
 */
const PasswordResetForm = ({ search, onPasswordReset }) => {
  const token = prop('token', URI(search).search(true));

  return (
    <>
      { token
        ? (
          <DefaultPasswordResponseForm
            token={ token }
            onPasswordReset={ onPasswordReset }
          />
        )
        : <DefaultPasswordRequestForm />
      }
    </>
  );
};

PasswordResetForm.propTypes = propTypes;

export default PasswordResetForm;
