var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { useMemo } from 'react';
import DashboardCardGrid from '../dashboard/DashboardCardGrid';
import DashboardWidget from '../dashboard/DashboardWidget';
import getWidgetLayout from '../dashboard/getWidgetLayout.ts';
import StaticPageRenderer from './StaticPageRenderer';
import { useShellStore } from '../stores/shellStore';
const Page = (props) => {
    const { gridConfig, widgetConfig, disableMargin, autoHeight, pageTypeId, } = props;
    const { getWidget, isValidWidget } = useShellStore();
    const gridProperties = gridConfig ? JSON.parse(gridConfig) : null;
    let widgets = widgetConfig || null;
    if (typeof widgetConfig === 'string') {
        widgets = JSON.parse(widgetConfig);
    }
    const calculateLayouts = () => {
        var _a;
        var _b, _c;
        if (!widgets)
            return null;
        const normalizeNumber = (value, fallback) => {
            if (typeof value === 'number' && Number.isFinite(value)) {
                return value;
            }
            if (typeof value === 'string') {
                const parsed = Number(value);
                if (Number.isFinite(parsed)) {
                    return parsed;
                }
            }
            return fallback;
        };
        const layouts = {};
        for (const widget of widgets) {
            if (!(widget === null || widget === void 0 ? void 0 : widget.layout))
                continue;
            for (const breakpoint in widget.layout) {
                if (breakpoint === '__typename' || !widget.layout[breakpoint])
                    continue;
                layouts[breakpoint] = layouts[breakpoint] || [];
                const _d = widget.layout[breakpoint], { __typename } = _d, layout = __rest(_d, ["__typename"]);
                const defaultLayout = getWidgetLayout(getWidget, widget.widgetType);
                const mergedLayout = Object.assign(Object.assign(Object.assign({}, defaultLayout), layout), { i: widget.id });
                mergedLayout.x = normalizeNumber(mergedLayout.x, 0);
                mergedLayout.y = normalizeNumber(mergedLayout.y, 0);
                mergedLayout.w = normalizeNumber(mergedLayout.w, (_b = defaultLayout === null || defaultLayout === void 0 ? void 0 : defaultLayout.w) !== null && _b !== void 0 ? _b : 2);
                mergedLayout.h = normalizeNumber(mergedLayout.h, (_c = defaultLayout === null || defaultLayout === void 0 ? void 0 : defaultLayout.h) !== null && _c !== void 0 ? _c : 2);
                const paramResizable = (_a = widget === null || widget === void 0 ? void 0 : widget.params) === null || _a === void 0 ? void 0 : _a.isResizable;
                mergedLayout.isResizable =
                    typeof paramResizable === 'boolean'
                        ? paramResizable
                        : Boolean(mergedLayout.isResizable);
                layouts[breakpoint].push(mergedLayout);
            }
        }
        return layouts;
    };
    const layouts = useMemo(() => calculateLayouts(), [calculateLayouts, widgetConfig]);
    const cards = useMemo(() => widgets &&
        widgets.map((_a) => {
            var { __typename } = _a, widget = __rest(_a, ["__typename"]);
            return (React.createElement(DashboardWidget, Object.assign({ key: widget.id }, widget)));
        }), [widgets]);
    if (!layouts)
        return null;
    if (Number(pageTypeId) === 1) {
        return (React.createElement(StaticPageRenderer, null, cards));
    }
    return (React.createElement(DashboardCardGrid, { layouts: layouts, gridProperties: gridProperties, disableMargin: disableMargin, autoHeight: autoHeight }, cards));
};
export default Page;
//# sourceMappingURL=Page.js.map