"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var classnames_1 = __importDefault(require("classnames"));
var Icon_1 = require("../../Icon");
var functions_1 = require("../../functions");
var index_en_US_1 = __importDefault(require("../../../../lang/common/index-en_US"));
exports.ASC = 'ASC';
exports.DESC = 'DESC';
exports.KEY_ENTER = 13;
exports.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 === exports.ASC;
            onChangeSorting(property, shouldReverse ? exports.DESC : exports.ASC);
            functions_1.playMessage(shouldReverse
                ? descendingText.replace('{name}', displayName || children || property)
                : ascendingText.replace('{name}', displayName || children || property));
        };
        _this.handleKeyDown = function (e) {
            if (e.keyCode === exports.KEY_ENTER || e.keyCode === exports.KEY_SPACE) {
                _this.handleClick();
                e.preventDefault();
            }
        };
        return _this;
    }
    CommonListSorter.prototype.sorterClassName = function () {
        var direction = this.props.direction;
        return classnames_1.default('sortable', {
            'order-asc': direction === exports.ASC,
            'order-desc': direction === exports.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 === exports.ASC) {
            sortName = 'ascending';
        }
        if (direction === exports.DESC) {
            sortName = 'descending';
        }
        return sortName;
    };
    CommonListSorter.prototype.renderSorterIcon = function () {
        var direction = this.props.direction;
        var iconName;
        if (direction === exports.ASC) {
            iconName = 'chevron-down-small';
        }
        if (direction === exports.DESC) {
            iconName = 'chevron-up-small';
        }
        var icon;
        if (iconName) {
            icon = react_1.default.createElement(Icon_1.Icon, { "aria-hidden": "true", name: iconName, className: "sorter-icon" });
        }
        else {
            icon = (react_1.default.createElement(Icon_1.Icon, { "aria-hidden": "true", name: "chevron-down-small", className: "sorter-icon list-hover-arrow" }));
        }
        return react_1.default.createElement("span", { className: "column-header-right" }, icon);
    };
    CommonListSorter.prototype.renderPrepend = function (prepend) {
        if (!prepend) {
            return null;
        }
        return react_1.default.createElement("span", null, prepend);
    };
    CommonListSorter.prototype.renderAppend = function (append) {
        if (!append) {
            return null;
        }
        return react_1.default.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_1.default.createElement("div", { className: "column-header-content" },
            this.renderPrepend(prepend),
            react_1.default.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_1.default.createElement(Tag, __assign({ onClick: this.handleClick, role: "columnheader", "aria-sort": this.getAriaSortName(), "data-test-automation-id": testAutomationId, className: classnames_1.default(className, this.sorterClassName()) }, props), this.renderCellContent()));
    };
    CommonListSorter.propTypes = {
        tagName: prop_types_1.default.string,
        className: prop_types_1.default.string,
        onChangeSorting: prop_types_1.default.func.isRequired,
        direction: prop_types_1.default.oneOf([null, exports.ASC, exports.DESC]).isRequired,
        children: prop_types_1.default.node,
        append: prop_types_1.default.node,
        prepend: prop_types_1.default.node,
        property: prop_types_1.default.string,
        displayName: prop_types_1.default.string,
        ascendingText: prop_types_1.default.string,
        descendingText: prop_types_1.default.string,
        sorterTitleText: prop_types_1.default.string,
        testAutomationId: prop_types_1.default.string
    };
    CommonListSorter.defaultProps = {
        tagName: 'th',
        className: '',
        children: null,
        append: null,
        prepend: null,
        property: '',
        displayName: '',
        get ascendingText() {
            return index_en_US_1.default.SORT_BY_ASCENDING;
        },
        get descendingText() {
            return index_en_US_1.default.SORT_BY_DESCENDING;
        },
        get sorterTitleText() {
            return index_en_US_1.default.CLICK_TO_SORT;
        },
        testAutomationId: null
    };
    return CommonListSorter;
}(react_1.default.Component));
exports.CommonListSorter = CommonListSorter;
//# sourceMappingURL=CommonListSorter.js.map