var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
define(["require", "exports", 'react', './Group', '../../utilities/css', '../../List', '../../utilities/selection/index', '../../utilities/object', './GroupedList.scss'], function (require, exports, React, Group_1, css_1, List_1, index_1, object_1) {
    "use strict";
    var GroupedList = (function (_super) {
        __extends(GroupedList, _super);
        function GroupedList(props) {
            _super.call(this, props);
            this._onToggleCollapse = this._onToggleCollapse.bind(this);
            this._onToggleSelectGroup = this._onToggleSelectGroup.bind(this);
            this._onToggleSummarize = this._onToggleSummarize.bind(this);
            this._getGroupKey = this._getGroupKey.bind(this);
            this._renderGroup = this._renderGroup.bind(this);
            this.state = {
                lastWidth: 0,
                groups: props.groups
            };
        }
        GroupedList.prototype.componentWillReceiveProps = function (newProps) {
            var _a = this.props, groups = _a.groups, selectionMode = _a.selectionMode;
            var shouldForceUpdates = false;
            if (newProps.groups !== groups) {
                this.setState({ groups: newProps.groups });
                shouldForceUpdates = true;
            }
            if (newProps.selectionMode !== selectionMode) {
                shouldForceUpdates = true;
            }
            if (shouldForceUpdates) {
                this._forceListUpdates();
            }
        };
        GroupedList.prototype.render = function () {
            var className = this.props.className;
            var groups = this.state.groups;
            return (React.createElement("div", {ref: 'root', className: css_1.css('ms-GroupedList', className), "data-automationid": 'GroupedList', "data-is-scrollable": 'false', role: 'grid'}, !groups ?
                this._renderGroup(null, 0) : (React.createElement(List_1.List, {ref: 'list', items: groups, onRenderCell: this._renderGroup, getItemCountForPage: function () { return 1; }}))));
        };
        GroupedList.prototype.forceUpdate = function () {
            _super.prototype.forceUpdate.call(this);
            this._forceListUpdates();
        };
        GroupedList.prototype._renderGroup = function (group, groupIndex) {
            var _a = this.props, dragDropEvents = _a.dragDropEvents, dragDropHelper = _a.dragDropHelper, eventsToRegister = _a.eventsToRegister, groupProps = _a.groupProps, items = _a.items, listProps = _a.listProps, onRenderCell = _a.onRenderCell, selectionMode = _a.selectionMode, selection = _a.selection, viewport = _a.viewport;
            // override group header/footer props as needed
            var headerProps = object_1.assign({}, groupProps && groupProps.headerProps ? groupProps.headerProps : {}, {
                onToggleCollapse: this._onToggleCollapse,
                onToggleSelectGroup: this._onToggleSelectGroup
            });
            var footerProps = object_1.assign({}, groupProps && groupProps.footerProps ? groupProps.footerProps : {}, {
                onToggleSummarize: this._onToggleSummarize
            });
            var groupNestingDepth = this._getGroupNestingDepth();
            return (!group || group.count > 0) ? (React.createElement(Group_1.Group, {ref: 'group_' + groupIndex, key: this._getGroupKey(group), dragDropEvents: dragDropEvents, dragDropHelper: dragDropHelper, eventsToRegister: eventsToRegister, footerProps: footerProps, getGroupItemLimit: groupProps && groupProps.getGroupItemLimit, group: group, groupIndex: groupIndex, groupNestingDepth: groupNestingDepth, headerProps: headerProps, listProps: listProps, items: items, onRenderCell: onRenderCell, selectionMode: selectionMode, selection: selection, viewport: viewport})) : null;
        };
        GroupedList.prototype._getGroupKey = function (group) {
            return 'group-' + (group ?
                group.key + '-' + group.count :
                '');
        };
        GroupedList.prototype._getGroupNestingDepth = function () {
            var groups = this.state.groups;
            var level = 0;
            var groupsInLevel = groups;
            while (groupsInLevel && groupsInLevel.length > 0) {
                level++;
                groupsInLevel = groupsInLevel[0].children;
            }
            return level;
        };
        GroupedList.prototype._onToggleCollapse = function (group) {
            var groupProps = this.props.groupProps;
            var onToggleCollapse = groupProps && groupProps.headerProps && groupProps.headerProps.onToggleCollapse;
            if (group) {
                if (onToggleCollapse) {
                    onToggleCollapse(group);
                }
                group.isCollapsed = !group.isCollapsed;
                this.setState({}, this.forceUpdate);
            }
        };
        GroupedList.prototype._onToggleSelectGroup = function (group) {
            var groups = this.state.groups;
            if (group) {
                var isSelected = !group.isSelected;
                this._selectGroup(group, isSelected);
                this.setState({
                    groups: groups
                });
            }
        };
        GroupedList.prototype._selectGroup = function (group, isSelected) {
            var _this = this;
            var groupProps = this.props.groupProps;
            group.isSelected = isSelected;
            if (group.children && group.children.length > 0) {
                group.children.forEach(function (childGroup) {
                    _this._selectGroup(childGroup, isSelected);
                });
            }
            else {
                var getGroupItemLimit = groupProps && groupProps.getGroupItemLimit;
                var groupItemLimit = getGroupItemLimit ? getGroupItemLimit(group) : Infinity;
                var start = group.startIndex;
                var end = group.startIndex + Math.min(group.count, groupItemLimit);
                for (var idx = start; idx < end; idx++) {
                    this.props.selection.setIndexSelected(idx, isSelected, false /* shouldAnchor */);
                }
                this.setState({}, this.forceUpdate);
            }
        };
        GroupedList.prototype._forceListUpdates = function (groups) {
            groups = groups || this.state.groups;
            var groupCount = groups ? groups.length : 1;
            if (this.refs.list) {
                this.refs.list.forceUpdate();
                for (var i = 0; i < groupCount; i++) {
                    var group = this.refs.list.refs['group_' + String(i)];
                    if (group) {
                        group.forceListUpdate();
                    }
                }
            }
            else {
                var group = this.refs['group_' + String(0)];
                if (group) {
                    group.forceListUpdate();
                }
            }
        };
        GroupedList.prototype._onToggleSummarize = function (group) {
            var groups = this.state.groups;
            var groupProps = this.props.groupProps;
            var onToggleSummarize = groupProps && groupProps.footerProps && groupProps.footerProps.onToggleSummarize;
            if (onToggleSummarize) {
                onToggleSummarize(group);
            }
            else {
                if (group) {
                    group.isShowingAll = !group.isShowingAll;
                    this.setState({
                        groups: groups
                    });
                }
            }
        };
        GroupedList.defaultProps = {
            selectionMode: index_1.SelectionMode.multiple,
            isHeaderVisible: true
        };
        return GroupedList;
    }(React.Component));
    exports.GroupedList = GroupedList;
});
