"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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var react_grid_layout_1 = __importDefault(require("react-grid-layout"));
var react_resize_detector_1 = __importDefault(require("react-resize-detector"));
var ramda_1 = require("ramda");
var classnames_1 = __importDefault(require("classnames"));
var helpers_1 = require("./helpers");
var constants_1 = require("./constants");
var styles_1 = require("./styles");
var ReltioGridLayout = function (_a) {
    var _b;
    var views = _a.views, layout = _a.layout, _c = _a.layoutOptions, layoutOptions = _c === void 0 ? constants_1.DEFAULT_LAYOUT_OPTIONS : _c, _d = _a.onLayoutChanged, onLayoutChanged = _d === void 0 ? ramda_1.identity : _d, draggableHandle = _a.draggableHandle, LayoutItem = _a.LayoutItem, onRemove = _a.onRemove, onDrop = _a.onDrop, droppingItem = _a.droppingItem, isDroppable = _a.isDroppable, onDropDragOver = _a.onDropDragOver, isStatic = _a.isStatic, classes = _a.classes, _e = _a.onResize, onResize = _e === void 0 ? ramda_1.identity : _e;
    var ref = (0, react_1.useRef)(null);
    var shouldCompareLayout = (0, react_1.useRef)(false);
    var heightsMap = (0, react_1.useRef)({});
    var styles = (0, styles_1.useStyles)();
    var _f = (0, react_1.useState)(null), resizingItemId = _f[0], setResizingItemId = _f[1];
    var _g = (0, react_1.useState)(null), fullscreenItemId = _g[0], setFullscreenItemId = _g[1];
    var _h = (0, react_1.useState)(0), width = _h[0], setWidth = _h[1];
    var cols = layoutOptions.cols, rowHeight = layoutOptions.rowHeight, margin = layoutOptions.margin;
    var processedLayout = (0, react_1.useMemo)(function () { return (0, helpers_1.reltioLayoutToReactGridLayout)(layout); }, [layout]);
    var convertReactLayoutToReltioLayout = (0, ramda_1.pipe)(helpers_1.reactGridLayoutToReltioLayout, (0, helpers_1.copyPropsFromPreviousLayout)(['autosizing'], layout), (0, ramda_1.ifElse)((0, ramda_1.always)(isStatic), (0, helpers_1.alignGroupedItemsHeights)(heightsMap.current), (0, helpers_1.calcLayoutItemsHeight)(heightsMap.current)));
    var onReltioLayoutChanged = (0, ramda_1.pipe)(convertReactLayoutToReltioLayout, onLayoutChanged);
    var handleDrop = function (layout, layoutItem, e) {
        if (onDrop && layoutItem) {
            onDrop(convertReactLayoutToReltioLayout(layout), (0, helpers_1.reactGridLayoutItemToReltioLayoutItem)(layoutItem), e);
        }
    };
    var onToggleFullscreen = (0, react_1.useCallback)(function (id) { return setFullscreenItemId(function (prevId) { return (prevId ? null : id); }); }, []);
    var isFullscreenEnabled = fullscreenItemId !== null;
    var getAutosizeComponent = function (layoutItemId) { return (react_1.default.createElement(react_resize_detector_1.default, { handleHeight: true, handleWidth: false, refreshMode: "debounce", refreshRate: 50, onResize: function (_, height) {
            if (height) {
                var newHeight = (0, helpers_1.calcHeight)(height, layoutOptions);
                if (newHeight !== heightsMap.current[layoutItemId]) {
                    heightsMap.current[layoutItemId] = newHeight;
                    shouldCompareLayout.current = false;
                    onReltioLayoutChanged(processedLayout);
                }
            }
        } })); };
    (0, react_1.useEffect)(function () {
        if (isStatic) {
            if (shouldCompareLayout.current) {
                var expectedLayout = convertReactLayoutToReltioLayout(processedLayout);
                var isSameLayout = (0, ramda_1.equals)((0, helpers_1.reltioLayoutToReactGridLayout)(expectedLayout), processedLayout);
                if (!isSameLayout) {
                    shouldCompareLayout.current = false;
                    onLayoutChanged(expectedLayout);
                }
            }
            else {
                shouldCompareLayout.current = true;
            }
        }
    }, [processedLayout, isStatic]);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(react_resize_detector_1.default, { refreshMode: "debounce", refreshRate: 50, handleWidth: true, handleHeight: false, onResize: function (width) { return width > 0 && setWidth(width); } }),
        !!width && (react_1.default.createElement(react_grid_layout_1.default, { innerRef: ref, layout: processedLayout, cols: cols, draggableHandle: draggableHandle, autoSize: true, rowHeight: rowHeight, width: width, margin: margin, onLayoutChange: isStatic ? undefined : onReltioLayoutChanged, isResizable: !isStatic && !isFullscreenEnabled, isDraggable: !isStatic && !isFullscreenEnabled, className: (0, classnames_1.default)(classes === null || classes === void 0 ? void 0 : classes.root, styles.gridLayout, (_b = {},
                _b[styles.gridFullscreen] = isFullscreenEnabled,
                _b)), onDrop: handleDrop, onDropDragOver: onDropDragOver, droppingItem: droppingItem, isDroppable: !isStatic && isDroppable, onResizeStart: function (_, layoutItem) { return setResizingItemId(layoutItem.i); }, onResizeStop: function () { return setResizingItemId(null); }, onResize: onResize, "data-reltio-id": "reltio-grid-layout" }, processedLayout.map(function (layoutItem) {
            var _a;
            var isItemFullscreen = fullscreenItemId === layoutItem.i;
            var isResizing = resizingItemId === layoutItem.i;
            var isAutosize = ((0, helpers_1.getReltioLayoutItemById)(layout, layoutItem.i) || {}).autosizing;
            var autosizeComponent = isAutosize
                ? { AutosizeComponent: getAutosizeComponent(layoutItem.i) }
                : {};
            return (react_1.default.createElement(LayoutItem, __assign({ key: layoutItem.i, layoutItemConfig: layoutItem, views: views, onToggleFullscreen: onToggleFullscreen, onRemove: onRemove, className: (0, classnames_1.default)(classes === null || classes === void 0 ? void 0 : classes.item, (_a = {
                        static: isStatic,
                        fullscreen: isItemFullscreen
                    },
                    _a[styles.isResizing] = isResizing,
                    _a)), isFullscreen: isItemFullscreen }, autosizeComponent)));
        })))));
};
ReltioGridLayout.displayName = 'ReltioGridLayout';
exports.default = (0, react_1.memo)(ReltioGridLayout);
