"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.calcLayoutItemsHeight = exports.alignGroupedItemsHeights = exports.calcHeight = exports.copyPropsFromPreviousLayout = exports.getReltioLayoutItemById = exports.reactGridLayoutToReltioLayout = exports.reltioLayoutToReactGridLayout = exports.reactGridLayoutItemToReltioLayoutItem = exports.reltioLayoutItemToReactGridLayoutItem = void 0;
var ramda_1 = require("ramda");
var reltioLayoutItemToReactGridLayoutItem = function (_a) {
    var x = _a.x, y = _a.y, width = _a.width, height = _a.height, id = _a.id, minWidth = _a.minWidth, minHeight = _a.minHeight, maxWidth = _a.maxWidth, maxHeight = _a.maxHeight, isStatic = _a.isStatic;
    return ({
        x: x,
        y: y,
        w: width,
        h: height,
        minW: minWidth,
        minH: minHeight,
        maxW: maxWidth,
        maxH: maxHeight,
        i: id,
        static: isStatic
    });
};
exports.reltioLayoutItemToReactGridLayoutItem = reltioLayoutItemToReactGridLayoutItem;
var reactGridLayoutItemToReltioLayoutItem = function (_a) {
    var x = _a.x, y = _a.y, w = _a.w, h = _a.h, maxW = _a.maxW, minW = _a.minW, minH = _a.minH, maxH = _a.maxH, i = _a.i, obj = __rest(_a, ["x", "y", "w", "h", "maxW", "minW", "minH", "maxH", "i"]);
    return ({
        x: x,
        y: y,
        width: w,
        height: h,
        minWidth: minW,
        minHeight: minH,
        maxWidth: maxW,
        maxHeight: maxH,
        id: i,
        isStatic: obj.static
    });
};
exports.reactGridLayoutItemToReltioLayoutItem = reactGridLayoutItemToReltioLayoutItem;
var reltioLayoutToReactGridLayout = function (layout) {
    return (layout || []).map(exports.reltioLayoutItemToReactGridLayoutItem);
};
exports.reltioLayoutToReactGridLayout = reltioLayoutToReactGridLayout;
var reactGridLayoutToReltioLayout = function (layout) {
    return (layout || []).map(exports.reactGridLayoutItemToReltioLayoutItem);
};
exports.reactGridLayoutToReltioLayout = reactGridLayoutToReltioLayout;
exports.getReltioLayoutItemById = (0, ramda_1.curry)(function (layout, id) {
    return (layout || []).find((0, ramda_1.propEq)('id', id));
});
var updateExtraPropsInLayoutItem = (0, ramda_1.curry)(function (props, from, to) { return (0, ramda_1.mergeAll)([to, (0, ramda_1.pick)(props, from)]); });
exports.copyPropsFromPreviousLayout = (0, ramda_1.curry)(function (props, oldReltioLayout, reltioLayout) {
    return (reltioLayout || []).map(function (layoutItem) {
        return (0, ramda_1.pipe)((0, ramda_1.prop)('id'), (0, exports.getReltioLayoutItemById)(oldReltioLayout), (0, ramda_1.defaultTo)({}), updateExtraPropsInLayoutItem(props, ramda_1.__, layoutItem))(layoutItem);
    });
});
var calcHeight = function (height, options) {
    var margin = options.margin, rowHeight = options.rowHeight;
    return Math.ceil((height + margin[1]) / (rowHeight + margin[1]));
};
exports.calcHeight = calcHeight;
exports.alignGroupedItemsHeights = (0, ramda_1.curry)(function (heightsMap, layout) {
    return layout.map(function (layoutItem) {
        var oneRowItemsHeights = layout
            .filter(function (item) { return item.y === layoutItem.y; })
            .map(function (item) { return heightsMap[item.id] || (!item.autosizing && item.minHeight) || 0; });
        var newHeight = Math.max.apply(Math, oneRowItemsHeights);
        if (layoutItem.height !== newHeight && newHeight) {
            var minMaxHeights = layoutItem.autosizing
                ? {
                    minHeight: newHeight,
                    maxHeight: newHeight
                }
                : {};
            return __assign(__assign(__assign({}, layoutItem), minMaxHeights), { height: newHeight });
        }
        return layoutItem;
    });
});
exports.calcLayoutItemsHeight = (0, ramda_1.curry)(function (heightsMap, layout) {
    return layout.map(function (layoutItem) {
        var height = heightsMap[layoutItem.id];
        if (height && layoutItem.autosizing) {
            return __assign(__assign({}, layoutItem), { minHeight: height, maxHeight: height, height: height });
        }
        return layoutItem;
    });
});
