export default function useTimeout() {
  const times: Record<string, NodeJS.Timeout[]> = {};

  function addSetTimeout(key: string, action: () => void, time: number) {
    if (!times[key]) times[key] = [] as NodeJS.Timeout[];
    times[key].push(setTimeout(action, time));
  }

  function clearSetTimeout(key: string) {
    if (!times[key]) return;
    times[key].forEach((item) => {
      clearTimeout(item);
    });
    times[key] = [];
  }

  return {
    addSetTimeout,
    clearSetTimeout,
  };
}
