var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
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);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Icon } from '../../Icon';
import { playMessage } from '../../functions';
import lang from '../../../../lang/common/index-en_US';
export var ASC = 'ASC';
export var DESC = 'DESC';
export var KEY_ENTER = 13;
export var KEY_SPACE = 32;
var CommonListSorter = /** @class */ (function (_super) {
    __extends(CommonListSorter, _super);
    function CommonListSorter() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.handleClick = function () {
            var _a = _this.props, _b = _a.ascendingText, ascendingText = _b === void 0 ? '' : _b, _c = _a.descendingText, descendingText = _c === void 0 ? '' : _c, displayName = _a.displayName, property = _a.property, children = _a.children, onChangeSorting = _a.onChangeSorting, direction = _a.direction;
            var shouldReverse = direction === ASC;
            onChangeSorting(property, shouldReverse ? DESC : ASC);
            playMessage(shouldReverse
                ? descendingText.replace('{name}', displayName || children || property)
                : ascendingText.replace('{name}', displayName || children || property));
        };
        _this.handleKeyDown = function (e) {
            if (e.keyCode === KEY_ENTER || e.keyCode === KEY_SPACE) {
                _this.handleClick();
                e.preventDefault();
            }
        };
        return _this;
    }
    CommonListSorter.prototype.sorterClassName = function () {
        var direction = this.props.direction;
        return classnames('sortable', {
            'order-asc': direction === ASC,
            'order-desc': direction === DESC
        });
    };
    CommonListSorter.prototype.getAccessibilityHeaderProps = function () {
        return {
            role: 'button',
            onKeyDown: this.handleKeyDown,
            tabIndex: 0,
            title: this.props.sorterTitleText
        };
    };
    CommonListSorter.prototype.getAriaSortName = function () {
        var direction = this.props.direction;
        var sortName = 'none';
        if (direction === ASC) {
            sortName = 'ascending';
        }
        if (direction === DESC) {
            sortName = 'descending';
        }
        return sortName;
    };
    CommonListSorter.prototype.renderSorterIcon = function () {
        var direction = this.props.direction;
        var iconName;
        if (direction === ASC) {
            iconName = 'chevron-down-small';
        }
        if (direction === DESC) {
            iconName = 'chevron-up-small';
        }
        var icon;
        if (iconName) {
            icon = React.createElement(Icon, { "aria-hidden": "true", name: iconName, className: "sorter-icon" });
        }
        else {
            icon = (React.createElement(Icon, { "aria-hidden": "true", name: "chevron-down-small", className: "sorter-icon list-hover-arrow" }));
        }
        return React.createElement("span", { className: "column-header-right" }, icon);
    };
    CommonListSorter.prototype.renderPrepend = function (prepend) {
        if (!prepend) {
            return null;
        }
        return React.createElement("span", null, prepend);
    };
    CommonListSorter.prototype.renderAppend = function (append) {
        if (!append) {
            return null;
        }
        return React.createElement("span", { className: "column-header-append" }, append);
    };
    CommonListSorter.prototype.renderCellContent = function () {
        var _a = this.props, prepend = _a.prepend, children = _a.children, append = _a.append;
        return (React.createElement("div", { className: "column-header-content" },
            this.renderPrepend(prepend),
            React.createElement("div", __assign({ className: "column-header-label text-overflow" }, this.getAccessibilityHeaderProps()), children),
            this.renderAppend(append),
            this.renderSorterIcon()));
    };
    CommonListSorter.prototype.render = function () {
        var _a = this.props, tagName = _a.tagName, onChangeSorting = _a.onChangeSorting, direction = _a.direction, children = _a.children, append = _a.append, prepend = _a.prepend, property = _a.property, displayName = _a.displayName, ascendingText = _a.ascendingText, descendingText = _a.descendingText, sorterTitleText = _a.sorterTitleText, testAutomationId = _a.testAutomationId, className = _a.className, props = __rest(_a, ["tagName", "onChangeSorting", "direction", "children", "append", "prepend", "property", "displayName", "ascendingText", "descendingText", "sorterTitleText", "testAutomationId", "className"]);
        var Tag = tagName;
        return (React.createElement(Tag, __assign({ onClick: this.handleClick, role: "columnheader", "aria-sort": this.getAriaSortName(), "data-test-automation-id": testAutomationId, className: classnames(className, this.sorterClassName()) }, props), this.renderCellContent()));
    };
    CommonListSorter.propTypes = {
        tagName: PropTypes.string,
        className: PropTypes.string,
        onChangeSorting: PropTypes.func.isRequired,
        direction: PropTypes.oneOf([null, ASC, DESC]).isRequired,
        children: PropTypes.node,
        append: PropTypes.node,
        prepend: PropTypes.node,
        property: PropTypes.string,
        displayName: PropTypes.string,
        ascendingText: PropTypes.string,
        descendingText: PropTypes.string,
        sorterTitleText: PropTypes.string,
        testAutomationId: PropTypes.string
    };
    CommonListSorter.defaultProps = {
        tagName: 'th',
        className: '',
        children: null,
        append: null,
        prepend: null,
        property: '',
        displayName: '',
        get ascendingText() {
            return lang.SORT_BY_ASCENDING;
        },
        get descendingText() {
            return lang.SORT_BY_DESCENDING;
        },
        get sorterTitleText() {
            return lang.CLICK_TO_SORT;
        },
        testAutomationId: null
    };
    return CommonListSorter;
}(React.Component));
export { CommonListSorter };
//# sourceMappingURL=CommonListSorter.js.map