"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var Utilities_1 = require("../../Utilities");
var OverflowSet_1 = require("../../OverflowSet");
var ResizeGroup_1 = require("../../ResizeGroup");
var FocusZone_1 = require("../../FocusZone");
var Utilities_2 = require("../../Utilities");
var Button_1 = require("../../Button");
var Tooltip_1 = require("../../Tooltip");
var getClassNames = Utilities_2.classNamesFunction();
var CommandBarBase = /** @class */ (function (_super) {
    tslib_1.__extends(CommandBarBase, _super);
    function CommandBarBase() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this._overflowSet = Utilities_2.createRef();
        _this._resizeGroup = Utilities_2.createRef();
        _this._onRenderData = function (data) {
            return (React.createElement(FocusZone_1.FocusZone, { className: Utilities_1.css(_this._classNames.root), direction: FocusZone_1.FocusZoneDirection.horizontal, role: 'menubar', "aria-label": _this.props.ariaLabel },
                React.createElement(OverflowSet_1.OverflowSet, { componentRef: _this._resolveRef('_overflowSet'), className: Utilities_1.css(_this._classNames.primarySet), doNotContainWithinFocusZone: true, role: 'presentation', items: data.primaryItems, overflowItems: data.overflowItems.length ? data.overflowItems : undefined, onRenderItem: _this._onRenderItem, onRenderOverflowButton: _this._onRenderOverflowButton }),
                data.farItems && (React.createElement(OverflowSet_1.OverflowSet, { className: Utilities_1.css(_this._classNames.secondarySet), doNotContainWithinFocusZone: true, role: 'presentation', items: data.farItems, onRenderItem: _this._onRenderItem, onRenderOverflowButton: Utilities_1.nullRender }))));
        };
        _this._onRenderItem = function (item) {
            var CommandButtonType = _this.props.buttonAs || item.commandBarButtonAs || Button_1.CommandBarButton;
            var itemText = item.text || item.name;
            if (item.onRender) {
                // These are the top level items, there is no relevant menu dismissing function to
                // provide for the IContextualMenuItem onRender function. Pass in a no op function instead.
                return item.onRender(item, function () { return undefined; });
            }
            var commandButtonProps = tslib_1.__assign({ allowDisabledFocus: true, role: 'menuitem' }, item, { styles: tslib_1.__assign({ root: { height: '100%' }, label: { whiteSpace: 'nowrap' } }, item.buttonStyles), className: Utilities_1.css('ms-CommandBarItem-link', item.className), text: !item.iconOnly ? itemText : undefined, menuProps: item.subMenuProps, onClick: _this._onButtonClick(item) });
            if (item.iconOnly && itemText !== undefined) {
                return (React.createElement(Tooltip_1.TooltipHost, tslib_1.__assign({ content: itemText }, item.tooltipHostProps),
                    React.createElement(CommandButtonType, tslib_1.__assign({}, commandButtonProps, { defaultRender: Button_1.CommandBarButton }))));
            }
            return React.createElement(CommandButtonType, tslib_1.__assign({}, commandButtonProps, { defaultRender: Button_1.CommandBarButton }));
        };
        _this._onRenderOverflowButton = function (overflowItems) {
            var _a = _this.props, _b = _a.overflowButtonAs, OverflowButtonType = _b === void 0 ? Button_1.CommandBarButton : _b, _c = _a.overflowButtonProps // assure that props is not empty
            , overflowButtonProps = _c === void 0 ? {} : _c // assure that props is not empty
            ;
            var combinedOverflowItems = (overflowButtonProps.menuProps ? overflowButtonProps.menuProps.items : []).concat(overflowItems);
            var overflowProps = tslib_1.__assign({}, overflowButtonProps, { styles: tslib_1.__assign({ menuIcon: { fontSize: '17px' } }, overflowButtonProps.styles), className: Utilities_1.css('ms-CommandBar-overflowButton', overflowButtonProps.className), menuProps: tslib_1.__assign({}, overflowButtonProps.menuProps, { items: combinedOverflowItems }), menuIconProps: tslib_1.__assign({ iconName: 'More' }, overflowButtonProps.menuIconProps) });
            return React.createElement(OverflowButtonType, tslib_1.__assign({}, overflowProps));
        };
        _this._onReduceData = function (data) {
            var _a = _this.props, shiftOnReduce = _a.shiftOnReduce, onDataReduced = _a.onDataReduced;
            var primaryItems = data.primaryItems, overflowItems = data.overflowItems, cacheKey = data.cacheKey;
            // Use first item if shiftOnReduce, otherwise use last item
            var movedItem = primaryItems[shiftOnReduce ? 0 : primaryItems.length - 1];
            if (movedItem !== undefined) {
                movedItem.renderedInOverflow = true;
                overflowItems = [movedItem].concat(overflowItems);
                primaryItems = shiftOnReduce ? primaryItems.slice(1) : primaryItems.slice(0, -1);
                data.primaryItems = primaryItems;
                data.overflowItems = overflowItems;
                cacheKey = _this._computeCacheKey(data);
                if (onDataReduced) {
                    onDataReduced(movedItem);
                }
                return tslib_1.__assign({}, data, { cacheKey: cacheKey });
            }
            return undefined;
        };
        _this._onGrowData = function (data) {
            var _a = _this.props, shiftOnReduce = _a.shiftOnReduce, onDataGrown = _a.onDataGrown;
            var minimumOverflowItems = data.minimumOverflowItems;
            var primaryItems = data.primaryItems, overflowItems = data.overflowItems, cacheKey = data.cacheKey;
            var movedItem = overflowItems[0];
            // Make sure that moved item exists and is not one of the original overflow items
            if (movedItem !== undefined && overflowItems.length > minimumOverflowItems) {
                movedItem.renderedInOverflow = false;
                overflowItems = overflowItems.slice(1);
                // if shiftOnReduce, movedItem goes first, otherwise, last.
                primaryItems = shiftOnReduce ? [movedItem].concat(primaryItems) : primaryItems.concat([movedItem]);
                data.primaryItems = primaryItems;
                data.overflowItems = overflowItems;
                cacheKey = _this._computeCacheKey(data);
                if (onDataGrown) {
                    onDataGrown(movedItem);
                }
                return tslib_1.__assign({}, data, { cacheKey: cacheKey });
            }
            return undefined;
        };
        return _this;
    }
    CommandBarBase.prototype.render = function () {
        var _a = this.props, className = _a.className, items = _a.items, overflowItems = _a.overflowItems, farItems = _a.farItems, styles = _a.styles, theme = _a.theme, _b = _a.onReduceData, onReduceData = _b === void 0 ? this._onReduceData : _b, _c = _a.onGrowData, onGrowData = _c === void 0 ? this._onGrowData : _c;
        var commandBarData = {
            primaryItems: items.slice(),
            overflowItems: overflowItems.slice(),
            minimumOverflowItems: overflowItems.slice().length,
            farItems: farItems,
            cacheKey: ''
        };
        this._classNames = getClassNames(styles, { theme: theme });
        return (React.createElement(ResizeGroup_1.ResizeGroup, { componentRef: this._resizeGroup, className: className, data: commandBarData, onReduceData: onReduceData, onGrowData: onGrowData, onRenderData: this._onRenderData }));
    };
    CommandBarBase.prototype.focus = function () {
        var overflowSet = this._overflowSet.current;
        overflowSet && overflowSet.focus();
    };
    CommandBarBase.prototype.remeasure = function () {
        this._resizeGroup.current && this._resizeGroup.current.remeasure();
    };
    CommandBarBase.prototype._onButtonClick = function (item) {
        return function (ev) {
            // inactive is deprecated. remove check in 7.0
            if (item.inactive) {
                return;
            }
            if (item.onClick) {
                item.onClick(ev, item);
            }
        };
    };
    CommandBarBase.prototype._computeCacheKey = function (data) {
        var primaryItems = data.primaryItems, _a = data.farItems, farItems = _a === void 0 ? [] : _a, overflowItems = data.overflowItems;
        var returnKey = function (acc, current) {
            var _a = current.cacheKey, cacheKey = _a === void 0 ? current.key : _a;
            return acc + cacheKey;
        };
        var primaryKey = primaryItems.reduce(returnKey, '');
        var farKey = farItems.reduce(returnKey, '');
        var overflowKey = !!overflowItems.length ? 'overflow' : '';
        return [primaryKey, farKey, overflowKey].join(' ');
    };
    CommandBarBase.defaultProps = {
        items: [],
        overflowItems: []
    };
    return CommandBarBase;
}(Utilities_1.BaseComponent));
exports.CommandBarBase = CommandBarBase;
//# sourceMappingURL=CommandBar.base.js.map