"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useContrastModeContext = exports.useContrastMode = exports.normalizeRgbaToRgb = exports.normalizeBackgroundColor = exports.isTransparentColor = exports.hueNeutral50Rgb = exports.getElementComputedBackgroundColor = exports.getBackgroundColor = exports.defaultParentBackgroundColor = exports.defaultContrast = exports.ContrastModeContext = void 0;
var Tokens = _interopRequireWildcard(require("@okta/odyssey-design-tokens"));
var _react = require("react");
var _hexToRgb = require("./hexToRgb.cjs");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/*!
 * Copyright (c) 2023-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

const ContrastModeContext = exports.ContrastModeContext = (0, _react.createContext)({
  contrastMode: "lowContrast"
});
const defaultContrast = exports.defaultContrast = "lowContrast";
const useContrastModeContext = () => (0, _react.useContext)(ContrastModeContext);
exports.useContrastModeContext = useContrastModeContext;
const hueNeutral50Rgb = exports.hueNeutral50Rgb = (0, _hexToRgb.hexToRgb)(Tokens.HueNeutral50).asFormattedString;
const isTransparentColor = color => color === "rgba(0, 0, 0, 0)" || color === "transparent";
exports.isTransparentColor = isTransparentColor;
const normalizeRgbaToRgb = rgba => rgba.replace(/rgba\((\d+),\s*(\d+),\s*(\d+),\s*[\d.]+\)/, "rgb($1, $2, $3)");
exports.normalizeRgbaToRgb = normalizeRgbaToRgb;
const getElementComputedBackgroundColor = element => window.getComputedStyle(element).backgroundColor;
exports.getElementComputedBackgroundColor = getElementComputedBackgroundColor;
const normalizeBackgroundColor = bgColor => {
  if (/rgba\((\d+),\s*(\d+),\s*(\d+),\s*[\d.]+\)/.test(bgColor)) {
    const normalizedColor = normalizeRgbaToRgb(bgColor);
    return normalizedColor === hueNeutral50Rgb ? Tokens.HueNeutral50 : normalizedColor;
  }
  return bgColor === hueNeutral50Rgb ? Tokens.HueNeutral50 : bgColor;
};
exports.normalizeBackgroundColor = normalizeBackgroundColor;
const defaultParentBackgroundColor = exports.defaultParentBackgroundColor = "#ffffff";
const getBackgroundColor = element => {
  while (element) {
    const bgColor = getElementComputedBackgroundColor(element);
    if (!isTransparentColor(bgColor)) {
      return normalizeBackgroundColor(bgColor);
    }
    element = element.parentElement;
  }
  return defaultParentBackgroundColor;
};
exports.getBackgroundColor = getBackgroundColor;
const useContrastMode = ({
  contrastMode: explicitContrastMode
}) => {
  const contrastContainerRef = (0, _react.useRef)(null);
  const {
    contrastMode: existingContrastMode
  } = useContrastModeContext();
  const [parentBackgroundColor, setParentBackgroundColor] = (0, _react.useState)(defaultParentBackgroundColor);
  const [contrastMode, setContrastMode] = (0, _react.useState)(() => explicitContrastMode || existingContrastMode);
  const updateBackgroundColor = (0, _react.useCallback)(() => {
    const newBgColor = getBackgroundColor(contrastContainerRef.current);
    setParentBackgroundColor(newBgColor);
    if (!explicitContrastMode) {
      setContrastMode(newBgColor === Tokens.HueNeutral50 ? "highContrast" : "lowContrast");
    }
  }, [explicitContrastMode]);
  (0, _react.useEffect)(() => {
    const observer = new MutationObserver(updateBackgroundColor);
    observer.observe(document.querySelector("html"), {
      attributes: true,
      attributeFilter: ["class", "style"]
    });
    observer.observe(document.head, {
      childList: true,
      subtree: true
    });
    const onTransitionEnd = event => {
      if (event.propertyName === "background-color") {
        updateBackgroundColor();
      }
    };
    document.addEventListener("transitionend", onTransitionEnd);
    updateBackgroundColor();
    return () => {
      document.removeEventListener("transitionend", onTransitionEnd);
      observer.disconnect();
    };
  }, [updateBackgroundColor]);
  return {
    contrastContainerRef,
    contrastMode,
    parentBackgroundColor
  };
};
exports.useContrastMode = useContrastMode;
//# sourceMappingURL=useContrastMode.cjs.map