"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useMatchAppElementToUiShellAppArea = exports.setStylesToMatchElement = exports.convertCamelCaseToKebabCase = void 0;
var _react = require("react");
var _OdysseyDesignTokensContext = require("../OdysseyDesignTokensContext.cjs");
/*!
 * Copyright (c) 2025-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 convertCamelCaseToKebabCase = string => string.replace(/([A-Z])/g, "-$1").toLowerCase();
exports.convertCamelCaseToKebabCase = convertCamelCaseToKebabCase;
const setStylesToMatchElement = ({
  additionalStyles,
  appContainerElement,
  appContentReferenceElement,
  parentElement
}) => {
  const appContentReferenceRectangle = appContentReferenceElement.getBoundingClientRect();
  const parentElementRectangle = parentElement.getBoundingClientRect();
  appContainerElement.style.setProperty("position", "absolute");
  appContainerElement.style.setProperty("top", `${appContentReferenceRectangle.top - parentElementRectangle.top}px`);
  appContainerElement.style.setProperty("left", `${appContentReferenceRectangle.left - parentElementRectangle.left}px`);
  appContainerElement.style.setProperty("width", `${appContentReferenceRectangle.width}px`);
  appContainerElement.style.setProperty("height", `${appContentReferenceRectangle.height}px`);
  Object.entries(additionalStyles).forEach(([cssPropertyName, cssPropertyValue]) => {
    appContainerElement.style.setProperty(convertCamelCaseToKebabCase(cssPropertyName), String(cssPropertyValue));
  });
};
exports.setStylesToMatchElement = setStylesToMatchElement;
const useMatchAppElementToUiShellAppArea = ({
  appElement,
  appElementScrollingMode,
  paddingMode,
  uiShellAppAreaRef,
  uiShellResizableRefs
}) => {
  const odysseyDesignTokens = (0, _OdysseyDesignTokensContext.useOdysseyDesignTokens)();
  const parentContainerRef = (0, _react.useRef)(null);
  const appContainerElementStyles = (0, _react.useMemo)(() => ({
    boxSizing: "border-box",
    zIndex: 1,
    ...(paddingMode === "comfortable" ? {
      paddingBlock: odysseyDesignTokens.Spacing5,
      paddingInline: odysseyDesignTokens.Spacing8
    } : {}),
    ...(paddingMode === "compact" ? {
      paddingBlock: odysseyDesignTokens.Spacing5,
      paddingInline: odysseyDesignTokens.Spacing5
    } : {}),
    ...(appElementScrollingMode === "horizontal" || appElementScrollingMode === "both" ? {
      overflowX: "auto"
    } : {
      overflowX: "hidden"
    }),
    ...(appElementScrollingMode === "vertical" || appElementScrollingMode === "both" ? {
      overflowY: "auto"
    } : {
      overflowY: "hidden"
    })
  }), [appElementScrollingMode, paddingMode, odysseyDesignTokens]);
  (0, _react.useEffect)(() => {
    if (uiShellAppAreaRef.current && appElement && parentContainerRef.current) {
      let animationFrameId;
      const updateStyles = () => {
        cancelAnimationFrame(animationFrameId);
        animationFrameId = requestAnimationFrame(() => {
          if (uiShellAppAreaRef.current && parentContainerRef.current) {
            setStylesToMatchElement({
              additionalStyles: appContainerElementStyles,
              appContentReferenceElement: uiShellAppAreaRef.current,
              appContainerElement: appElement,
              parentElement: parentContainerRef.current
            });
          }
        });
      };
      const resizingElements = uiShellResizableRefs.map(resizingRef => resizingRef.current).filter(element => Boolean(element));
      const observer = new ResizeObserver(updateStyles);
      resizingElements.forEach(resizingElement => {
        resizingElement.addEventListener("transitionend", updateStyles);
        observer.observe(resizingElement);
      });
      updateStyles();
      return () => {
        observer.disconnect();
        resizingElements.forEach(resizingElement => {
          resizingElement.removeEventListener("transitionend", updateStyles);
        });
      };
    }
    return () => {};
  }, [appElement, appContainerElementStyles, uiShellAppAreaRef, uiShellResizableRefs]);
  return {
    parentContainerRef
  };
};
exports.useMatchAppElementToUiShellAppArea = useMatchAppElementToUiShellAppArea;
//# sourceMappingURL=useMatchAppElementToUiShellAppArea.cjs.map