"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 });
var nestedHelpers_1 = require("./nestedHelpers");
var nestedData = function (values) {
    var indexPath = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        indexPath[_i - 1] = arguments[_i];
    }
    return ({ values: values, indexPath: indexPath });
};
describe('nested table api', function () {
    describe('flattenNestedColumnValues', function () {
        it('should work correctly with simple nested array', function () {
            var data = [[1]];
            expect((0, nestedHelpers_1.flattenNestedColumnValues)(data)).toEqual([nestedData([1], 0)]);
        });
        it('should work correctly with long simple nested array', function () {
            var data = [[1, 2], [], [5, 6]];
            expect((0, nestedHelpers_1.flattenNestedColumnValues)(data)).toEqual([
                nestedData([1, 2], 0),
                nestedData([], 1),
                nestedData([5, 6], 2)
            ]);
        });
        it('should work correctly with complex nested array', function () {
            var data = [[[1, 2], []], [[]]];
            expect((0, nestedHelpers_1.flattenNestedColumnValues)(data)).toEqual([
                nestedData([1, 2], 0, 0),
                nestedData([], 0, 1),
                nestedData([], 1, 0)
            ]);
        });
        it('should work correctly with more complex nested array', function () {
            var data = [[[], [[], [5, 6]]], [[[7, 8], []]]];
            expect((0, nestedHelpers_1.flattenNestedColumnValues)(data)).toEqual([
                nestedData([], 0, 0),
                nestedData([], 0, 1, 0),
                nestedData([5, 6], 0, 1, 1),
                nestedData([7, 8], 1, 0, 0),
                nestedData([], 1, 0, 1)
            ]);
        });
    });
    describe('getNestedKeyForNestedColumnCell', function () {
        it('should zip column nested path and value index path', function () {
            var columnNestedPath = ['a', 'b', 'c'];
            var value = { indexPath: [0, 0, 1] };
            expect((0, nestedHelpers_1.getNestedKeyForNestedColumnCell)(columnNestedPath, value)).toEqual('a.0.b.0.c.1');
        });
        it('should truncate key by value index path', function () {
            var columnNestedPath = ['a', 'b', 'c'];
            var value = { indexPath: [0, 0] };
            expect((0, nestedHelpers_1.getNestedKeyForNestedColumnCell)(columnNestedPath, value)).toEqual('a.0.b.0');
        });
    });
    describe('getSiblingSubRowsKeyBySubRowKey', function () {
        it('should discard index from nested key tail', function () {
            expect((0, nestedHelpers_1.getSiblingSubRowsKeyBySubRowKey)('NestedAttr.0')).toEqual('NestedAttr');
        });
    });
    describe('getParentRowKeyBySiblingsSubRowsKey', function () {
        it('should return "total" key for topmost sibling subRows key', function () {
            expect((0, nestedHelpers_1.getParentRowKeyBySiblingsSubRowsKey)('NestedAttr')).toEqual(nestedHelpers_1.TOTAL_ROW_SPAN_KEY);
        });
        it('should return parent row key for all subRows key', function () {
            expect((0, nestedHelpers_1.getParentRowKeyBySiblingsSubRowsKey)('NestedAttr1.0.NestedAttr1')).toEqual('NestedAttr1.0');
        });
    });
    describe('validateRowSpanData', function () {
        it('should return correct rowSpan data for sorted nested keys', function () {
            var _a, _b;
            var rowSpanData = (_a = {
                    'c.0': 1
                },
                _a[nestedHelpers_1.TOTAL_ROW_SPAN_KEY] = 3,
                _a['a.0'] = 1,
                _a['a.0.b.0'] = 1,
                _a['d.0'] = 1,
                _a['d.1'] = 1,
                _a['d.2'] = 1,
                _a);
            var validatedData = (_b = {
                    'c.0': 3
                },
                _b[nestedHelpers_1.TOTAL_ROW_SPAN_KEY] = 3,
                _b['a.0.b.0'] = 3,
                _b['a.0'] = 3,
                _b['d.0'] = 1,
                _b['d.1'] = 1,
                _b['d.2'] = 1,
                _b);
            expect((0, nestedHelpers_1.validateRowSpanData)(rowSpanData)).toEqual(validatedData);
            expect((0, nestedHelpers_1.validateRowSpanData)(__assign(__assign({}, rowSpanData), { 'a.0.b.1': 1 }))).toEqual(__assign(__assign({}, validatedData), { 'a.0.b.0': 1, 'a.0.b.1': 2 }));
        });
        it('should return correct rowSpan data for unsorted nested keys', function () {
            var _a, _b;
            var rowSpanData = (_a = {
                    'c.0': 1
                },
                _a[nestedHelpers_1.TOTAL_ROW_SPAN_KEY] = 3,
                _a['a.0.b.0'] = 1,
                _a['a.0'] = 1,
                _a['d.0'] = 1,
                _a['d.1'] = 1,
                _a['d.2'] = 1,
                _a);
            var validatedData = (_b = {
                    'c.0': 3
                },
                _b[nestedHelpers_1.TOTAL_ROW_SPAN_KEY] = 3,
                _b['a.0.b.0'] = 3,
                _b['a.0'] = 3,
                _b['d.0'] = 1,
                _b['d.1'] = 1,
                _b['d.2'] = 1,
                _b);
            expect((0, nestedHelpers_1.validateRowSpanData)(rowSpanData)).toEqual(validatedData);
            expect((0, nestedHelpers_1.validateRowSpanData)(__assign(__assign({}, rowSpanData), { 'a.0.b.1': 1 }))).toEqual(__assign(__assign({}, validatedData), { 'a.0.b.0': 1, 'a.0.b.1': 2 }));
        });
    });
    describe('calcRowSpansForNestedKeys', function () {
        it('should build correct rowSpansData for simple nested keys', function () {
            var _a;
            var nestedKeys = ['a.0', 'a.1', 'a.2'];
            var rowSpansData = (_a = {
                    'a.0': 1,
                    'a.1': 1,
                    'a.2': 1
                },
                _a[nestedHelpers_1.TOTAL_ROW_SPAN_KEY] = 3,
                _a);
            expect((0, nestedHelpers_1.calcRowSpansForNestedKeys)(nestedKeys)).toEqual(rowSpansData);
        });
        it('should build correct rowSpansData for simple nested keys with different heights', function () {
            var nestedKeys = JSON.parse("[\n                \"a.0\",  \"b.0\",\n                \"a.1\",  \"b.1\",\n                \"a.2\",  \"b.2\",\n                        \"b.3\", \n                        \"b.4\"\n            ]");
            var rowSpansData = JSON.parse("{\n                \"a.0\": 1,  \"b.0\": 1,\n                \"a.1\": 1,  \"b.1\": 1,\n                \"a.2\": 3,  \"b.2\": 1,\n                           \"b.3\": 1, \n                           \"b.4\": 1,\n                \"".concat(nestedHelpers_1.TOTAL_ROW_SPAN_KEY, "\": 5\n            }"));
            expect((0, nestedHelpers_1.calcRowSpansForNestedKeys)(nestedKeys)).toEqual(rowSpansData);
        });
        it('should build correct rowSpansData for complex nested keys', function () {
            var nestedKeys = JSON.parse("[\n                \"a.0\",  \"a.0.b.0\",  \"a.0.c.0\",\n                        \"a.0.b.1\",  \n                \"a.1\",  \"a.1.b.0\",  \"a.1.c.0\",\n                        \"a.1.b.1\",\n                        \"a.1.b.2\"\n            ]");
            var rowSpansData = JSON.parse("{\n                \"a.0\": 2,  \"a.0.b.0\": 1,  \"a.0.c.0\": 2,\n                           \"a.0.b.1\": 1,  \n                \"a.1\": 3,  \"a.1.b.0\": 1,  \"a.1.c.0\": 3,\n                           \"a.1.b.1\": 1,\n                           \"a.1.b.2\": 1,\n                \"".concat(nestedHelpers_1.TOTAL_ROW_SPAN_KEY, "\": 5\n            }"));
            expect((0, nestedHelpers_1.calcRowSpansForNestedKeys)(nestedKeys)).toEqual(rowSpansData);
        });
        it('should build correct rowSpansData for more complex nested keys', function () {
            var nestedKeys = JSON.parse("[\n                \"a.0\",  \"a.0.b.0\",  \"a.0.c.0\",  \"a.0.b.0.d.0\",\n                                                \"a.0.b.0.d.1\",\n                        \"a.0.b.1\",                           \n                \"a.1\",  \"a.1.b.0\",  \"a.1.c.0\",  \"a.1.b.0.d.0\",\n                        \"a.1.b.1\",                           \n                        \"a.1.b.2\",              \"a.1.b.2.d.0\",\n                                                \"a.1.b.2.d.1\"\n            ]");
            var rowSpansData = JSON.parse("{\n                \"a.0\": 3,  \"a.0.b.0\": 2,  \"a.0.c.0\": 3,  \"a.0.b.0.d.0\": 1,\n                                                         \"a.0.b.0.d.1\": 1,\n                           \"a.0.b.1\": 1,                           \n                \"a.1\": 4,  \"a.1.b.0\": 1,  \"a.1.c.0\": 4,  \"a.1.b.0.d.0\": 1,\n                           \"a.1.b.1\": 1,                           \n                           \"a.1.b.2\": 2,                 \"a.1.b.2.d.0\": 1,\n                                                         \"a.1.b.2.d.1\": 1,\n                \"".concat(nestedHelpers_1.TOTAL_ROW_SPAN_KEY, "\": 7\n            }"));
            expect((0, nestedHelpers_1.calcRowSpansForNestedKeys)(nestedKeys)).toEqual(rowSpansData);
        });
        it('should build correct rowSpansData for very complex nested keys', function () {
            var nestedKeys = JSON.parse("[\n                \"a.0\",  \"a.0.b.0\",  \"a.0.c.0\",  \"a.0.b.0.d.0\",  \"a.0.b.0.d.0.e.0.f.0\",  \"a.0.b.0.d.0.e.0\",\n                                                                \"a.0.b.0.d.0.e.1.f.0\",  \"a.0.b.0.d.0.e.1\",\n                                                                \"a.0.b.0.d.0.e.1.f.1\",\n                                                                \"a.0.b.0.d.0.e.1.f.2\",\n                                                \"a.0.b.0.d.1\",\n                        \"a.0.b.1\",              \"a.0.b.1.d.0\",                          \"a.0.b.1.d.0.e.0\",\n                                                                                        \"a.0.b.1.d.0.e.1\",\n                                                                                        \"a.0.b.1.d.0.e.2\",\n                \"a.1\",  \"a.1.b.0\",  \"a.1.c.0\",  \"a.1.b.0.d.0\",                          \"a.1.b.0.d.0.e.0\",\n                        \"a.1.b.1\",                            \n                        \"a.1.b.2\",              \"a.1.b.2.d.0\",                          \"a.1.b.2.d.0.e.0\",\n                                                                                        \"a.1.b.2.d.0.e.1\",\n                                                \"a.1.b.2.d.1\"\n            ]");
            var rowSpansData = JSON.parse("{\n                \"a.0\": 8,  \"a.0.b.0\": 5,  \"a.0.c.0\": 8,  \"a.0.b.0.d.0\": 4,  \"a.0.b.0.d.0.e.0.f.0\": 1,  \"a.0.b.0.d.0.e.0\": 1,\n                                                                            \"a.0.b.0.d.0.e.1.f.0\": 1,  \"a.0.b.0.d.0.e.1\": 3,\n                                                                            \"a.0.b.0.d.0.e.1.f.1\": 1,\n                                                                            \"a.0.b.0.d.0.e.1.f.2\": 1,\n                                                         \"a.0.b.0.d.1\": 1,\n                           \"a.0.b.1\": 3,                 \"a.0.b.1.d.0\": 3,                             \"a.0.b.1.d.0.e.0\": 1,\n                                                                                                       \"a.0.b.1.d.0.e.1\": 1,\n                                                                                                       \"a.0.b.1.d.0.e.2\": 1,\n                \"a.1\": 5,  \"a.1.b.0\": 1,  \"a.1.c.0\": 5,  \"a.1.b.0.d.0\": 1,                             \"a.1.b.0.d.0.e.0\": 1,\n                           \"a.1.b.1\": 1,                            \n                           \"a.1.b.2\": 3,                 \"a.1.b.2.d.0\": 2,                             \"a.1.b.2.d.0.e.0\": 1,\n                                                                                                       \"a.1.b.2.d.0.e.1\": 1,\n                                                         \"a.1.b.2.d.1\": 1,\n            \"".concat(nestedHelpers_1.TOTAL_ROW_SPAN_KEY, "\": 13\n            }"));
            expect((0, nestedHelpers_1.calcRowSpansForNestedKeys)(nestedKeys)).toEqual(rowSpansData);
        });
        it('should return same result for unsorted and sorted nested keys', function () {
            var unsortedNestedKeys = [
                'root.0',
                'root.1',
                'root.0.attr.0.Nested Level 1.11.Nested Level 2.0',
                'root.0.attr.0.Nested Level 1.2.Nested Level 2.0',
                'root.0.attr.0.Nested Level 1.0.Nested Level 2.0',
                'root.0.attr.0.Nested Level 1.0.Nested Level 2.1',
                'root.0.attr.0.Nested Level 1.1.Nested Level 2.0',
                'root.1.attr.0.Nested Level 1.0.Nested Level 2.0',
                'root.0.attr.0.Nested Level 1.11',
                'root.0.attr.0.Nested Level 1.2',
                'root.0.attr.0.Nested Level 1.0',
                'root.0.attr.0.Nested Level 1.1',
                'root.1.attr.0.Nested Level 1.0'
            ];
            var sortedNestedKeysRight = [
                'root.0',
                'root.1',
                'root.0.attr.0.Nested Level 1.0',
                'root.0.attr.0.Nested Level 1.1',
                'root.0.attr.0.Nested Level 1.2',
                'root.0.attr.0.Nested Level 1.11',
                'root.1.attr.0.Nested Level 1.0',
                'root.0.attr.0.Nested Level 1.0.Nested Level 2.0',
                'root.0.attr.0.Nested Level 1.0.Nested Level 2.1',
                'root.0.attr.0.Nested Level 1.1.Nested Level 2.0',
                'root.0.attr.0.Nested Level 1.2.Nested Level 2.0',
                'root.0.attr.0.Nested Level 1.11.Nested Level 2.0',
                'root.1.attr.0.Nested Level 1.0.Nested Level 2.0'
            ];
            var rowSpansWrongRight = JSON.parse("{\n                \"root.0\": 5,   \"root.0.attr.0\": 5,  \"root.0.attr.0.Nested Level 1.0\": 2,    \"root.0.attr.0.Nested Level 1.0.Nested Level 2.0\": 1,\n                                                                                            \"root.0.attr.0.Nested Level 1.0.Nested Level 2.1\": 1,\n                                                    \"root.0.attr.0.Nested Level 1.1\": 1,    \"root.0.attr.0.Nested Level 1.1.Nested Level 2.0\": 1,\n                                                    \"root.0.attr.0.Nested Level 1.2\": 1,    \"root.0.attr.0.Nested Level 1.2.Nested Level 2.0\": 1,\n                                                    \"root.0.attr.0.Nested Level 1.11\": 1,   \"root.0.attr.0.Nested Level 1.11.Nested Level 2.0\": 1,\n                \"root.1\": 1,   \"root.1.attr.0\": 1,  \"root.1.attr.0.Nested Level 1.0\": 1,    \"root.1.attr.0.Nested Level 1.0.Nested Level 2.0\": 1,\n                \n            \"".concat(nestedHelpers_1.TOTAL_ROW_SPAN_KEY, "\": 6\n            }"));
            expect((0, nestedHelpers_1.calcRowSpansForNestedKeys)(sortedNestedKeysRight)).toEqual(rowSpansWrongRight);
            expect((0, nestedHelpers_1.calcRowSpansForNestedKeys)(unsortedNestedKeys)).toEqual(rowSpansWrongRight);
        });
    });
});
