import React from 'react';
import { DefaultCellValueRenderer } from '../../DefaultCellValueRenderer';
import { getTableRowsData, getMaxRowValuesCount } from './dataHelpers';
describe('dataHelpers', function () {
    describe('getTableRowsData', function () {
        it('should pick values by column ids from row values and apply cell renderer to them', function () {
            var rowValues = [
                { a: 1, c: 1 },
                { a: 2, b: 2 }
            ];
            var columnsData = [
                { id: 'a', label: 'columnA' },
                { id: 'b', label: 'columnB' }
            ];
            var renderRowCell = jest.fn(function (_a) {
                var cell = _a.cell;
                return "rendered-".concat(cell.values);
            });
            expect(getTableRowsData(rowValues, columnsData, renderRowCell)).toEqual([
                { data: { a: [{ values: 'rendered-1' }], b: [{ values: 'rendered-' }] }, id: 0 },
                { data: { a: [{ values: 'rendered-2' }], b: [{ values: 'rendered-2' }] }, id: 1 }
            ]);
            expect(renderRowCell.mock.calls).toEqual([
                [
                    {
                        columnIndex: 0,
                        columnData: columnsData[0],
                        rowIndex: 0,
                        rowValue: rowValues[0],
                        cellIndex: 0,
                        cell: { values: [1] },
                        CellValueRenderer: DefaultCellValueRenderer
                    }
                ],
                [
                    {
                        columnIndex: 1,
                        columnData: columnsData[1],
                        rowIndex: 0,
                        rowValue: rowValues[0],
                        cellIndex: 0,
                        cell: { values: [undefined] },
                        CellValueRenderer: DefaultCellValueRenderer
                    }
                ],
                [
                    {
                        columnIndex: 0,
                        columnData: columnsData[0],
                        rowIndex: 1,
                        rowValue: rowValues[1],
                        cellIndex: 0,
                        cell: { values: [2] },
                        CellValueRenderer: DefaultCellValueRenderer
                    }
                ],
                [
                    {
                        columnIndex: 1,
                        columnData: columnsData[1],
                        rowIndex: 1,
                        rowValue: rowValues[1],
                        cellIndex: 0,
                        cell: { values: [2] },
                        CellValueRenderer: DefaultCellValueRenderer
                    }
                ]
            ]);
        });
        it('should use custom cell value renderers from columns data if they are provided', function () {
            var rowValues = [{ a: 1, b: 1 }];
            var ColumnARenderer = function (_a) {
                var value = _a.value;
                return "columnA-".concat(value);
            };
            var ColumnBRenderer = function (_a) {
                var value = _a.value;
                return "columnB-".concat(value);
            };
            var columnsData = [
                { id: 'a', label: 'columnA', rowCellValueRenderer: ColumnARenderer },
                { id: 'b', label: 'columnB', rowCellValueRenderer: ColumnBRenderer }
            ];
            var renderRowCell = jest.fn(function (_a) {
                var cell = _a.cell, CellValueRenderer = _a.CellValueRenderer;
                return cell.values.map(function (value, index) { return React.createElement(CellValueRenderer, { key: index, value: value }); });
            });
            expect(getTableRowsData(rowValues, columnsData, renderRowCell)).toEqual([
                {
                    data: {
                        a: [{ values: [React.createElement(ColumnARenderer, { key: 0, value: 1 })] }],
                        b: [{ values: [React.createElement(ColumnBRenderer, { key: 0, value: 1 })] }]
                    },
                    id: 0
                }
            ]);
            expect(renderRowCell.mock.calls).toEqual([
                [
                    {
                        columnIndex: 0,
                        columnData: columnsData[0],
                        rowIndex: 0,
                        rowValue: rowValues[0],
                        cellIndex: 0,
                        cell: { values: [1] },
                        CellValueRenderer: ColumnARenderer
                    }
                ],
                [
                    {
                        columnIndex: 1,
                        columnData: columnsData[1],
                        rowIndex: 0,
                        rowValue: rowValues[0],
                        cellIndex: 0,
                        cell: { values: [1] },
                        CellValueRenderer: ColumnBRenderer
                    }
                ]
            ]);
        });
        it('should calculate height for each cell row if getRowCellHeight property is provided', function () {
            var rowValues = [
                { a: [1, 2], c: 1 },
                { a: 3, b: 2 }
            ];
            var columnsData = [
                { id: 'a', label: 'columnA' },
                { id: 'b', label: 'columnB' }
            ];
            var renderRowCell = function (_a) {
                var cell = _a.cell;
                return cell.values;
            };
            var getRowHeight = jest.fn(function (_a) {
                var cell = _a.cell;
                return cell.values.length * 20;
            });
            expect(getTableRowsData(rowValues, columnsData, renderRowCell, getRowHeight)).toEqual([
                { data: { a: [{ values: [1, 2], height: 40 }], b: [{ values: [undefined], height: 20 }] }, id: 0 },
                { data: { a: [{ values: [3], height: 20 }], b: [{ values: [2], height: 20 }] }, id: 1 }
            ]);
            expect(getRowHeight.mock.calls).toEqual([
                [
                    {
                        columnIndex: 0,
                        columnData: columnsData[0],
                        rowIndex: 0,
                        rowValue: rowValues[0],
                        cellIndex: 0,
                        cell: { values: [1, 2] }
                    }
                ],
                [
                    {
                        columnIndex: 1,
                        columnData: columnsData[1],
                        rowIndex: 0,
                        rowValue: rowValues[0],
                        cellIndex: 0,
                        cell: { values: [undefined] }
                    }
                ],
                [
                    {
                        columnIndex: 0,
                        columnData: columnsData[0],
                        rowIndex: 1,
                        rowValue: rowValues[1],
                        cellIndex: 0,
                        cell: { values: [3] }
                    }
                ],
                [
                    {
                        columnIndex: 1,
                        columnData: columnsData[1],
                        rowIndex: 1,
                        rowValue: rowValues[1],
                        cellIndex: 0,
                        cell: { values: [2] }
                    }
                ]
            ]);
        });
        it('should calculate row spans if columns data contains nested paths', function () {
            var rowValues = [
                {
                    'n1.a': [[1, 2], [3]],
                    c: 1,
                    'n1.n2.b': [[], [[5], [6]]],
                    'n1.d': []
                }
            ];
            var columnsData = [
                { id: 'n1.a', nestedPath: ['n1'], label: 'columnA' },
                { id: 'n1.n2.b', nestedPath: ['n1', 'n2'], label: 'columnB' },
                { id: 'c', label: 'columnC' },
                { id: 'n1.d', nestedPath: ['n1'], label: 'columnD' }
            ];
            var renderRowCell = function (_a) {
                var cell = _a.cell;
                return cell.values;
            };
            expect(getTableRowsData(rowValues, columnsData, renderRowCell)).toEqual([
                {
                    data: {
                        'n1.a': [
                            { values: [1, 2], indexPath: [0], nestedKey: 'n1.0', rowSpan: 1 },
                            { values: [3], indexPath: [1], nestedKey: 'n1.1', rowSpan: 2 }
                        ],
                        'n1.n2.b': [
                            { values: [], indexPath: [0], nestedKey: 'n1.0', rowSpan: 1 },
                            { values: [5], indexPath: [1, 0], nestedKey: 'n1.1.n2.0', rowSpan: 1 },
                            { values: [6], indexPath: [1, 1], nestedKey: 'n1.1.n2.1', rowSpan: 1 }
                        ],
                        c: [{ values: [1], rowSpan: 3 }],
                        'n1.d': [{ values: [], rowSpan: 3 }]
                    },
                    id: 0
                }
            ]);
        });
        it('should calculate correct rowspan if there are two or more columns having same nesting level', function () {
            var rowValues = [
                {
                    'n1.a.b': [[0], [1]],
                    'n1.x.y': [[2, 3], [4, 5], [6]],
                    'n1.x.y.z': [[[7], [8]], [[9], [10]], []]
                }
            ];
            var columnsData = [
                { id: 'n1.a.b', nestedPath: ['a'], label: 'columnA' },
                { id: 'n1.x.y', nestedPath: ['x'], label: 'columnB' },
                { id: 'n1.x.y.z', nestedPath: ['x', 'y'], label: 'columnC' }
            ];
            var renderRowCell = function (_a) {
                var cell = _a.cell;
                return cell.values;
            };
            expect(getTableRowsData(rowValues, columnsData, renderRowCell)).toEqual([
                {
                    data: {
                        'n1.a.b': [
                            { values: [0], indexPath: [0], nestedKey: 'a.0', rowSpan: 2 },
                            { values: [1], indexPath: [1], nestedKey: 'a.1', rowSpan: 3 }
                        ],
                        'n1.x.y': [
                            { values: [2, 3], indexPath: [0], nestedKey: 'x.0', rowSpan: 2 },
                            { values: [4, 5], indexPath: [1], nestedKey: 'x.1', rowSpan: 2 },
                            { values: [6], indexPath: [2], nestedKey: 'x.2', rowSpan: 1 }
                        ],
                        'n1.x.y.z': [
                            { values: [7], indexPath: [0, 0], nestedKey: 'x.0.y.0', rowSpan: 1 },
                            { values: [8], indexPath: [0, 1], nestedKey: 'x.0.y.1', rowSpan: 1 },
                            { values: [9], indexPath: [1, 0], nestedKey: 'x.1.y.0', rowSpan: 1 },
                            { values: [10], indexPath: [1, 1], nestedKey: 'x.1.y.1', rowSpan: 1 },
                            { values: [], indexPath: [2], nestedKey: 'x.2', rowSpan: 1 }
                        ]
                    },
                    id: 0
                }
            ]);
        });
        it('should apply max values count constraint if getRowMaxValuesCount is provided', function () {
            var rowValues = [
                {
                    'n1.a': [[1, 2], [3]],
                    c: [7, 8, 9, 10],
                    'n1.n2.b': [[], [[4, 5], [6]]],
                    'n1.d': []
                }
            ];
            var columnsData = [
                { id: 'n1.a', nestedPath: ['n1'], label: 'columnA' },
                { id: 'n1.n2.b', nestedPath: ['n1', 'n2'], label: 'columnB' },
                { id: 'c', label: 'columnC' },
                { id: 'n1.d', nestedPath: ['n1'], label: 'columnD' }
            ];
            var renderRowCell = function (_a) {
                var cell = _a.cell;
                return cell.values;
            };
            var getRowMaxValuesCount = jest.fn(function () { return 1; });
            expect(getTableRowsData(rowValues, columnsData, renderRowCell, null, getRowMaxValuesCount)).toEqual([
                {
                    data: {
                        'n1.a': [
                            {
                                hiddenValues: [2, 3],
                                values: [1],
                                indexPath: [0],
                                nestedKey: 'n1.0',
                                rowSpan: 1
                            }
                        ],
                        'n1.n2.b': [
                            { values: [], hiddenValues: [4, 5, 6], indexPath: [0], nestedKey: 'n1.0', rowSpan: 1 }
                        ],
                        c: [{ hiddenValues: [8, 9, 10], values: [7], rowSpan: 1 }],
                        'n1.d': [{ values: [], hiddenValues: [], rowSpan: 1 }]
                    },
                    id: 0
                }
            ]);
            expect(getRowMaxValuesCount).toHaveBeenCalledWith(0);
        });
        it('should apply hiddenValues correctly if maxValuesCount more than 1', function () {
            var rowValues = [
                {
                    'n1.n2.a': [[[1, 2]], [[3, 4, 5]]]
                }
            ];
            var columnsData = [{ id: 'n1.n2.a', nestedPath: ['n1', 'n2', 'a'], label: 'columnA' }];
            var renderRowCell = function (_a) {
                var cell = _a.cell;
                return cell.values;
            };
            var getRowMaxValuesCount = jest.fn(function () { return 2; });
            expect(getTableRowsData(rowValues, columnsData, renderRowCell, null, getRowMaxValuesCount)).toEqual([
                {
                    data: {
                        'n1.n2.a': [
                            {
                                values: [1, 2],
                                hiddenValues: [5],
                                indexPath: [0, 0],
                                nestedKey: 'n1.0.n2.0',
                                rowSpan: 1
                            },
                            { values: [3, 4], indexPath: [1, 0], nestedKey: 'n1.1.n2.0', rowSpan: 1 }
                        ]
                    },
                    id: 0
                }
            ]);
            expect(getRowMaxValuesCount).toHaveBeenCalledWith(0);
        });
        it('should generate id for each row from rowValue if getIdFromRowValue is provided', function () {
            var rowValues = [
                { a: 1, b: 1 },
                { a: 2, b: 2 },
                { a: 3, b: 3 }
            ];
            var columnsData = [
                { id: 'a', label: 'columnA' },
                { id: 'b', label: 'columnB' }
            ];
            var getRowCellHeight = function () { return 50; };
            var renderRowCell = function (_a) {
                var cell = _a.cell;
                return "rendered-".concat(cell.values);
            };
            var getRowMaxValuesCount = function () { return Infinity; };
            var getIdFromRowValue = function (rowValue) { return "custom-id-".concat(rowValue.a); };
            var tableRowsData = getTableRowsData(rowValues, columnsData, renderRowCell, getRowCellHeight, getRowMaxValuesCount, getIdFromRowValue);
            expect(tableRowsData).toEqual([
                {
                    data: { a: [{ values: 'rendered-1', height: 50 }], b: [{ values: 'rendered-1', height: 50 }] },
                    id: 'custom-id-1'
                },
                {
                    data: { a: [{ values: 'rendered-2', height: 50 }], b: [{ values: 'rendered-2', height: 50 }] },
                    id: 'custom-id-2'
                },
                {
                    data: { a: [{ values: 'rendered-3', height: 50 }], b: [{ values: 'rendered-3', height: 50 }] },
                    id: 'custom-id-3'
                }
            ]);
        });
    });
    describe('getMaxRowValuesCount', function () {
        it('should return correct count for plain data', function () {
            var columnsData = [
                { id: 'a', label: 'columnA' },
                { id: 'b', label: 'columnB' }
            ];
            var rowValue = {
                a: [1, 2, 3],
                b: 2
            };
            expect(getMaxRowValuesCount(columnsData, rowValue)).toBe(3);
        });
        it('should return correct count for nested data', function () {
            var columnsData = [
                { id: 'n1.a', nestedPath: ['n1'], label: 'columnA' },
                { id: 'n1.n2.b', nestedPath: ['n1', 'n2'], label: 'columnB' },
                { id: 'c', label: 'columnC' },
                { id: 'n1.d', nestedPath: ['n1'], label: 'columnD' }
            ];
            var rowValue = {
                'n1.a': [[1, 2], [3]],
                c: [7, 8],
                'n1.n2.b': [[], [[4, 5], [6], [9]]],
                'n1.d': []
            };
            expect(getMaxRowValuesCount(columnsData, rowValue)).toBe(3);
        });
    });
});
