"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShareSheet = ShareSheet;
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 theme_1 = require("../../lib/theme");
const livestream_store_1 = require("../../livestream-store");
const streamplace_store_1 = require("../../streamplace-store");
const format_handle_1 = require("../../utils/format-handle");
const bluesky_icon_1 = require("../icons/bluesky-icon");
const ui_1 = require("../ui");
function ShareSheet({ onShare, target } = {}) {
    const profile = (0, livestream_store_1.useLivestreamStore)((x) => x.profile);
    const [isCopying, setIsCopying] = (0, react_1.useState)(false);
    const url = (0, streamplace_store_1.useUrl)();
    // Get the current stream URL
    const getStreamUrl = (0, react_1.useCallback)(() => {
        if (target)
            return target.url;
        return url + (profile ? `/${(0, format_handle_1.formatHandle)(profile)}` : "");
    }, [target, url, profile]);
    // Get the embed URL
    const getEmbedUrl = (0, react_1.useCallback)(() => {
        if (target)
            return target.embedUrl;
        return url + (profile ? `/embed/${(0, format_handle_1.formatHandle)(profile)}` : "");
    }, [target, url, profile]);
    // Get embed code
    const getEmbedCode = (0, react_1.useCallback)(() => {
        const embedUrl = getEmbedUrl();
        return `<iframe src="${embedUrl}" width="640" height="360" frameborder="0" allowfullscreen></iframe>`;
    }, [getEmbedUrl]);
    // Copy to clipboard handler
    const copyToClipboard = (0, react_1.useCallback)(async (text, label) => {
        setIsCopying(true);
        try {
            if (react_native_1.Platform.OS === "web") {
                await navigator.clipboard.writeText(text);
            }
            else {
                react_native_1.Clipboard.setString(text);
            }
            onShare?.(`copy_${label.toLowerCase().replace(/\s+/g, "_")}`, true);
        }
        catch (error) {
            onShare?.(`copy_${label.toLowerCase().replace(/\s+/g, "_")}`, false);
        }
        finally {
            setIsCopying(false);
        }
    }, [onShare]);
    // Share to Bluesky
    const shareToBluesky = (0, react_1.useCallback)(() => {
        const streamUrl = getStreamUrl();
        const text = target?.message
            ? `${target.message} ${streamUrl}`
            : profile && profile.handle
                ? `Check out @${profile.handle} live on Streamplace! ${streamUrl}`
                : `Check out this stream on Streamplace! ${streamUrl}`;
        const blueskyUrl = `https://bsky.app/intent/compose?text=${encodeURIComponent(text)}`;
        react_native_1.Linking.openURL(blueskyUrl);
        onShare?.("share_bluesky", true);
    }, [profile, getStreamUrl, onShare, target]);
    // Native share (mobile)
    const nativeShare = (0, react_1.useCallback)(async () => {
        const streamUrl = getStreamUrl();
        const text = target?.message
            ? target.message
            : profile && profile.handle
                ? `Check out @${profile.handle} live on Streamplace!`
                : `Check out this stream on Streamplace!`;
        if (react_native_1.Platform.OS === "web" && navigator.share) {
            try {
                await navigator.share({
                    title: "Streamplace",
                    text: text,
                    url: streamUrl,
                });
                onShare?.("share_native", true);
            }
            catch (error) {
                // User cancelled or error occurred
                onShare?.("share_native", false);
            }
        }
    }, [profile, getStreamUrl, onShare, target]);
    return ((0, jsx_runtime_1.jsxs)(ui_1.DropdownMenu, { children: [(0, jsx_runtime_1.jsx)(ui_1.DropdownMenuTrigger, { children: (0, jsx_runtime_1.jsx)(lucide_react_native_1.Share2, { color: theme_1.colors.gray[200] }) }), (0, jsx_runtime_1.jsxs)(ui_1.ResponsiveDropdownMenuContent, { children: [(0, jsx_runtime_1.jsxs)(ui_1.DropdownMenuGroup, { title: "Share", children: [(0, jsx_runtime_1.jsx)(ui_1.DropdownMenuItem, { onPress: shareToBluesky, closeOnPress: true, children: (0, jsx_runtime_1.jsxs)(react_native_1.View, { style: { flexDirection: "row", alignItems: "center", gap: 12 }, children: [(0, jsx_runtime_1.jsx)(bluesky_icon_1.BlueskyIcon, { size: 20, color: theme_1.colors.gray[400] }), (0, jsx_runtime_1.jsx)(ui_1.Text, { children: "Share to Bluesky" })] }) }), react_native_1.Platform.OS !== "web" || (navigator && navigator.share) ? ((0, jsx_runtime_1.jsx)(ui_1.DropdownMenuItem, { onPress: nativeShare, children: (0, jsx_runtime_1.jsxs)(react_native_1.View, { style: { flexDirection: "row", alignItems: "center", gap: 12 }, children: [(0, jsx_runtime_1.jsx)(lucide_react_native_1.Share2, { size: 20, color: theme_1.colors.gray[400] }), (0, jsx_runtime_1.jsx)(ui_1.Text, { children: "More Options..." })] }) })) : null] }), (0, jsx_runtime_1.jsxs)(ui_1.DropdownMenuGroup, { title: "Copy", children: [(0, jsx_runtime_1.jsx)(ui_1.DropdownMenuItem, { onPress: () => copyToClipboard(getStreamUrl(), "Stream link"), disabled: isCopying, closeOnPress: true, children: (0, jsx_runtime_1.jsxs)(react_native_1.View, { style: { flexDirection: "row", alignItems: "center", gap: 12 }, children: [(0, jsx_runtime_1.jsx)(lucide_react_native_1.Link2, { size: 20, color: theme_1.colors.gray[400] }), (0, jsx_runtime_1.jsx)(ui_1.Text, { children: "Copy Link" })] }) }), (0, jsx_runtime_1.jsx)(ui_1.DropdownMenuSeparator, {}), (0, jsx_runtime_1.jsx)(ui_1.DropdownMenuItem, { onPress: () => copyToClipboard(getEmbedCode(), "Embed code"), disabled: isCopying, closeOnPress: true, children: (0, jsx_runtime_1.jsxs)(react_native_1.View, { style: { flexDirection: "row", alignItems: "center", gap: 12 }, children: [(0, jsx_runtime_1.jsx)(lucide_react_native_1.Code, { size: 20, color: theme_1.colors.gray[400] }), (0, jsx_runtime_1.jsx)(ui_1.Text, { children: "Copy Embed Code" })] }) }), (0, jsx_runtime_1.jsx)(ui_1.DropdownMenuSeparator, {}), (0, jsx_runtime_1.jsx)(ui_1.DropdownMenuItem, { closeOnPress: true, onPress: () => copyToClipboard(getEmbedUrl(), "Embed URL"), disabled: isCopying, children: (0, jsx_runtime_1.jsxs)(react_native_1.View, { style: { flexDirection: "row", alignItems: "center", gap: 12 }, children: [(0, jsx_runtime_1.jsx)(lucide_react_native_1.Copy, { size: 20, color: theme_1.colors.gray[400] }), (0, jsx_runtime_1.jsx)(ui_1.Text, { children: "Copy Embed URL" })] }) })] })] })] }));
}
//# sourceMappingURL=sharesheet.js.map