import PropTypes from "../../../utils/prop-types/index.js";
import { getStateName } from "../../../utils/election/index.js";

/**
 * A component that prints a state's name in the specified format.
 *
 * We currently support four formats: `abbr`, `ap_abbr`, `sentence` and `name`.
 *
 * You can also set the `possessive` prop to `true`
 * to return a possessive version of the state's name.
 *
 * And you can set the  `capitalized` prop to `true` to ensure the first word
 * is capitalized in the `sentence` format.
 */
const StateName = function StateName(props) {
  const {
    stateFips,
    format,
    possessive,
    capitalized
  } = props;
  const name = getStateName(stateFips, {
    format,
    possessive,
    capitalized
  });
  if (!name) {
    return null;
  }
  return <>
      {name}
    </>;
};
StateName.defaultProps = {
  format: 'name',
  possessive: false,
  capitalized: false
};
StateName.propTypes = {
  /**
   * The FIPS code of the state you want to format.
   * If stateFips is undefined, or not a valid FIPS code,
   * the component will return null.
   */
  stateFips: PropTypes.string.isRequired,
  /**
   * The format to use.
   */
  format: PropTypes.oneOf(['abbr', 'ap_abbr', 'name']),
  /**
   * Whether or not the name should be possessive.
   */
  possessive: PropTypes.bool,
  /**
   * Whether or not the first word of the state name should be capitalized. Most names are
   * capitalized by default, but some, like "the Northern Mariana Islands", need a little help
   * when they start a sentence.
   */
  capitalized: PropTypes.bool
};
export default StateName;