"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAQState = useAQState;
const tslib_1 = require("tslib");
const react_1 = require("react");
const storage_1 = tslib_1.__importDefault(require("../storage"));
function useAQState(key, defaultValue) {
    const [state, setState] = (0, react_1.useState)(defaultValue);
    const [isLoaded, setIsLoaded] = (0, react_1.useState)(false);
    (0, react_1.useEffect)(() => {
        const loadFromStorage = async () => {
            try {
                const stored = await storage_1.default.getItem(key);
                if (stored !== null) {
                    setState(JSON.parse(stored));
                }
            }
            catch (error) {
                console.error(`Failed to load ${key} from storage:`, error);
            }
            finally {
                setIsLoaded(true);
            }
        };
        loadFromStorage();
    }, [key]);
    const setStoredState = (value) => {
        setState(value);
        if (isLoaded) {
            storage_1.default.setItem(key, JSON.stringify(value)).catch((error) => {
                console.error(`Failed to save ${key} to storage:`, error);
            });
        }
    };
    return [state, setStoredState];
}
//# sourceMappingURL=useAQState.js.map