import { breakpointNames } from "../../../components/application/ContainerBreakpoint/breakpoints.js";
import useContainerBreakpoint from '../useContainerBreakpoint';

/**
 * Resolve a responsive value map based on the current _container_ breakpoint
 * measurement. This works exactly like `useResponsiveValues`, except it
 * resolves the value map based on the _container_ breakpoint. That means it
 * requires a `ContainerBreakpoint` container to function properly.
 *
 * @param {Object|any} values
 *   Either a plain value to use as-is at all breakpoints or a responsive value
 *   map.
 */
const useContainerResponsiveValues = function useContainerResponsiveValues(values) {
  const currentBreakpoint = useContainerBreakpoint();
  if (typeof values !== 'object') return values;
  if (breakpointNames.every(name => typeof values[name] === 'undefined')) {
    return values;
  }
  const normalizedValues = breakpointNames.reduce((breakpoints, name, i) => {
    const lastName = breakpointNames[i - 1];
    const value = typeof values[name] === 'undefined' ? breakpoints.get(lastName) : values[name];
    breakpoints.set(name, value);
    return breakpoints;
  }, new Map());
  return normalizedValues.get(currentBreakpoint);
};
export default useContainerResponsiveValues;