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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');
var icons = require('../icons.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var HeadCellSort = /** @class */ (function (_super) {
    tslib.__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 (_J$X_("span", tslib.__assign({ className: cx('TableCell-sortBtn', 'aaa') }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getTestId(), { onClick: function () { return tslib.__awaiter(_this, void 0, void 0, function () {
                var sortPayload, prevented;
                return tslib.__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*/];
                    }
                });
            }); } }),
            _J$X_("i", { className: cx('TableCell-sortBtn--down', active && this.state.orderDir === 'desc' ? 'is-active' : '') },
                _J$X_(icons.Icon, { icon: "sort-desc", className: "icon", iconContent: "table-sort-down", testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('desc') })),
            _J$X_("i", { className: cx('TableCell-sortBtn--up', active && this.state.orderDir === 'asc' ? 'is-active' : '') },
                _J$X_(icons.Icon, { icon: "sort-asc", className: "icon", iconContent: "table-sort-up", testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('asc') })),
            _J$X_("i", { className: cx('TableCell-sortBtn--default', active ? '' : 'is-active') },
                _J$X_(icons.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["default"].PureComponent));
var HeadCellSort$1 = amisCore.themeable(amisCore.localeable(HeadCellSort));

exports.HeadCellSort = HeadCellSort;
exports["default"] = HeadCellSort$1;
