"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LikeButton = LikeButton;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const lucide_react_native_1 = require("lucide-react-native");
const react_1 = require("react");
const react_native_1 = require("react-native");
const react_native_reanimated_1 = tslib_1.__importStar(require("react-native-reanimated"));
const streamplace_store_1 = require("../../streamplace-store/streamplace-store");
const ui_1 = require("../../ui");
const vod_interactions_1 = require("../../vod-store/vod-interactions");
const text_1 = require("../ui/text");
function LikeButton({ subjectUri }) {
    const [likeCount, setLikeCount] = (0, react_1.useState)(0);
    const [userLiked, setUserLiked] = (0, react_1.useState)(false);
    const [userLikeUri, setUserLikeUri] = (0, react_1.useState)(null);
    const [loading, setLoading] = (0, react_1.useState)(false);
    const userDID = (0, streamplace_store_1.useDID)();
    const getLikes = (0, vod_interactions_1.useGetLikes)();
    const createLike = (0, vod_interactions_1.useCreateLike)();
    const deleteLike = (0, vod_interactions_1.useDeleteLike)();
    const { theme } = (0, ui_1.useTheme)();
    const scale = (0, react_native_reanimated_1.useSharedValue)(1);
    const animatedStyle = (0, react_native_reanimated_1.useAnimatedStyle)(() => ({
        transform: [{ scale: scale.value }],
    }));
    const loadLikes = (0, react_1.useCallback)(async () => {
        try {
            const result = await getLikes(subjectUri, 50);
            setLikeCount(result.count);
            if (userDID && result.likes) {
                const myLike = result.likes.find((l) => l.author.did === userDID);
                setUserLiked(!!myLike);
                setUserLikeUri(myLike?.uri || null);
            }
        }
        catch (e) {
            console.error("Failed to load likes", e);
        }
    }, [subjectUri, getLikes, userDID]);
    (0, react_1.useEffect)(() => {
        loadLikes();
    }, [loadLikes]);
    const toggleLike = (0, react_1.useCallback)(async () => {
        scale.value = (0, react_native_reanimated_1.withSpring)(1.05, { stiffness: 500, damping: 10 }, () => {
            scale.value = (0, react_native_reanimated_1.withSpring)(1, { stiffness: 500 });
        });
        setLoading(true);
        try {
            if (userLiked && userLikeUri) {
                await deleteLike(userLikeUri);
                setUserLiked(false);
                setUserLikeUri(null);
                setLikeCount((c) => Math.max(0, c - 1));
            }
            else {
                const result = await createLike(subjectUri);
                setUserLiked(true);
                setUserLikeUri(result.data.uri);
                setLikeCount((c) => c + 1);
            }
        }
        catch (e) {
            console.error("Failed to toggle like", e);
        }
        finally {
            setLoading(false);
        }
    }, [userLiked, userLikeUri, subjectUri, createLike, deleteLike, scale]);
    const heartColor = userLiked
        ? theme.colors.background
        : theme.colors.foreground;
    return ((0, jsx_runtime_1.jsx)(react_native_1.TouchableOpacity, { onPress: toggleLike, disabled: loading, children: (0, jsx_runtime_1.jsxs)(react_native_1.View, { style: [ui_1.layout.flex.row, ui_1.layout.flex.alignCenter, ui_1.gap.all[2]], children: [(0, jsx_runtime_1.jsx)(react_native_reanimated_1.default.View, { style: animatedStyle, children: (0, jsx_runtime_1.jsx)(lucide_react_native_1.ThumbsUp, { color: heartColor, fill: userLiked ? heartColor : "none" }) }), (0, jsx_runtime_1.jsx)(text_1.Text, { size: "sm", children: likeCount })] }) }));
}
//# sourceMappingURL=like-button.js.map