import { useState, useRef } from 'preact/hooks';

export { showToast, toast } from './command';

export function useToast(durationParams = 2000) {
  const [showToast, setShowToast] = useState<{show: boolean; text?: string}>({
    show: false,
    text: '',
  });
  const toastTimer = useRef<any>(null);

  const onShowToast = (text, duration = durationParams) => {
    setShowToast({ show: true, text });
    clearTimeout(toastTimer.current);
    toastTimer.current = setTimeout(() => {
      setShowToast({ show: false, text: '' });
    }, duration);
  };

  const handleToastClick = () => {
    console.log('handleToastClick');
  };


  return {
    showToast,
    setShowToast,
    onShowToast,
    handleToastClick,
  };
}
