import AsyncStorage from '@react-native-async-storage/async-storage';
import { useEffect, useState } from 'react';

const KEY_PREFIX = '@guide:';

export function useGuide(featureKey: string) {
   const [visible, setVisible] = useState(false);
   const [checked, setChecked] = useState(false);

   useEffect(() => {
      AsyncStorage.getItem(KEY_PREFIX + featureKey).then((val) => {
         if (!val) setVisible(true);
         setChecked(true);
      });
   }, [featureKey]);

   async function dismiss() {
      setVisible(false);
      await AsyncStorage.setItem(KEY_PREFIX + featureKey, 'done');
   }

   async function reset() {
      await AsyncStorage.removeItem(KEY_PREFIX + featureKey);
      setVisible(true);
   }

   return { visible: checked && visible, dismiss, reset };
}
