import { jsx } from "react/jsx-runtime";
import { forwardRef, createElement } from "react";
import { atoms } from "../../css/atoms/atoms.mjs";
import { useBraidTheme } from "../../../../side-effects/lib/components/BraidProvider/BraidProvider.mjs";
import { BackgroundProvider, useBackgroundLightness, useBackground } from "./BackgroundContext.mjs";
import { lightModeTone, darkModeTone, lightModeNeutralOverride, darkModeNeutralOverride } from "../../css/typography.css.mjs";
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 = useBackgroundLightness();
  const currentBackgroundContext = useBackground();
  const { backgroundLightness } = useBraidTheme();
  const lightnessMap = {
    ...backgroundLightness,
    customDark: "dark",
    customLight: "light"
  };
  const classList = [];
  if (boxShadow) {
    const normalisedBoxShadows = normalisedBoxShadow(boxShadow);
    const isSemantic = typeof boxShadow === "string";
    classList.push(
      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(lightModeTone[lightnessMap[lightMode]]);
    classList.push(darkModeTone[lightnessMap[darkMode]]);
    const lightModeTone$1 = lightMode === "neutral" ? darkMode : lightMode;
    const darkModeTone$1 = darkMode === "neutral" ? lightMode : darkMode;
    if (lightModeTone$1 && lightModeNeutralOverride[lightModeTone$1]) {
      classList.push(lightModeNeutralOverride[lightModeTone$1]);
    }
    if (darkModeTone$1 && darkModeNeutralOverride[darkModeTone$1]) {
      classList.push(darkModeNeutralOverride[darkModeTone$1]);
    }
    classList.push(
      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 = forwardRef(
  ({ component, background, boxShadow, className, ...props }, ref) => {
    const { backgroundContext, classList: colorClasses } = useColoredBoxClasses(
      { background, boxShadow }
    );
    const element = createElement(component, {
      className: `${className}${colorClasses ? ` ${colorClasses}` : ""}`,
      ...props,
      ref
    });
    return backgroundContext ? /* @__PURE__ */ jsx(BackgroundProvider, { value: backgroundContext, children: element }) : element;
  }
);
ColoredBox.displayName = "ColoredBox";
export {
  ColoredBox,
  useColoredBoxClasses
};
