import { __awaiter } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
import EditorContext from "@qorebase/app-runtime/src/EditorContext";
import { RuntimeContext } from "@qorebase/app-runtime/src/RuntimeContext";
import { customAlphabet } from "nanoid";
import { exposeGlobalRuntime } from "@qorebase/app-runtime/src/utils/exposeGlobalRuntime";
import { generateImportMaps } from "../../../../../../app/utils/generateImportMap";
import Head from "next/head";
import dynamic from "next/dynamic";
import { useRouter } from "next/router";
import React from "react";
import qoreStorageClient from "../../../../../../app/hooks/qoreStorageClient";
const EditorPage = dynamic(() => import("../../../../../../app/editor/EditorPage"), {
    ssr: false,
});
export const subdomain = customAlphabet("abcdefghijklmnopqrstuvwxyz", 6);
const AppLoader = () => {
    const router = useRouter();
    const editorActions = EditorContext.useActions();
    React.useEffect(exposeGlobalRuntime, []);
    React.useEffect(() => {
        (() => __awaiter(void 0, void 0, void 0, function* () {
            var _a;
            if (!router.query.appID)
                return;
            console.log("Loading app", router.query.appID);
            const revisions = yield qoreStorageClient.client
                .table("revisions")
                .readRows({
                limit: 1,
                condition: {
                    apprevision: {
                        id: router.query.appID,
                        app_environment: {
                            project: {
                                $and: [
                                    {
                                        id: router.query.projectID,
                                    },
                                    {
                                        $or: [
                                            {
                                                "project-id_items": {
                                                    user: "{{user.id}}",
                                                    "project-role": {
                                                        roles_items: {
                                                            permissions: {
                                                                name: "project.detail",
                                                            },
                                                        },
                                                    },
                                                },
                                            },
                                            {
                                                team: {
                                                    "team-id_items": {
                                                        user: "{{user.id}}",
                                                        "team-role": {
                                                            roles_items: {
                                                                permissions: {
                                                                    name: "project.detail",
                                                                },
                                                            },
                                                        },
                                                    },
                                                },
                                            },
                                            {
                                                orgs: {
                                                    "organization-id_items": {
                                                        user: "{{user.id}}",
                                                        "organization-role": {
                                                            roles_items: {
                                                                permissions: {
                                                                    name: "project.detail",
                                                                },
                                                            },
                                                        },
                                                    },
                                                },
                                            },
                                        ],
                                    },
                                ],
                            },
                            name: router.query.env === "live" ? "production" : "staging",
                        },
                    },
                },
                orderBy: { created_at: "DESC" },
            })
                .toPromise();
            const environment = yield qoreStorageClient.client
                .table("environment")
                .readRows({
                condition: {
                    project: { id: { $eq: router.query.projectID } },
                    name: "production",
                },
            })
                .toPromise();
            const production = environment.data.nodes[0];
            const currentApp = (_a = revisions.data) === null || _a === void 0 ? void 0 : _a.nodes[0];
            if (!(currentApp === null || currentApp === void 0 ? void 0 : currentApp.id))
                return;
            editorActions.setRevisionID(currentApp.id);
            editorActions.setHasProduction((production === null || production === void 0 ? void 0 : production.id) ? true : false);
            console.log("Loaded", router.query.appID);
        }))();
    }, [router.query.appID]);
    React.useEffect(() => {
        editorActions.setEditMode("select");
    }, []);
    return _jsx(EditorPage, {});
};
export function getServerSideProps(context) {
    return __awaiter(this, void 0, void 0, function* () {
        return {
            props: {
                importMap: yield generateImportMaps(),
            },
        };
    });
}
const EditorWrapper = ({ importMap, }) => {
    return (_jsxs(_Fragment, { children: [_jsx(Head, { children: _jsx("script", { type: "importmap", dangerouslySetInnerHTML: {
                        __html: JSON.stringify(importMap),
                    } }) }), _jsx(EditorContext.Provider, { children: _jsx(RuntimeContext.Provider, { children: _jsx(AppLoader, {}) }) })] }));
};
export default EditorWrapper;
//# sourceMappingURL=%5B%5B...appRoutes%5D%5D.js.map