import { useCallback, useEffect, useRef, useState } from "react";
import { getStoredValue, setStoredValue } from "../utils/Storage.js";
const useStorageState = (key, initialValue, storageType = 'localStorage', prefix = 'ui')=>{
    const getValue = useCallback(()=>{
        const storedValue = getStoredValue(key, storageType, prefix);
        const result = void 0 !== storedValue ? storedValue : initialValue;
        return result;
    }, [
        initialValue,
        key,
        storageType,
        prefix
    ]);
    const [value, setValue] = useState(getValue);
    const isInitialMount = useRef(true);
    useEffect(()=>{
        if (isInitialMount.current) {
            isInitialMount.current = false;
            return;
        }
        setStoredValue(key, value, storageType, prefix);
    }, [
        value,
        key,
        storageType,
        prefix
    ]);
    const setStoredValueCallback = useCallback((newValue)=>{
        setValue((prevValue)=>{
            const valueToStore = newValue instanceof Function ? newValue(prevValue) : newValue;
            setStoredValue(key, valueToStore, storageType, prefix);
            return valueToStore;
        });
    }, [
        key,
        storageType,
        prefix
    ]);
    return [
        value,
        setStoredValueCallback
    ];
};
export { useStorageState };
