"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var WidgetBase_1 = require("@dojo/framework/core/WidgetBase");
var vdom_1 = require("@dojo/framework/core/vdom");
var I18n_1 = require("@dojo/framework/core/mixins/I18n");
var Themed_1 = require("@dojo/framework/core/mixins/Themed");
var PlaceholderRow_1 = require("./PlaceholderRow");
var Row_1 = require("./Row");
var fixedCss = require("./styles/body.m.css");
var css = require("../theme/default/grid-body.m.css");
var diffProperty_1 = require("@dojo/framework/core/decorators/diffProperty");
var diff_1 = require("@dojo/framework/core/diff");
var PaginatedBody = /** @class */ (function (_super) {
    tslib_1.__extends(PaginatedBody, _super);
    function PaginatedBody() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    PaginatedBody.prototype._defaultPlaceholderRow = function (key) {
        var _a = this.properties, classes = _a.classes, theme = _a.theme;
        return vdom_1.w(PlaceholderRow_1.default, { key: key, theme: theme, classes: classes });
    };
    PaginatedBody.prototype._updater = function (rowNumber, columnId, value) {
        var page = Math.max(Math.ceil(rowNumber / this.properties.pageSize), 1);
        var pageItemNumber = rowNumber - (page - 1) * this.properties.pageSize;
        this.properties.updater(page, pageItemNumber, columnId, value);
    };
    PaginatedBody.prototype._onScroll = function (event) {
        var scrollLeft = event.target.scrollLeft;
        this.properties.onScroll(scrollLeft);
    };
    PaginatedBody.prototype._renderRows = function () {
        var _a = this.properties, i18nBundle = _a.i18nBundle, pageSize = _a.pageSize, fetcher = _a.fetcher, pages = _a.pages, columnConfig = _a.columnConfig, _b = _a.placeholderRowRenderer, placeholderRowRenderer = _b === void 0 ? this._defaultPlaceholderRow : _b, pageNumber = _a.pageNumber, theme = _a.theme, classes = _a.classes, columnWidths = _a.columnWidths, onRowSelect = _a.onRowSelect, _c = _a.selectedRows, selectedRows = _c === void 0 ? [] : _c;
        var data = pages["page-" + pageNumber];
        if (!data) {
            fetcher(pageNumber, pageSize);
        }
        var rows = [];
        var rowCount = data ? data.length : pageSize;
        var _loop_1 = function (i) {
            if (!data) {
                rows.push(placeholderRowRenderer(i));
            }
            else {
                rows.push(vdom_1.w(Row_1.default, {
                    id: i,
                    key: i,
                    i18nBundle: i18nBundle,
                    theme: theme,
                    classes: classes,
                    item: data[i],
                    columnConfig: columnConfig,
                    columnWidths: columnWidths,
                    updater: this_1._updater,
                    onRowSelect: onRowSelect
                        ? function (type) {
                            onRowSelect && onRowSelect(i, type);
                        }
                        : undefined,
                    selected: selectedRows.indexOf(i) !== -1
                }));
            }
        };
        var this_1 = this;
        for (var i = 0; i < rowCount; i++) {
            _loop_1(i);
        }
        return rows;
    };
    PaginatedBody.prototype.render = function () {
        var _a = this.properties, height = _a.height, width = _a.width, columnWidths = _a.columnWidths;
        var rowWidth = columnWidths &&
            Object.keys(columnWidths).reduce(function (rowWidth, key) {
                return rowWidth + columnWidths[key];
            }, 0);
        return vdom_1.v('div', {
            key: 'root',
            classes: [this.theme(css.root), fixedCss.rootFixed],
            role: 'rowgroup',
            onscroll: this._onScroll,
            styles: width
                ? { height: height + "px", width: width + "px" }
                : { height: height + "px" }
        }, [
            vdom_1.v('div', { styles: rowWidth ? { width: rowWidth + "px" } : {} }, tslib_1.__spread([
                vdom_1.v('div')
            ], this._renderRows()))
        ]);
    };
    PaginatedBody = tslib_1.__decorate([
        Themed_1.theme(css),
        diffProperty_1.diffProperty('pages', diff_1.reference)
    ], PaginatedBody);
    return PaginatedBody;
}(I18n_1.default(Themed_1.default(WidgetBase_1.default))));
exports.default = PaginatedBody;

/*# sourceMappingURL=PaginatedBody.js.map*/