"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGetContentMetadata = exports.useSaveContentMetadata = exports.useGetBroadcasterDID = void 0;
const streamplace_store_1 = require("./streamplace-store");
const xrpc_1 = require("./xrpc");
const useGetBroadcasterDID = () => {
    const pdsAgent = (0, xrpc_1.usePDSAgent)();
    const did = (0, streamplace_store_1.useDID)();
    const setBroadcasterDID = (0, streamplace_store_1.useStreamplaceStore)((state) => state.setBroadcasterDID);
    const setServerDID = (0, streamplace_store_1.useStreamplaceStore)((state) => state.setServerDID);
    return async () => {
        if (!pdsAgent || !did) {
            throw new Error("No PDS agent or DID available");
        }
        const result = await pdsAgent.place.stream.broadcast.getBroadcaster();
        if (!result.success) {
            throw new Error("Failed to get broadcaster DID");
        }
        setBroadcasterDID(result.data.broadcaster);
        if (result.data.server) {
            setServerDID(result.data.server);
        }
        else {
            setServerDID(null);
        }
    };
};
exports.useGetBroadcasterDID = useGetBroadcasterDID;
const useSaveContentMetadata = () => {
    const pdsAgent = (0, xrpc_1.usePDSAgent)();
    const did = (0, streamplace_store_1.useDID)();
    const setContentMetadata = (0, streamplace_store_1.useSetContentMetadata)();
    return async (metadataRecord) => {
        if (!pdsAgent || !did) {
            throw new Error("No PDS agent or DID available");
        }
        try {
            // Try to update existing record first
            const result = await pdsAgent.com.atproto.repo.putRecord({
                repo: did,
                collection: "place.stream.metadata.configuration",
                rkey: "self",
                record: metadataRecord,
            });
            const contentMetadata = {
                record: metadataRecord,
                uri: result.data.uri,
                cid: result.data.cid || "",
                rkey: "self",
            };
            setContentMetadata(contentMetadata);
            return contentMetadata;
        }
        catch (error) {
            // If record doesn't exist, create it
            if (error instanceof Error &&
                (error.message?.includes("not found") ||
                    error.message?.includes("RecordNotFound") ||
                    error.message?.includes("mst: not found") ||
                    error?.status === 404)) {
                const createResult = await pdsAgent.com.atproto.repo.createRecord({
                    repo: did,
                    collection: "place.stream.metadata.configuration",
                    rkey: "self",
                    record: metadataRecord,
                });
                const contentMetadata = {
                    record: metadataRecord,
                    uri: createResult.data.uri,
                    cid: createResult.data.cid || "",
                    rkey: "self",
                };
                setContentMetadata(contentMetadata);
                return contentMetadata;
            }
            throw error;
        }
    };
};
exports.useSaveContentMetadata = useSaveContentMetadata;
// Simple get function
const useGetContentMetadata = () => {
    const pdsAgent = (0, xrpc_1.usePDSAgent)();
    const did = (0, streamplace_store_1.useDID)();
    const setContentMetadata = (0, streamplace_store_1.useSetContentMetadata)();
    return async (params) => {
        if (!pdsAgent) {
            throw new Error("No PDS agent available");
        }
        const targetDid = params?.userDid || did;
        if (!targetDid) {
            throw new Error("No DID provided or user not authenticated");
        }
        try {
            const result = await pdsAgent.com.atproto.repo.getRecord({
                repo: targetDid,
                collection: "place.stream.metadata.configuration",
                rkey: params?.rkey || "self",
            });
            if (!result.success) {
                throw new Error("Failed to get content metadata record");
            }
            const contentMetadata = {
                record: result.data.value,
                uri: result.data.uri,
                cid: result.data.cid || "",
            };
            setContentMetadata(contentMetadata);
            return contentMetadata;
        }
        catch (error) {
            // Handle record not found - this is normal for new users
            if (error instanceof Error &&
                (error.message?.includes("not found") ||
                    error.message?.includes("RecordNotFound") ||
                    error.message?.includes("mst: not found") ||
                    error?.status === 404)) {
                return null;
            }
            throw error;
        }
    };
};
exports.useGetContentMetadata = useGetContentMetadata;
//# sourceMappingURL=content-metadata-actions.js.map