"use strict";
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const styles_lib_css_atoms_atoms_cjs = require("../../css/atoms/atoms.cjs");
const sideEffects_lib_components_BraidProvider_BraidProvider_cjs = require("../../../../side-effects/lib/components/BraidProvider/BraidProvider.cjs");
const styles_lib_components_Box_BackgroundContext_cjs = require("./BackgroundContext.cjs");
const styles_lib_css_typography_css_cjs = require("../../css/typography.css.cjs");
const adaptiveBackgrounds = {
  body: "bodyDark",
  surface: "surfaceDark",
  brand: "neutral",
  promoteLight: "neutral",
  infoLight: "neutral",
  positiveLight: "neutral",
  cautionLight: "neutral",
  criticalLight: "neutral",
  neutralLight: "neutral",
  formAccentSoft: "neutral",
  formAccentSoftActive: "neutralActive",
  formAccentSoftHover: "neutralHover"
};
const adaptiveBoxShadow = {
  borderBrandAccent: "borderBrandAccentLight",
  borderBrandAccentLarge: "borderBrandAccentLightLarge",
  borderCaution: "borderCautionLight",
  borderCritical: "borderCriticalLight",
  borderCriticalLarge: "borderCriticalLightLarge",
  borderFormAccent: "borderFormAccentLight",
  borderFormAccentLarge: "borderFormAccentLightLarge",
  borderInfo: "borderInfoLight",
  borderNeutral: "borderNeutralInverted",
  borderNeutralLarge: "borderNeutralInvertedLarge",
  borderNeutralLight: "borderNeutral",
  borderPositive: "borderPositiveLight",
  borderPromote: "borderPromoteLight"
};
const normaliseBackground = (background) => ({
  lightMode: typeof background === "object" ? background.lightMode : background,
  darkMode: typeof background === "object" ? background.darkMode : adaptiveBackgrounds[background] || background
});
const normalisedBoxShadow = (boxShadow) => ({
  lightMode: typeof boxShadow === "object" ? boxShadow.lightMode : boxShadow,
  darkMode: typeof boxShadow === "object" ? boxShadow.darkMode : boxShadow
});
const useColoredBoxClasses = ({
  background,
  boxShadow
}) => {
  const parentLightness = styles_lib_components_Box_BackgroundContext_cjs.useBackgroundLightness();
  const currentBackgroundContext = styles_lib_components_Box_BackgroundContext_cjs.useBackground();
  const { backgroundLightness } = sideEffects_lib_components_BraidProvider_BraidProvider_cjs.useBraidTheme();
  const lightnessMap = {
    ...backgroundLightness,
    customDark: "dark",
    customLight: "light"
  };
  const classList = [];
  if (boxShadow) {
    const normalisedBoxShadows = normalisedBoxShadow(boxShadow);
    const isSemantic = typeof boxShadow === "string";
    classList.push(
      styles_lib_css_atoms_atoms_cjs.atoms({
        boxShadow: {
          lightMode: isSemantic && parentLightness.lightMode === "dark" ? adaptiveBoxShadow[normalisedBoxShadows.lightMode] || normalisedBoxShadows.lightMode : normalisedBoxShadows.lightMode,
          darkMode: isSemantic && parentLightness.darkMode === "dark" ? adaptiveBoxShadow[normalisedBoxShadows.darkMode] || normalisedBoxShadows.darkMode : normalisedBoxShadows.darkMode
        }
      })
    );
  }
  if (background) {
    const { lightMode, darkMode } = normaliseBackground(background);
    classList.push(styles_lib_css_typography_css_cjs.lightModeTone[lightnessMap[lightMode]]);
    classList.push(styles_lib_css_typography_css_cjs.darkModeTone[lightnessMap[darkMode]]);
    const lightModeTone = lightMode === "neutral" ? darkMode : lightMode;
    const darkModeTone = darkMode === "neutral" ? lightMode : darkMode;
    if (lightModeTone && styles_lib_css_typography_css_cjs.lightModeNeutralOverride[lightModeTone]) {
      classList.push(styles_lib_css_typography_css_cjs.lightModeNeutralOverride[lightModeTone]);
    }
    if (darkModeTone && styles_lib_css_typography_css_cjs.darkModeNeutralOverride[darkModeTone]) {
      classList.push(styles_lib_css_typography_css_cjs.darkModeNeutralOverride[darkModeTone]);
    }
    classList.push(
      styles_lib_css_atoms_atoms_cjs.atoms({
        background: {
          lightMode: lightMode === "customDark" || lightMode === "customLight" ? void 0 : lightMode,
          darkMode: darkMode === "customDark" || darkMode === "customLight" ? void 0 : darkMode
        }
      })
    );
    return {
      backgroundContext: {
        lightMode: lightMode || currentBackgroundContext.lightMode,
        darkMode: darkMode || currentBackgroundContext.darkMode
      },
      classList: classList.join(" ")
    };
  }
  return {
    classList: classList.join(" ")
  };
};
const ColoredBox = React.forwardRef(
  ({ component, background, boxShadow, className, ...props }, ref) => {
    const { backgroundContext, classList: colorClasses } = useColoredBoxClasses(
      { background, boxShadow }
    );
    const element = React.createElement(component, {
      className: `${className}${colorClasses ? ` ${colorClasses}` : ""}`,
      ...props,
      ref
    });
    return backgroundContext ? /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Box_BackgroundContext_cjs.BackgroundProvider, { value: backgroundContext, children: element }) : element;
  }
);
ColoredBox.displayName = "ColoredBox";
exports.ColoredBox = ColoredBox;
exports.useColoredBoxClasses = useColoredBoxClasses;
