"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useBetaStatus = useBetaStatus;
exports.useRequestBetaAccess = useRequestBetaAccess;
const react_1 = require("react");
const streamplace_store_1 = require("../streamplace-store/streamplace-store");
const xrpc_1 = require("../streamplace-store/xrpc");
// Reports the logged-in account's access status for a named beta feature
// (e.g. "vod") via place.stream.beta.getStatus. The DID is passed explicitly
// so the query works against the unauthenticated node agent. Returns
// status=null while loading or when logged out.
function useBetaStatus(feature) {
    const agent = (0, xrpc_1.usePossiblyUnauthedPDSAgent)();
    const did = (0, streamplace_store_1.useDID)();
    const [status, setStatus] = (0, react_1.useState)(null);
    const [loading, setLoading] = (0, react_1.useState)(true);
    const [error, setError] = (0, react_1.useState)(null);
    const refetch = (0, react_1.useCallback)(async () => {
        if (!agent || !did) {
            setStatus(null);
            setLoading(false);
            return;
        }
        setLoading(true);
        setError(null);
        try {
            const res = await agent.place.stream.beta.getStatus({ feature, did });
            setStatus(res.data.status);
        }
        catch (e) {
            console.error("error fetching beta status", e);
            setError(e?.message ?? "failed to load beta status");
        }
        finally {
            setLoading(false);
        }
    }, [agent, did, feature]);
    (0, react_1.useEffect)(() => {
        refetch();
    }, [refetch]);
    // Optimistically reflect a just-published request without waiting for the
    // firehose to index it and getStatus to catch up.
    const markRequested = (0, react_1.useCallback)(() => setStatus("requested"), []);
    return { status, loading, error, refetch, markRequested, did };
}
// Publishes a place.stream.beta.request record in the caller's own repo,
// asking for access to the named feature. The requesting account is the
// record's authority, so no subject field is needed.
function useRequestBetaAccess(feature) {
    const agent = (0, xrpc_1.usePDSAgent)();
    const did = (0, streamplace_store_1.useDID)();
    return (0, react_1.useCallback)(async () => {
        if (!agent || !did) {
            throw new Error("not logged in");
        }
        const record = {
            $type: "place.stream.beta.request",
            feature,
            createdAt: new Date().toISOString(),
        };
        return await agent.com.atproto.repo.createRecord({
            repo: did,
            collection: "place.stream.beta.request",
            record,
        });
    }, [agent, did, feature]);
}
//# sourceMappingURL=useBeta.js.map