import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Button, VModal, VModalBody, VModalTitle } from "@vertesia/ui/core";
import { useEffect, useState } from "react";
import { useUserSession } from "@vertesia/ui/session";
export function InviteAcceptModal() {
    const session = useUserSession();
    const { client, account } = session;
    const [showModal, setShowModal] = useState(false);
    const [invites, setInvites] = useState([]);
    useEffect(() => {
        client.account.listInvites().then(invites => {
            if (invites.length > 0) {
                // Filter out legacy invites that do not have account data
                const notLegacyInvites = invites.filter(i => i.data.account);
                if (notLegacyInvites.length === 0) {
                    console.log("No valid invites found, closing modal");
                    return;
                }
                // If we have valid invites, show the modal
                console.log("Found valid invites", notLegacyInvites.length);
                setShowModal(true);
                setInvites(notLegacyInvites);
            }
            else {
                console.log("No invites found, closing modal");
                setShowModal(false);
            }
        }).catch(err => {
            console.error("Error fetching invites", err);
        });
    }, [account?.id]);
    const closeModal = () => setShowModal(false);
    const accept = async (invite) => {
        await client.account.acceptInvite(invite.id);
        await session.fetchAccounts();
        const remainingInvites = invites.filter(i => i.id !== invite.id);
        const notLegacyInvites = remainingInvites.filter(i => i.data.account);
        if (notLegacyInvites.length > 0) {
            setInvites(notLegacyInvites);
        }
        else {
            closeModal();
        }
    };
    const reject = async (invite) => {
        await client.account.rejectInvite(invite.id);
        const remainingInvites = invites.filter(i => i.id !== invite.id);
        setInvites(remainingInvites);
        if (remainingInvites.length === 0) {
            closeModal();
        }
    };
    const inviteList = invites.map(invite => {
        if (!invite.data.account) {
            console.warn("Invite has no account data", invite);
            return null; // Skip rendering this invite
        }
        return (_jsxs("div", { className: "flex flex-row w-full justify-between border rounded-sm px-2 py-2 ", children: [_jsxs("div", { className: "flex flex-col", children: [_jsx("div", { className: "w-full font-semibold", children: invite.data.account.name ?? invite.data.account }), invite.data.project && _jsxs("div", { className: "w-full text-base", children: ["- ", invite.data.project.name] }), _jsxs("div", { className: "text-xs", children: ["Role: ", invite.data.role] }), invite.data.invited_by && _jsxs("div", { className: "text-xs", children: ["by ", invite.data.invited_by.name ?? invite.data.invited_by] })] }), _jsxs("div", { className: "flex flex-col gap-4", children: [_jsx(Button, { size: 'xs', onClick: () => accept(invite), children: "Accept" }), " ", _jsx(Button, { size: 'xs', variant: "secondary", onClick: () => reject(invite), children: "Reject" })] })] }, invite.id));
    });
    return (_jsx("div", { children: _jsxs(VModal, { isOpen: showModal, onClose: closeModal, children: [_jsx(VModalTitle, { children: "Review Invites" }), _jsxs(VModalBody, { children: [_jsx("div", { className: "text-sm pb-4", children: "You have received the following invites to join other accounts. Please review and accept or declined them." }), inviteList] })] }) }));
}
//# sourceMappingURL=InviteAcceptModal.js.map