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);
};
import React, { useEffect, useCallback, useMemo, useRef, useState, memo } from 'react';
import GridLayout from 'react-grid-layout';
import ReactResizeDetector from 'react-resize-detector';
import { pipe, ifElse, always, equals, identity } from 'ramda';
import classnames from 'classnames';
import { alignGroupedItemsHeights, calcLayoutItemsHeight, reltioLayoutToReactGridLayout, reactGridLayoutToReltioLayout, reactGridLayoutItemToReltioLayoutItem, copyPropsFromPreviousLayout, calcHeight, getReltioLayoutItemById } from './helpers';
import { DEFAULT_LAYOUT_OPTIONS } from './constants';
import { useStyles } from './styles';
var ReltioGridLayout = function (_a) {
    var _b;
    var views = _a.views, layout = _a.layout, _c = _a.layoutOptions, layoutOptions = _c === void 0 ? DEFAULT_LAYOUT_OPTIONS : _c, _d = _a.onLayoutChanged, onLayoutChanged = _d === void 0 ? 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 ? identity : _e;
    var ref = useRef();
    var shouldCompareLayout = useRef(false);
    var heightsMap = useRef({});
    var styles = useStyles();
    var _f = useState(null), resizingItemId = _f[0], setResizingItemId = _f[1];
    var _g = useState(null), fullscreenItemId = _g[0], setFullscreenItemId = _g[1];
    var _h = useState(0), width = _h[0], setWidth = _h[1];
    var cols = layoutOptions.cols, rowHeight = layoutOptions.rowHeight, margin = layoutOptions.margin;
    var processedLayout = useMemo(function () { return reltioLayoutToReactGridLayout(layout); }, [layout]);
    var convertReactLayoutToReltioLayout = pipe(reactGridLayoutToReltioLayout, copyPropsFromPreviousLayout(['autosizing'], layout), ifElse(always(isStatic), alignGroupedItemsHeights(heightsMap.current), calcLayoutItemsHeight(heightsMap.current)));
    var onReltioLayoutChanged = pipe(convertReactLayoutToReltioLayout, onLayoutChanged);
    var handleDrop = function (layout, layoutItem, e) {
        if (onDrop && layoutItem) {
            onDrop(convertReactLayoutToReltioLayout(layout), reactGridLayoutItemToReltioLayoutItem(layoutItem), e);
        }
    };
    var onToggleFullscreen = useCallback(function (id) { return setFullscreenItemId(function (prevId) { return (prevId ? null : id); }); }, []);
    var isFullscreenEnabled = fullscreenItemId !== null;
    var getAutosizeComponent = function (layoutItemId) { return (React.createElement(ReactResizeDetector, { handleHeight: true, handleWidth: false, refreshMode: "debounce", refreshRate: 50, onResize: function (_, height) {
            if (height) {
                var newHeight = calcHeight(height, layoutOptions);
                if (newHeight !== heightsMap.current[layoutItemId]) {
                    heightsMap.current[layoutItemId] = newHeight;
                    shouldCompareLayout.current = false;
                    onReltioLayoutChanged(processedLayout);
                }
            }
        } })); };
    useEffect(function () {
        if (isStatic) {
            if (shouldCompareLayout.current) {
                var expectedLayout = convertReactLayoutToReltioLayout(processedLayout);
                var isSameLayout = equals(reltioLayoutToReactGridLayout(expectedLayout), processedLayout);
                if (!isSameLayout) {
                    shouldCompareLayout.current = false;
                    onLayoutChanged(expectedLayout);
                }
            }
            else {
                shouldCompareLayout.current = true;
            }
        }
    }, [processedLayout, isStatic]);
    return (React.createElement(React.Fragment, null,
        React.createElement(ReactResizeDetector, { refreshMode: "debounce", refreshRate: 50, handleWidth: true, handleHeight: false, onResize: function (width) { return width > 0 && setWidth(width); } }),
        !!width && (React.createElement(GridLayout, { 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: classnames(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 = (getReltioLayoutItemById(layout, layoutItem.i) || {}).autosizing;
            var autosizeComponent = isAutosize
                ? { AutosizeComponent: getAutosizeComponent(layoutItem.i) }
                : {};
            return (React.createElement(LayoutItem, __assign({ key: layoutItem.i, layoutItemConfig: layoutItem, views: views, onToggleFullscreen: onToggleFullscreen, onRemove: onRemove, className: classnames(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';
export default memo(ReltioGridLayout);
