/*!
 * 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.
 */

import { useEffect, useMemo, useRef } from "react";
import { useOdysseyDesignTokens } from "../OdysseyDesignTokensContext.js";
export const convertCamelCaseToKebabCase = string => string.replace(/([A-Z])/g, "-$1").toLowerCase();
export 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));
  });
};
export const useMatchAppElementToUiShellAppArea = ({
  appElement,
  appElementScrollingMode,
  paddingMode,
  uiShellAppAreaRef,
  uiShellResizableRefs
}) => {
  const odysseyDesignTokens = useOdysseyDesignTokens();
  const parentContainerRef = useRef(null);
  const appContainerElementStyles = 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]);
  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
  };
};
//# sourceMappingURL=useMatchAppElementToUiShellAppArea.js.map