import colors from "../../styles/data/colors.module.scss";

/**
 * Utility function to retrieve a named color. By default the `color` parameter
 * is used as the name of a color provided by this library, but if no such
 * color is found the name itself is returned under the assumption that it's
 * actually a valid CSS color itself. This utility is useful for interpreting
 * color props passed to components in this library, allowing the value of
 * a color prop to either be the name of a named color or a fully custom color
 * provided as a hex code or similar.
 *
 * ```js
 *
 * import { getColor } from '@politico/interactive-style';
 *
 * ```
 *
 * @param {string} color
 *   Either the name of a color provided by this library (e.g., `altblack-60`)
 *   or a valid CSS color string (e.g., `red`, `#f66`). Note that if the
 *   `customAllowed` option is set to false then _only_ named colors are
 *   allowed and all other inputs will return `undefined`.
 * @param {Object} options
 *   Additional configuration:
 *   - `customAllowed`: A boolean flag that determines what should happen if no
 *     library color is found with the given name. If this flag is `true` (the
 *     default) then the color name itself gets returned as the color value; if
 *     it is `false` then this utility returns `undefined`.
 * @returns {string?} A color value
 */
const getColor = function getColor(color, options = {}) {
  const {
    customAllowed = true
  } = options;
  if (!color) return undefined;
  const fallback = customAllowed ? color : undefined;
  return colors[color] || fallback;
};
export default getColor;