import { useCallback, useEffect, useState } from "react";
const DUD = typeof "";
const toString = (val) => {
    switch (typeof val) {
        case "boolean":
            return !val ? "0" : "1";
        case "string":
            return val;
        case "object":
            return JSON.stringify(val);
        default:
            throw new Error("Doesn't know how to handle type " + typeof val);
    }
};
const fromString = (type, value) => {
    switch (type) {
        case "boolean":
            return value === "1";
        case "string":
            return value;
        case "object":
            return JSON.parse(value);
        default:
            throw new Error("Doesn't know how to handle type " + type);
    }
};
export default function useLocalStorage(id, defaultValue) {
    const [val, setVal] = useState(() => {
        const persistedValue = localStorage.getItem(id);
        if (persistedValue === null)
            return defaultValue;
        return fromString(typeof defaultValue, persistedValue);
    });
    useEffect(() => {
        if (val !== defaultValue)
            localStorage.setItem(id, toString(val));
    }, [val]);
    const del = useCallback(() => {
        localStorage.removeItem(id);
        setVal(defaultValue);
    }, []);
    return [val, setVal, del];
}
//# sourceMappingURL=useLocalStorage.js.map