"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useScrollIndication = void 0;
var _react = require("react");
var _OdysseyDesignTokensContext = require("../OdysseyDesignTokensContext.cjs");
/*!
 * 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 useScrollIndication = ({
  tableOuterContainer,
  tableInnerContainer,
  setIsTableContainerScrolledToStart,
  setIsTableContainerScrolledToEnd,
  setTableInnerContainerWidth
}) => {
  const animationFrameIdRef = (0, _react.useRef)(null);
  const resizeObserverRef = (0, _react.useRef)(null);
  const odysseyDesignTokens = (0, _OdysseyDesignTokensContext.useOdysseyDesignTokens)();
  const checkScrollIndicators = (0, _react.useCallback)(() => {
    if (!tableOuterContainer || !tableInnerContainer) return;
    const containerWidth = tableOuterContainer.clientWidth;
    const contentWidth = tableInnerContainer.scrollWidth;
    const containerStartScrollPosition = tableInnerContainer.scrollLeft;
    const containerEndScrollPosition = containerStartScrollPosition + containerWidth;
    setIsTableContainerScrolledToStart(containerStartScrollPosition <= 16);
    setIsTableContainerScrolledToEnd(containerEndScrollPosition >= contentWidth - 16);
  }, [tableInnerContainer, tableOuterContainer, setIsTableContainerScrolledToEnd, setIsTableContainerScrolledToStart]);
  (0, _react.useEffect)(() => {
    if (typeof ResizeObserver === "undefined" || resizeObserverRef.current) {
      return;
    }
    let debounceTimer;
    resizeObserverRef.current = new ResizeObserver(() => {
      clearTimeout(debounceTimer);
      debounceTimer = setTimeout(() => {
        if (!animationFrameIdRef.current) {
          animationFrameIdRef.current = requestAnimationFrame(() => {
            checkScrollIndicators();
            animationFrameIdRef.current = null;
          });
        }
        setTableInnerContainerWidth(tableInnerContainer?.clientWidth ? `${tableInnerContainer.clientWidth - Number(odysseyDesignTokens.BorderWidthMain) * 2}px` : "100%");
      }, 100);
    });
    if (tableOuterContainer && tableOuterContainer.parentElement) {
      resizeObserverRef.current.observe(tableOuterContainer.parentElement);
    }
    return () => {
      if (resizeObserverRef.current) {
        resizeObserverRef.current.disconnect();
        resizeObserverRef.current = null;
      }
      clearTimeout(debounceTimer);
      if (animationFrameIdRef.current) {
        cancelAnimationFrame(animationFrameIdRef.current);
        animationFrameIdRef.current = null;
      }
    };
  }, [checkScrollIndicators, setTableInnerContainerWidth, tableOuterContainer, tableInnerContainer, setIsTableContainerScrolledToStart, setIsTableContainerScrolledToEnd, odysseyDesignTokens.BorderWidthMain]);
  (0, _react.useEffect)(() => {
    tableInnerContainer?.addEventListener("scroll", checkScrollIndicators);
    return () => tableInnerContainer?.removeEventListener("scroll", checkScrollIndicators);
  }, [tableInnerContainer, checkScrollIndicators]);
  (0, _react.useEffect)(() => {
    checkScrollIndicators();
  }, []);
};
exports.useScrollIndication = useScrollIndication;
//# sourceMappingURL=useScrollIndication.cjs.map