/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

import { __extends, __assign, __awaiter, __generator } from 'tslib';
import React__default from 'react';
import { themeable, localeable } from 'amis-core';
import { Icon } from '../icons.js';

/**
 * @file table/HeadCellSort
 * @author fex
 */
var HeadCellSort = /** @class */ (function (_super) {
    __extends(HeadCellSort, _super);
    function HeadCellSort(props) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            orderDir: '',
            orderBy: ''
        };
        return _this;
    }
    HeadCellSort.prototype.componentDidUpdate = function (prevProps) {
        var props = this.props;
        // 失效后重置，同时只能有一列在排序
        if ((props === null || props === void 0 ? void 0 : props.active) !== undefined &&
            !(props === null || props === void 0 ? void 0 : props.active) &&
            props.active !== (prevProps === null || prevProps === void 0 ? void 0 : prevProps.active)) {
            this.setState({ orderBy: '', orderDir: '' });
        }
    };
    HeadCellSort.prototype.render = function () {
        var _this = this;
        var _a = this.props, active = _a.active, column = _a.column, onSort = _a.onSort, cx = _a.classnames, testIdBuilder = _a.testIdBuilder;
        return (React__default.createElement("span", __assign({ className: cx('TableCell-sortBtn', 'aaa') }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getTestId(), { onClick: function () { return __awaiter(_this, void 0, void 0, function () {
                var sortPayload, prevented;
                return __generator(this, function (_a) {
                    switch (_a.label) {
                        case 0:
                            sortPayload = {
                                orderBy: '',
                                orderDir: ''
                            };
                            if (column.name === this.state.orderBy) {
                                if (this.state.orderDir === 'desc') {
                                    // 降序改为取消
                                    sortPayload = { orderBy: '', orderDir: '' };
                                }
                                else {
                                    // 升序之后降序
                                    sortPayload = { orderBy: column.name, orderDir: 'desc' };
                                }
                            }
                            else {
                                // 默认先升序
                                sortPayload = { orderBy: column.name, orderDir: 'asc' };
                            }
                            if (!onSort) return [3 /*break*/, 2];
                            return [4 /*yield*/, onSort({
                                    orderBy: sortPayload.orderBy,
                                    orderDir: sortPayload.orderDir
                                })];
                        case 1:
                            prevented = _a.sent();
                            if (prevented) {
                                return [2 /*return*/];
                            }
                            _a.label = 2;
                        case 2:
                            this.setState(sortPayload);
                            return [2 /*return*/];
                    }
                });
            }); } }),
            React__default.createElement("i", { className: cx('TableCell-sortBtn--down', active && this.state.orderDir === 'desc' ? 'is-active' : '') },
                React__default.createElement(Icon, { icon: "sort-desc", className: "icon", iconContent: "table-sort-down", testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('desc') })),
            React__default.createElement("i", { className: cx('TableCell-sortBtn--up', active && this.state.orderDir === 'asc' ? 'is-active' : '') },
                React__default.createElement(Icon, { icon: "sort-asc", className: "icon", iconContent: "table-sort-up", testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('asc') })),
            React__default.createElement("i", { className: cx('TableCell-sortBtn--default', active ? '' : 'is-active') },
                React__default.createElement(Icon, { icon: "sort-default", className: "icon", iconContent: "table-sort-default", testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('default') }))));
    };
    return HeadCellSort;
}(React__default.PureComponent));
var HeadCellSort$1 = themeable(localeable(HeadCellSort));

export { HeadCellSort, HeadCellSort$1 as default };
