import { nanoid } from "nanoid";
import { showConditionToConditionGroup } from "../utils/filtrexToFormula";
import { omit } from "../utils/omit";
import { objectMap } from "../utils/objectMap";
import { initialApp, initialPage } from "../InitialData";
export const transformQorebaseSchema = (schema) => {
    var _a;
    // delete large unused data from column
    const transformColumn = (column_) => {
        const column = omit(["metadata", "isHidden", "description"], column_);
        const columnDefinition = column.definition &&
            omit([
                "default",
                "nullable",
                "unique",
                "textType",
                "triggers",
                "exact",
                "function",
                "expression",
            ], column_.definition);
        switch (column.type) {
            case "action":
                return Object.assign(Object.assign({}, column), { definition: (columnDefinition === null || columnDefinition === void 0 ? void 0 : columnDefinition.action) === "JavaScript"
                        ? omit(["function"], columnDefinition)
                        : (columnDefinition === null || columnDefinition === void 0 ? void 0 : columnDefinition.action) === "Print"
                            ? omit(["template"], columnDefinition)
                            : (columnDefinition === null || columnDefinition === void 0 ? void 0 : columnDefinition.action) === "Operations"
                                ? omit(["operations"], columnDefinition)
                                : columnDefinition });
            default:
                return Object.assign(Object.assign({}, column), { definition: columnDefinition });
        }
    };
    return {
        tables: schema.tables.map((table) => ({
            name: table.name,
            columns: table.columns.map(transformColumn),
        })),
        views: schema.tables.flatMap((table) => (table.views || []).map((view) => {
            var _a;
            return ({
                name: view.name,
                // @ts-ignore
                query: view.query,
                columns: (((_a = view.query) === null || _a === void 0 ? void 0 : _a.fields) || [])
                    .map((columnName) => table.columns.find((column) => column.name === columnName))
                    .filter(Boolean)
                    .map(transformColumn),
            });
        })),
        insights: schema.tables.flatMap((table) => (table.insights || []).map((insight) => ({
            name: insight.name,
            columns: insight.columns,
            table: table.name,
            sorts: insight.sorts,
            insight: insight.insight,
            params: insight.params,
            registeredParams: insight.registeredParams,
        }))),
        // @ts-ignore
        queries: (_a = schema.queries) !== null && _a !== void 0 ? _a : [],
    };
};
export const normalizeAppPage = (page) => {
    var _a;
    const newPage = Object.assign(Object.assign(Object.assign({}, initialPage), page), { params: page.params || {
            rowId: ((_a = page.defaultRow) === null || _a === void 0 ? void 0 : _a.type) === "row-id" ? page.defaultRow.rowId : "",
        }, navbar: page.navbar || initialPage.navbar });
    if (["navigation", "menu"].includes(newPage.type)) {
        delete newPage.type;
    }
    return newPage;
};
export const normalizeAppComponent = (component) => {
    return Object.assign(Object.assign({}, component), { showCondition: Array.isArray(component.showCondition)
            ? showConditionToConditionGroup(component.showCondition)
            : component.showCondition });
};
export const normalizeApp = (app) => {
    const pages = objectMap(app.maps.page, (page) => page && normalizeAppPage(page));
    const components = objectMap(app.maps.component, (component) => normalizeAppComponent(component));
    const convertOldNav = () => {
        const pages = Object.values(app.maps.page).filter((page) => page.type !== "system");
        const bottomContent = pages.slice(0, 4).map((page) => ({
            icon: page.icon,
            label: page.title,
            page: page.id,
        }));
        const sidetopContent = pages.reduce((sidetopContent, page) => {
            if (page.type === "navigation" || page.type === "menu") {
                const { id, title, icon } = page;
                const sidetopId = nanoid(3);
                return Object.assign(Object.assign({}, sidetopContent), { root: [...sidetopContent.root, sidetopId], items: Object.assign(Object.assign({}, sidetopContent.items), { [sidetopId]: {
                            id: sidetopId,
                            children: [],
                            hasChildren: false,
                            isExpanded: false,
                            isChildrenLoading: false,
                            data: {
                                label: title,
                                icon: icon,
                                page: id,
                                linked: true,
                            },
                        } }) });
            }
            return sidetopContent;
        }, {
            items: {},
            root: [],
        });
        return {
            bottom: Object.assign(Object.assign({}, initialApp.navbar.bottom), { content: bottomContent }),
            sidetop: Object.assign(Object.assign({}, initialApp.navbar.sidetop), { content: Object.assign(Object.assign({}, initialApp.navbar.sidetop.content), { rootId: "root", items: Object.assign(Object.assign(Object.assign({}, initialApp.navbar.sidetop.content.items), sidetopContent.items), { root: Object.assign(Object.assign({}, initialApp.navbar.sidetop.content.items.root), { children: sidetopContent.root }) }) }) }),
        };
    };
    return Object.assign(Object.assign({}, app), { maps: {
            page: pages,
            component: components,
        }, integrations: app.integrations || {}, customSources: app.customSources || {}, theme: Object.assign(Object.assign({}, initialApp.theme), app.theme), navbar: app.navbar || convertOldNav(), schema: Object.assign(Object.assign({}, initialApp.schema), app.schema), components: app.components || initialApp.components });
};
//# sourceMappingURL=normalizeSchema.js.map