import { __awaiter } from "tslib";
import { QoreClient } from "@qorebase/client";
import { qoreConfig } from "../../../../../app/hooks/qoreStorageClient";
import { normalizeAppComponent, normalizeAppPage, } from "@qorebase/app-runtime/src/utils/normalizeSchema";
const pageID = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    var _a;
    const client = new QoreClient(qoreConfig);
    client.project.axios.defaults.headers["x-qore-engine-admin-secret"] =
        qoreConfig.adminSecret;
    const revisions = yield client
        .table("revisions")
        .readRows({
        condition: {
            apprevision: { $eq: req.query.appId },
        },
        orderBy: {
            created_at: "DESC",
        },
        limit: 1,
    })
        .toPromise();
    const revision = (_a = revisions.data) === null || _a === void 0 ? void 0 : _a.nodes[0];
    if (!revision) {
        return res.status(404).json({
            data: {},
            error: {
                type: "NO_REVISION",
                message: "Application schema not found!",
            },
        });
    }
    const pageQuery = req.query.page.endsWith(".json")
        ? req.query.page.slice(0, -5)
        : req.query.page;
    const page = revision.data.maps.page[pageQuery];
    if (!page) {
        return res.status(404).json({
            data: {},
            error: {
                type: "PAGE_NOT_FOUND",
                message: "Page schema not found!",
            },
        });
    }
    const components = page.components.flatMap(({ component }) => revision.data.maps.component[component]
        ? [normalizeAppComponent(revision.data.maps.component[component])]
        : []);
    res.status(200).json({
        components,
        page: normalizeAppPage(page),
    });
});
export default pageID;
//# sourceMappingURL=%5Bpage%5D.js.map