"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIsRowCollapsibleFromMaxValuesCount = exports.getMaxRowValuesCount = exports.getTableRowsData = void 0;
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var DefaultCellValueRenderer_1 = require("../../DefaultCellValueRenderer");
var nestedHelpers_1 = require("./nestedHelpers");
var applyCellRendererToColumnCells = function (columnsData, renderRowCell, rowIndex, rowValue) { return function (cells, columnId) {
    var columnIndex = columnsData.findIndex((0, ramda_1.propEq)('id', columnId));
    var columnData = columnsData[columnIndex];
    var _a = columnsData[columnIndex].rowCellValueRenderer, CellValueRenderer = _a === void 0 ? DefaultCellValueRenderer_1.DefaultCellValueRenderer : _a;
    return cells.map(function (cell, cellIndex) { return (__assign(__assign({}, cell), { values: renderRowCell({
            columnIndex: columnIndex,
            columnData: columnData,
            rowIndex: rowIndex,
            rowValue: rowValue,
            cellIndex: cellIndex,
            cell: cell,
            CellValueRenderer: CellValueRenderer
        }) })); });
}; };
var applyCellRenderer = (0, ramda_1.pipe)(applyCellRendererToColumnCells, ramda_1.mapObjIndexed);
var calcHeightForColumnCells = function (columnsData, getRowCellHeight, rowIndex, rowValue) { return function (cells, columnId) {
    var columnIndex = columnsData.findIndex((0, ramda_1.propEq)('id', columnId));
    var columnData = columnsData[columnIndex];
    return cells.map(function (cell, cellIndex) { return (__assign(__assign({}, cell), { height: getRowCellHeight({ columnIndex: columnIndex, columnData: columnData, rowIndex: rowIndex, rowValue: rowValue, cellIndex: cellIndex, cell: cell }) })); });
}; };
var calcHeights = function (columnsData, getRowCellHeight, rowIndex, rowValue) { return function (rowData) {
    return getRowCellHeight
        ? (0, ramda_1.mapObjIndexed)(calcHeightForColumnCells(columnsData, getRowCellHeight, rowIndex, rowValue), rowData)
        : rowData;
}; };
var addRowSpansToColumnCells = function (rowSpansData) {
    return (0, ramda_1.map)(function (cell) { return (__assign(__assign({}, cell), { rowSpan: rowSpansData[cell.nestedKey || nestedHelpers_1.TOTAL_ROW_SPAN_KEY] })); });
};
var getNestedKeysFromRowCells = (0, ramda_1.pipe)((0, ramda_1.chain)((0, ramda_1.pluck)('nestedKey')), (0, ramda_1.reject)(ramda_1.isNil), ramda_1.uniq);
var calcRowSpansData = (0, ramda_1.pipe)(ramda_1.values, getNestedKeysFromRowCells, nestedHelpers_1.calcRowSpansForNestedKeys);
var calcRowSpans = function (rowData) {
    var rowSpansData = calcRowSpansData(rowData);
    return (0, ramda_1.isEmpty)(rowSpansData) ? rowData : (0, ramda_1.map)(addRowSpansToColumnCells(rowSpansData), rowData);
};
var calcNestedKeys = function (columnNestedPath) {
    return (0, ramda_1.map)(function (cell) { return (__assign(__assign({}, cell), { nestedKey: (0, nestedHelpers_1.getNestedKeyForNestedColumnCell)(columnNestedPath, cell) })); });
};
var parseNestedColumnValues = function (columnNestedPath, nestedValues) {
    return (0, ramda_1.pipe)(nestedHelpers_1.flattenNestedColumnValues, calcNestedKeys(columnNestedPath))(nestedValues);
};
var getCellValues = function (columnNestedPath, columnValues) {
    return columnNestedPath && !(0, ramda_1.isEmpty)(columnValues)
        ? parseNestedColumnValues(columnNestedPath, columnValues)
        : [{ values: (0, mdm_sdk_1.wrapInArrayIfNeeded)(columnValues) }];
};
var buildRowValues = function (rowValue) {
    return (0, ramda_1.reduce)(function (acc, _a) {
        var id = _a.id, nestedPath = _a.nestedPath;
        return (0, ramda_1.assoc)(id, getCellValues(nestedPath, rowValue[id]), acc);
    }, {});
};
var areNestedValuesExceedingLimit = function (maxValuesCount) {
    return function (_a) {
        var indexPath = _a.indexPath;
        return indexPath && indexPath.some(function (index) { return index > maxValuesCount - 1; });
    };
};
var getHiddenValues = function (rowValues, maxValuesCount) {
    if (maxValuesCount === void 0) { maxValuesCount = Infinity; }
    return rowValues.reduce(function (hiddenValues, _a) {
        var _b = _a.values, values = _b === void 0 ? [] : _b, indexPath = _a.indexPath;
        var newHiddenValues = areNestedValuesExceedingLimit(maxValuesCount)({ indexPath: indexPath })
            ? values
            : values.slice(maxValuesCount);
        return hiddenValues.concat(newHiddenValues);
    }, []);
};
var adjustFirstWithHiddenValues = function (maxValuesCount) {
    if (maxValuesCount === void 0) { maxValuesCount = Infinity; }
    return function (rowValues) {
        var hiddenValues = getHiddenValues(rowValues, maxValuesCount);
        return (0, ramda_1.adjust)(0, (0, ramda_1.assoc)('hiddenValues', hiddenValues), rowValues);
    };
};
var applyDefaultValuesCountConstraint = function (maxValuesCount) {
    return (0, ramda_1.unless)(function () { return maxValuesCount === Infinity; }, (0, ramda_1.map)((0, ramda_1.pipe)(adjustFirstWithHiddenValues(maxValuesCount), (0, ramda_1.reject)(areNestedValuesExceedingLimit(maxValuesCount)), (0, ramda_1.map)((0, ramda_1.evolve)({
        values: (0, ramda_1.slice)(0, maxValuesCount)
    })))));
};
var defaultGetIdFromRowValue = function (_, rowIndex) { return rowIndex; };
var getTableRowsData = function (rowValues, columnsData, renderRowCell, getRowCellHeight, getRowMaxValuesCount, getIdFromRowValue, applyMaxValuesCountConstraint) {
    if (getRowMaxValuesCount === void 0) { getRowMaxValuesCount = (0, ramda_1.always)(Infinity); }
    if (getIdFromRowValue === void 0) { getIdFromRowValue = defaultGetIdFromRowValue; }
    applyMaxValuesCountConstraint = applyMaxValuesCountConstraint || applyDefaultValuesCountConstraint;
    return rowValues.map(function (rowValue, rowIndex) {
        return (0, ramda_1.pipe)(buildRowValues(rowValue), applyMaxValuesCountConstraint(getRowMaxValuesCount(rowIndex)), calcRowSpans, calcHeights(columnsData, getRowCellHeight, rowIndex, rowValue), applyCellRenderer(columnsData, renderRowCell, rowIndex, rowValue), function (val) { return ({ data: val, id: getIdFromRowValue(rowValue, rowIndex) }); })(columnsData);
    });
};
exports.getTableRowsData = getTableRowsData;
var calcMaxValuesCount = (0, ramda_1.reduce)(function (maxCount, cellValues) {
    var maxSubNestedValuesCount = (0, ramda_1.pipe)((0, ramda_1.chain)((0, ramda_1.propOr)([], 'indexPath')), (0, ramda_1.map)((0, ramda_1.add)(1)), (0, ramda_1.reduce)(ramda_1.max, 0))(cellValues);
    var maxPlainValuesCount = (0, ramda_1.pipe)((0, ramda_1.pluck)('values'), (0, ramda_1.pluck)('length'), (0, ramda_1.reduce)(ramda_1.max, 0))(cellValues);
    return Math.max(maxCount, maxSubNestedValuesCount, maxPlainValuesCount);
}, 0);
var getMaxRowValuesCount = function (columnsData, rowValue) {
    return (0, ramda_1.pipe)(buildRowValues(rowValue), ramda_1.values, calcMaxValuesCount)(columnsData);
};
exports.getMaxRowValuesCount = getMaxRowValuesCount;
var getHasHiddenAttributes = (0, ramda_1.pipe)(ramda_1.values, (0, ramda_1.chain)(ramda_1.identity), (0, ramda_1.chain)((0, ramda_1.pathOr)([], ['hiddenValues'])), ramda_1.length, (0, ramda_1.gt)(ramda_1.__, 0));
var getIsRowCollapsibleFromMaxValuesCount = function (columnsData, rowValue, maxValuesCount, applyMaxValuesCountConstraint) {
    if (applyMaxValuesCountConstraint) {
        return (0, ramda_1.pipe)(buildRowValues(rowValue), applyMaxValuesCountConstraint(maxValuesCount), getHasHiddenAttributes)(columnsData);
    }
    return getMaxRowValuesCount(columnsData, rowValue) > maxValuesCount;
};
exports.getIsRowCollapsibleFromMaxValuesCount = getIsRowCollapsibleFromMaxValuesCount;
