// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import { useEffect, useRef } from 'react';
const AUTOCLOSE_TIMEOUT = 6000;
const getAutoCloseTimeout = (autoClosable, autoCloseTimeout) => {
  if (typeof autoClosable !== 'boolean') {
    return autoCloseTimeout;
  }
  return autoClosable ? AUTOCLOSE_TIMEOUT : undefined;
};
export const useAutoClose = _ref => {
  let {
    autoClosable,
    autoCloseTimeout: externalAutoCloseTimeout,
    onClose
  } = _ref;
  const onCloseRef = useRef(onClose);
  onCloseRef.current = onClose;
  useEffect(() => {
    const autoCloseTimeout = getAutoCloseTimeout(autoClosable, externalAutoCloseTimeout);
    let timeoutId;
    if (autoCloseTimeout && onCloseRef.current) {
      timeoutId = window.setTimeout(onCloseRef.current, autoCloseTimeout);
    }
    return () => window.clearTimeout(timeoutId);
  }, [autoClosable, externalAutoCloseTimeout]);
};