import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Center } from "@vertesia/ui/core";
import { LastSelectedAccountId_KEY, LastSelectedProjectId_KEY, useUserSession } from "@vertesia/ui/session";
import { LockIcon } from "lucide-react";
import { useEffect, useState } from "react";
import { AppInstallationProvider } from "./AppInstallationProvider";
import { AppProjectSelector } from "./AppProjectSelector";
export function StandaloneApp({ name, AccessDenied = AccessDeniedMessage, children }) {
    return name ? (_jsx(StandaloneAppImpl, { name: name, AccessDenied: AccessDenied, children: children })) : (_jsx(UnknownAppName, {}));
}
export function StandaloneAppImpl({ name, AccessDenied = AccessDeniedMessage, children }) {
    const { authToken, client } = useUserSession();
    const [installation, setInstallation] = useState(null);
    const [state, setState] = useState("loading");
    useEffect(() => {
        if (!authToken) {
            setState("loading");
        }
        else {
            const isAppVisible = authToken.apps.includes(name);
            if (isAppVisible) {
                client.apps.getAppInstallationByName(name).then(inst => {
                    if (!inst) {
                        console.log(`App ${name} not found!`);
                        setState("error");
                    }
                    else {
                        setState("loaded");
                        setInstallation(inst);
                    }
                });
            }
            else {
                setState("error");
            }
        }
    }, [name, authToken]);
    if (state === "loading") {
        return null;
    }
    else if (state === "error") {
        return _jsx(AccessDenied, { name: name });
    }
    else if (installation) {
        return _jsx(AppInstallationProvider, { installation: installation, children: children });
    }
}
function AccessDeniedMessage({ name }) {
    const { project } = useUserSession();
    const onChange = (project) => {
        localStorage.setItem(LastSelectedAccountId_KEY, project.account);
        localStorage.setItem(LastSelectedProjectId_KEY + '-' + project.account, project.id);
        window.location.reload();
    };
    return (_jsxs(Center, { className: "pt-10 flex flex-col items-center text-center text-gray-700", children: [_jsx(LockIcon, { className: "w-10 h-10 mb-4 text-gray-500" }), _jsx("div", { className: "text-xl font-semibold", children: "Access Denied" }), _jsxs("div", { className: "mt-2 text-sm text-gray-500", children: ["You don't have permission to view the ", _jsx("span", { className: "font-semibold", children: name }), " app in project: ", _jsxs("span", { className: "font-semibold", children: ["\u00AB", project?.name, "\u00BB"] }), "."] }), _jsx("div", { className: "mt-4", children: _jsx(AppProjectSelector, { app: { name }, onChange: onChange }) })] }));
}
function UnknownAppName() {
    return (_jsxs(Center, { className: "pt-10 flex flex-col items-center text-center text-gray-700", children: [_jsx(LockIcon, { className: "w-10 h-10 mb-4 text-gray-500" }), _jsx("div", { className: "text-xl font-semibold", children: "Application not registered" }), _jsxs("div", { className: "mt-2 text-sm text-gray-500", children: ["Before starting to code a Vertesia application you must register an application manifest in Vertesia Studio then install it in one or more projects.", _jsx("p", {}), "Then use the created app name as a parameter to ", _jsx("code", { children: "<StandaloneApp name=\"your-app-name\">" }), " in the ", _jsx("code", { children: "src/main.tsx" }), " file."] })] }));
}
//# sourceMappingURL=StandaloneApp.js.map