"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isResponsiveObjectValue = exports.getValueForScreenSize = void 0;
var _typeHelpers = require("../utils/type-helpers");
/**
 * Returns the value of a responsive object for the current screen size.
 * @param responsiveValue - The responsive object to get the value from.
 * @param breakpoints - The breakpoints object from the theme.
 * @param dimensions - The dimensions object containing the current scree n width and height.
 * @returns The value of the responsive object for the current screen size.
 */
const getValueForScreenSize = ({
  responsiveValue,
  breakpoints,
  dimensions
}) => {
  // Sort breakpoints by width
  const sortedBreakpoints = Object.entries(breakpoints).sort((valA, valB) => {
    const valAWidth = getWidth(valA[1]);
    const valBWidth = getWidth(valB[1]);
    return valAWidth - valBWidth;
  });
  const {
    width,
    height
  } = dimensions;

  // Find the value for the current screen size
  return sortedBreakpoints.reduce((acc, [key, value]) => {
    if (typeof value === "object") {
      if (width >= value.width && height >= value.height && responsiveValue[key] !== undefined) {
        return responsiveValue[key];
      }
    } else if (width >= value && responsiveValue[key] !== undefined) {
      return responsiveValue[key];
    }
    return acc;
  }, undefined);
};

/**
 * Determines whether a value is a responsive object value.
 * @param val - The value to check.
 * @param theme - The theme object.
 * @returns Whether the value is a responsive object value.
 */
exports.getValueForScreenSize = getValueForScreenSize;
const isResponsiveObjectValue = (val, theme) => {
  if (!val) return false;
  if (typeof val !== "object") return false;
  return (0, _typeHelpers.getKeys)(val).reduce((acc, key) => {
    return acc && theme.breakpoints[key] !== undefined;
  }, true);
};

/**
 * Gets the width of a breakpoint.
 * @param value - The breakpoint to get the width of.
 * @returns The width of the breakpoint.
 */
exports.isResponsiveObjectValue = isResponsiveObjectValue;
function getWidth(value) {
  if (typeof value === "object") {
    return value.width;
  }
  return value;
}
//# sourceMappingURL=responsive-helpers.js.map