"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLikeCount = exports.useGetLikes = exports.useGetVodComments = exports.useDeleteLike = exports.useCreateLike = exports.useDeleteVodComment = exports.useCreateVodComment = void 0;
const react_1 = require("react");
const streamplace_store_1 = require("../streamplace-store/streamplace-store");
const xrpc_1 = require("../streamplace-store/xrpc");
const useCreateVodComment = () => {
    const pdsAgent = (0, xrpc_1.usePDSAgent)();
    const userDID = (0, streamplace_store_1.useDID)();
    return (0, react_1.useCallback)(async (params) => {
        if (!pdsAgent || !userDID) {
            throw new Error("No PDS agent or user DID found");
        }
        const record = {
            $type: "place.stream.vod.comment",
            text: params.text,
            createdAt: new Date().toISOString(),
            video: params.video,
        };
        return await pdsAgent.com.atproto.repo.createRecord({
            repo: userDID,
            collection: "place.stream.vod.comment",
            record,
        });
    }, [pdsAgent, userDID]);
};
exports.useCreateVodComment = useCreateVodComment;
const useDeleteVodComment = () => {
    const pdsAgent = (0, xrpc_1.usePDSAgent)();
    const userDID = (0, streamplace_store_1.useDID)();
    return (0, react_1.useCallback)(async (uri) => {
        if (!pdsAgent || !userDID) {
            throw new Error("No PDS agent or user DID found");
        }
        const rkey = uri.split("/").pop();
        if (!rkey)
            throw new Error("No rkey found");
        return await pdsAgent.com.atproto.repo.deleteRecord({
            repo: userDID,
            collection: "place.stream.vod.comment",
            rkey,
        });
    }, [pdsAgent, userDID]);
};
exports.useDeleteVodComment = useDeleteVodComment;
const useCreateLike = () => {
    const pdsAgent = (0, xrpc_1.usePDSAgent)();
    const userDID = (0, streamplace_store_1.useDID)();
    return (0, react_1.useCallback)(async (subject) => {
        if (!pdsAgent || !userDID) {
            throw new Error("No PDS agent or user DID found");
        }
        const record = {
            $type: "place.stream.like",
            subject,
            createdAt: new Date().toISOString(),
        };
        return await pdsAgent.com.atproto.repo.createRecord({
            repo: userDID,
            collection: "place.stream.like",
            record,
        });
    }, [pdsAgent, userDID]);
};
exports.useCreateLike = useCreateLike;
const useDeleteLike = () => {
    const pdsAgent = (0, xrpc_1.usePDSAgent)();
    const userDID = (0, streamplace_store_1.useDID)();
    return (0, react_1.useCallback)(async (uri) => {
        if (!pdsAgent || !userDID) {
            throw new Error("No PDS agent or user DID found");
        }
        const rkey = uri.split("/").pop();
        if (!rkey)
            throw new Error("No rkey found");
        return await pdsAgent.com.atproto.repo.deleteRecord({
            repo: userDID,
            collection: "place.stream.like",
            rkey,
        });
    }, [pdsAgent, userDID]);
};
exports.useDeleteLike = useDeleteLike;
const useGetVodComments = () => {
    const pdsAgent = (0, xrpc_1.usePDSAgent)();
    return (0, react_1.useCallback)(async (video, limit, cursor) => {
        if (!pdsAgent) {
            throw new Error("No PDS agent found");
        }
        const res = await pdsAgent.place.stream.vod.getComments({
            video,
            limit,
            cursor,
        });
        return res.data;
    }, [pdsAgent]);
};
exports.useGetVodComments = useGetVodComments;
const useGetLikes = () => {
    const pdsAgent = (0, xrpc_1.usePDSAgent)();
    return (0, react_1.useCallback)(async (subject, limit, cursor) => {
        if (!pdsAgent) {
            throw new Error("No PDS agent found");
        }
        const res = await pdsAgent.place.stream.getLikes({
            subject,
            limit,
            cursor,
        });
        return res.data;
    }, [pdsAgent]);
};
exports.useGetLikes = useGetLikes;
const useLikeCount = () => {
    const pdsAgent = (0, xrpc_1.usePDSAgent)();
    return (0, react_1.useCallback)(async (subject) => {
        if (!pdsAgent) {
            throw new Error("No PDS agent found");
        }
        const res = await pdsAgent.place.stream.getLikes({
            subject,
            limit: 1,
        });
        return res.data.count;
    }, [pdsAgent]);
};
exports.useLikeCount = useLikeCount;
//# sourceMappingURL=vod-interactions.js.map