"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useFitViewWithDelay = void 0;
var _react = require("react");
var _react2 = require("@xyflow/react");
/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * Custom hook that provides a function to fit the view with a delay
 * Useful for ensuring smooth transitions when changing views
 *
 * @param delay - Delay in milliseconds before fitting the view (default: 100ms)
 * @param padding - Padding for the fit view operation (default: 0.15)
 * @param duration - Duration of the fit view animation in milliseconds (default: 400ms)
 * @returns Function to trigger the delayed fit view
 */
const useFitViewWithDelay = (delay = 100, padding = 0.15, duration = 400) => {
  // Ref to store timeout ID for delayed view fitting
  const timeoutRef = (0, _react.useRef)();
  const reactFlowInstance = (0, _react2.useReactFlow)();

  /**
   * Fits the view to show all nodes with a slight delay
   * Used to ensure smooth transitions when changing views
   */
  const fitViewWithDelay = (0, _react.useCallback)(() => {
    if (timeoutRef.current) {
      clearTimeout(timeoutRef.current);
    }
    timeoutRef.current = setTimeout(() => {
      if (reactFlowInstance !== null && reactFlowInstance !== void 0 && reactFlowInstance.fitView) {
        // We will not auto zoom beyond 100% (maxZoom: 1) and below 60% (minZoom: 0.6)
        reactFlowInstance.fitView({
          minZoom: 0.6,
          maxZoom: 1,
          padding,
          duration
        });
      }
    });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [reactFlowInstance, delay, padding, duration]);

  // Cleanup timeout on unmount to prevent memory leaks
  (0, _react.useEffect)(() => {
    return () => {
      if (timeoutRef.current) {
        clearTimeout(timeoutRef.current);
      }
    };
  }, []);
  return fitViewWithDelay;
};
exports.useFitViewWithDelay = useFitViewWithDelay;