import * as tslib_1 from "tslib";
import * as React from 'react';
import { Icon } from '../../Icon';
import { BaseComponent, css, createRef } from '../../Utilities';
import { ColumnActionsMode } from './DetailsList.types';
var INNER_PADDING = 16; // Account for padding around the cell.
var ISPADDED_WIDTH = 24;
var MOUSEDOWN_PRIMARY_BUTTON = 0; // for mouse down event we are using ev.button property, 0 means left button
var DetailsColumn = /** @class */ (function (_super) {
    tslib_1.__extends(DetailsColumn, _super);
    function DetailsColumn(props) {
        var _this = _super.call(this, props) || this;
        _this._onRenderColumnHeaderTooltip = function (tooltipHostProps, defaultRender) {
            return React.createElement("span", { className: tooltipHostProps.hostClassName }, tooltipHostProps.children);
        };
        _this._onRootMouseDown = function (ev) {
            var isDraggable = _this.props.isDraggable;
            // Ignore anything except the primary button.
            if (isDraggable && ev.button === MOUSEDOWN_PRIMARY_BUTTON) {
                ev.stopPropagation();
            }
        };
        _this._root = createRef();
        _this._onDragStart = _this._onDragStart.bind(_this);
        _this._onDragEnd = _this._onDragEnd.bind(_this);
        _this._onRootMouseDown = _this._onRootMouseDown.bind(_this);
        return _this;
    }
    DetailsColumn.prototype.render = function () {
        var _a = this.props, column = _a.column, columnIndex = _a.columnIndex, parentId = _a.parentId, isDraggable = _a.isDraggable, headerClassNames = _a.headerClassNames;
        var _b = this.props.onRenderColumnHeaderTooltip, onRenderColumnHeaderTooltip = _b === void 0 ? this._onRenderColumnHeaderTooltip : _b;
        return [
            React.createElement("div", { key: column.key, ref: this._root, role: 'columnheader', "aria-sort": column.isSorted ? (column.isSortedDescending ? 'descending' : 'ascending') : 'none', "aria-colindex": columnIndex, className: css(headerClassNames.cell, column.headerClassName, column.columnActionsMode !== ColumnActionsMode.disabled &&
                    'is-actionable ' + headerClassNames.cellIsActionable, !column.name && 'is-empty ' + headerClassNames.cellIsEmpty, (column.isSorted || column.isGrouped || column.isFiltered) && 'is-icon-visible', column.isPadded && headerClassNames.cellWrapperPadded), "data-is-draggable": isDraggable, draggable: isDraggable, style: { width: column.calculatedWidth + INNER_PADDING + (column.isPadded ? ISPADDED_WIDTH : 0) }, "data-automationid": 'ColumnsHeaderColumn', "data-item-key": column.key },
                isDraggable && React.createElement(Icon, { iconName: 'GripperBarVertical', className: headerClassNames.gripperBarVerticalStyle }),
                onRenderColumnHeaderTooltip({
                    hostClassName: headerClassNames.cellTooltip,
                    id: parentId + "-" + column.key + "-tooltip",
                    setAriaDescribedBy: false,
                    content: column.columnActionsMode !== ColumnActionsMode.disabled ? column.ariaLabel : '',
                    children: (React.createElement("span", { id: parentId + "-" + column.key, "aria-label": column.isIconOnly ? column.name : undefined, "aria-labelledby": column.isIconOnly ? undefined : parentId + "-" + column.key + "-name ", className: headerClassNames.cellTitle, "data-is-focusable": column.columnActionsMode !== ColumnActionsMode.disabled, role: column.columnActionsMode !== ColumnActionsMode.disabled ? 'button' : undefined, "aria-describedby": this.props.onRenderColumnHeaderTooltip ? parentId + "-" + column.key + "-tooltip" : undefined, onContextMenu: this._onColumnContextMenu.bind(this, column), onClick: this._onColumnClick.bind(this, column), "aria-haspopup": column.columnActionsMode === ColumnActionsMode.hasDropdown },
                        React.createElement("span", { id: parentId + "-" + column.key + "-name", className: css(headerClassNames.cellName, (_c = {},
                                _c[headerClassNames.iconOnlyHeader] = column.isIconOnly,
                                _c)) },
                            (column.iconName || column.iconClassName) && (React.createElement(Icon, { className: css(headerClassNames.nearIcon, column.iconClassName), iconName: column.iconName })),
                            !column.isIconOnly ? column.name : undefined),
                        column.isFiltered && React.createElement(Icon, { className: headerClassNames.nearIcon, iconName: 'Filter' }),
                        column.isSorted && (React.createElement(Icon, { className: css(headerClassNames.nearIcon, headerClassNames.sortIcon), iconName: column.isSortedDescending ? 'SortDown' : 'SortUp' })),
                        column.isGrouped && React.createElement(Icon, { className: headerClassNames.nearIcon, iconName: 'GroupedDescending' }),
                        column.columnActionsMode === ColumnActionsMode.hasDropdown &&
                            !column.isIconOnly && React.createElement(Icon, { className: headerClassNames.filterChevron, iconName: 'ChevronDown' })))
                }, this._onRenderColumnHeaderTooltip)),
            column.ariaLabel && !this.props.onRenderColumnHeaderTooltip ? (React.createElement("label", { key: column.key + "_label", id: parentId + "-" + column.key + "-tooltip", className: headerClassNames.accessibleLabel }, column.ariaLabel)) : null
        ];
        var _c;
    };
    DetailsColumn.prototype.componentDidMount = function () {
        var _this = this;
        if (this._dragDropSubscription) {
            this._dragDropSubscription.dispose();
            delete this._dragDropSubscription;
        }
        if (this.props.dragDropHelper && this.props.isDraggable) {
            this._dragDropSubscription = this.props.dragDropHelper.subscribe(this._root.current, this._events, this._getColumnDragDropOptions());
            // We need to use native on this to avoid MarqueeSelection from handling the event before us.
            this._events.on(this._root.current, 'mousedown', this._onRootMouseDown);
        }
        var headerClassNames = this.props.headerClassNames;
        if (this.props.isDropped) {
            if (this._root.current) {
                this._root.current.classList.add(headerClassNames.borderAfterDropping);
            }
            setTimeout(function () {
                if (_this._root.current) {
                    _this._root.current.classList.remove(headerClassNames.borderAfterDropping);
                }
            }, 1500);
        }
    };
    DetailsColumn.prototype.componentWillUnmount = function () {
        if (this._dragDropSubscription) {
            this._dragDropSubscription.dispose();
            delete this._dragDropSubscription;
        }
    };
    DetailsColumn.prototype.componentDidUpdate = function () {
        if (!this._dragDropSubscription && this.props.dragDropHelper && this.props.isDraggable) {
            this._dragDropSubscription = this.props.dragDropHelper.subscribe(this._root.value, this._events, this._getColumnDragDropOptions());
            // We need to use native on this to avoid MarqueeSelection from handling the event before us.
            this._events.on(this._root.current, 'mousedown', this._onRootMouseDown);
        }
        if (this._dragDropSubscription && !this.props.isDraggable) {
            this._dragDropSubscription.dispose();
            this._events.off(this._root.current, 'mousedown');
            delete this._dragDropSubscription;
        }
    };
    DetailsColumn.prototype._onColumnClick = function (column, ev) {
        var onColumnClick = this.props.onColumnClick;
        if (column.onColumnClick) {
            column.onColumnClick(ev, column);
        }
        if (onColumnClick) {
            onColumnClick(ev, column);
        }
    };
    DetailsColumn.prototype._getColumnDragDropOptions = function () {
        var _this = this;
        var columnIndex = this.props.columnIndex;
        var options = {
            selectionIndex: columnIndex,
            context: { data: columnIndex, index: columnIndex },
            canDrag: function () { return _this.props.isDraggable; },
            canDrop: function () { return false; },
            onDragStart: this._onDragStart,
            updateDropState: function () { return undefined; },
            onDrop: function () { return undefined; },
            onDragEnd: this._onDragEnd
        };
        return options;
    };
    DetailsColumn.prototype._onDragStart = function (item, itemIndex, selectedItems, event) {
        var headerClassNames = this.props.headerClassNames;
        if (itemIndex && this.props.setDraggedItemIndex) {
            this.props.setDraggedItemIndex(itemIndex);
            this._root.current.classList.add(headerClassNames.borderWhileDragging);
        }
    };
    DetailsColumn.prototype._onDragEnd = function (item, event) {
        var headerClassNames = this.props.headerClassNames;
        if (this.props.setDraggedItemIndex) {
            this.props.setDraggedItemIndex(-1);
            this._root.current.classList.remove(headerClassNames.borderWhileDragging);
        }
    };
    DetailsColumn.prototype._onColumnContextMenu = function (column, ev) {
        var onColumnContextMenu = this.props.onColumnContextMenu;
        if (column.onColumnContextMenu) {
            column.onColumnContextMenu(column, ev);
            ev.preventDefault();
        }
        if (onColumnContextMenu) {
            onColumnContextMenu(column, ev);
            ev.preventDefault();
        }
    };
    return DetailsColumn;
}(BaseComponent));
export { DetailsColumn };
//# sourceMappingURL=DetailsColumn.js.map