"use strict";
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 });
exports.ActivityLog = void 0;
var react_1 = __importStar(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var ramda_1 = require("ramda");
var helpers_1 = require("./helpers");
var Divider_1 = __importDefault(require("@mui/material/Divider"));
var activities_1 = require("../utils/activities");
var GroupItem_1 = require("./components/GroupItem");
var ActivityItem_1 = require("./components/ActivityItem");
var AutoSizeList_1 = require("../../../AutoSizeList");
var CollapsibleItemsContext_1 = require("../contexts/CollapsibleItemsContext");
var styles_1 = require("./styles");
var ActivityLog = function (_a) {
    var width = _a.width, height = _a.height, activities = _a.activities;
    var styles = (0, styles_1.useStyles)();
    var _b = (0, react_1.useState)([]), groupedItems = _b[0], setGroupedItems = _b[1];
    var toggleGroupedItem = function (index) {
        setGroupedItems((0, ramda_1.adjust)(index, (0, ramda_1.evolve)({ collapsed: ramda_1.not })));
    };
    (0, react_1.useEffect)(function () {
        (0, ramda_1.pipe)((0, ramda_1.map)(activities_1.removeIgnoredItems), (0, ramda_1.filter)(function (_a) {
            var itemsTotal = _a.itemsTotal;
            return itemsTotal !== 0;
        }), helpers_1.buildGroupedItems, setGroupedItems)(activities);
    }, [activities]);
    var flatItems = (0, react_1.useMemo)(function () { return (0, helpers_1.flattenGroupedItems)(groupedItems); }, [groupedItems]);
    var renderRow = (0, react_1.useCallback)(function (_a) {
        var _b;
        var data = _a.data, index = _a.index;
        var item = data[index];
        var isSingleRow = data.length === 1;
        var isFirstRow = index === 0;
        var isLastRow = index === data.length - 1;
        return (react_1.default.createElement("div", { className: (0, classnames_1.default)(styles.listRow, (_b = {},
                _b[styles.singleRow] = isSingleRow,
                _b[styles.firstRow] = isFirstRow,
                _b[styles.lastRow] = isLastRow,
                _b)) }, (0, helpers_1.isGroupItem)(item) ? (react_1.default.createElement(GroupItem_1.GroupItem, { item: item, onToggle: toggleGroupedItem })) : (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(ActivityItem_1.ActivityItem, { item: item }),
            !isLastRow && react_1.default.createElement(Divider_1.default, { className: styles.rowDivider, light: true, variant: "inset" })))));
    }, []); // eslint-disable-line
    return (react_1.default.createElement(CollapsibleItemsContext_1.CollapsibleItemsProvider, null,
        react_1.default.createElement(AutoSizeList_1.AutoSizeList, { width: width, height: height, data: flatItems, defaultItemSize: 40, itemKey: helpers_1.getItemKey }, renderRow)));
};
exports.ActivityLog = ActivityLog;
