import { useEffect, useState } from 'react';
import { loadItem, saveItem } from '../utils';

export const useLocalStorage = (
  key: string,
  initialItem: any,
  secure: boolean = false
) => {
  const [item, setItem] = useState<any>(() => (
    loadItem(key, initialItem, secure)
  ));

  useEffect(() => {
    saveItem(key, item, secure);
  }, [key, item, secure]);

  return [item, setItem];
};
