var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { useContext } from 'react';
import { useRecoilCallback } from 'recoil';
import { WebSocketCtx, useRequestExtras, useTaskContext } from '../../shared/context';
import {} from '../../types';
import { resolveVariable } from './resolve-variable';
import { isRegistered } from './store';
/**
 * Helper hook to get the current state of the variable.
 * For client-side variables, returns their value. For server-side variables, returns their resolved forms.
 */
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
export default function useVariableState() {
    const extras = useRequestExtras();
    const { client } = useContext(WebSocketCtx);
    const taskCtx = useTaskContext();
    return useRecoilCallback(({ snapshot }) => {
        return (variable) => __awaiter(this, void 0, void 0, function* () {
            if (!isRegistered(variable)) {
                return '__NOT_REGISTERED__';
            }
            // get the resolved form of the variable
            const resolvedVariable = yield resolveVariable(variable, client, taskCtx, extras, (v) => snapshot.getLoadable(v).toPromise());
            return resolvedVariable;
        });
    }, []);
}
//# sourceMappingURL=use-variable-state.js.map