"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useColorScheme = void 0;
var _react = require("react");
var _typeHelpers = require("../theme/utils/type-helpers");
var _useTheme = require("./useTheme");
var _utils = require("./utils/utils");
/**
 * Recursively replaces color values in an object with a new value.
 * @param newValue The new value to replace the color values with.
 * @param obj The object to replace the color values in.
 * @returns The updated object with the new color values.
 */
const replaceColorValuesInObject = (newValue, obj) => {
  return Object.entries(obj).reduce((updatedObj, [key, value]) => {
    if (typeof value === "object" && !(0, _typeHelpers.getKeys)(obj).includes("$$typeof")) {
      updatedObj[key] = replaceColorValuesInObject(newValue, value);
    } else if (typeof value === "string" && value.includes("primary")) {
      updatedObj[key] = value.replace("primary", newValue);
    } else {
      updatedObj[key] = value;
    }
    return updatedObj;
  }, {});
};

/**
 * Hook to convert an existing style props object to a different color scheme from the active theme palette.
 * @param targetColorScheme Name of the target color scheme.
 * @param props Style props object.
 * @returns The updated style props object with the new color scheme.
 */
const useColorScheme = (targetColorScheme, props) => {
  // If no target color scheme is provided, return the original props object.
  if (!targetColorScheme) return props;

  // Get the active theme from the useTheme hook.
  const {
    theme
  } = (0, _useTheme.useTheme)();

  // Check if the target color scheme is available in the theme palette.
  (0, _utils.checkKeyAvailability)(targetColorScheme, theme.palette, "theme.palette");

  // Replace the color values in the props object with the new target color scheme.
  // Use a memoized version of replaceColorValuesInObject to avoid unnecessary computations.
  const memoizedReplaceColorValuesInObject = (0, _react.useMemo)(() => replaceColorValuesInObject(targetColorScheme, props), [targetColorScheme, props]);
  return memoizedReplaceColorValuesInObject;
};
exports.useColorScheme = useColorScheme;
//# sourceMappingURL=useColorScheme.js.map