"use strict";
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 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                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 });
exports.Th = exports.ThBase = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const box_1 = __importDefault(require("../box"));
const core_1 = require("../core");
const utils_1 = require("../utils");
const context_1 = require("./context");
const tableSortIcon_1 = __importDefault(require("./tableSortIcon"));
exports.ThBase = core_1.styled.thBox `
  font-weight: demi;
  text-align: left;
  transition-duration: fast;
`;
/** Displays Table heading with support for sort. */
exports.Th = (0, core_1.vui)((props, ref) => {
    var _a;
    const { children, className, disabled, field, isInteractive: isInteractiveProp, isSortable, onClick: onClickProp, title } = props, rest = __rest(props, ["children", "className", "disabled", "field", "isInteractive", "isSortable", "onClick", "title"]);
    const { onSortChange, sort } = (_a = (0, context_1.useTableContext)()) !== null && _a !== void 0 ? _a : {};
    const styles = (0, core_1.useStyleConfig)('Table', (0, context_1.useTableContext)());
    const onClick = isSortable
        ? (0, utils_1.useCallbackRef)((e) => {
            onClickProp === null || onClickProp === void 0 ? void 0 : onClickProp(e);
            field && onSortChange(field);
        })
        : onClickProp;
    const isActive = !disabled && (sort === null || sort === void 0 ? void 0 : sort.key) === field;
    const sortOrder = isActive ? sort.order : undefined;
    const isInteractive = isInteractiveProp !== null && isInteractiveProp !== void 0 ? isInteractiveProp : onClick !== undefined;
    const activeProps = isActive ? { bg: 'skyBlue.active' } : {};
    const disabledProps = disabled ? { bg: 'disabled.bg', color: 'disabled.color', cursor: 'not-allowed' } : {};
    const hoverBg = isSortable ? 'skyBlue.hover' : undefined;
    const interactiveProps = !disabled && isInteractive
        ? {
            cursor: 'pointer',
            onClick,
            tabIndex: 0,
            userSelect: 'none',
        }
        : {};
    return ((0, jsx_runtime_1.jsx)(exports.ThBase, Object.assign({ className: (0, utils_1.cs)('vui-th', className), "data-active": isActive, "data-disabled": disabled, hoverBg: hoverBg, px: 2, ref: ref }, styles.th, activeProps, disabledProps, interactiveProps, rest, { children: children !== null && children !== void 0 ? children : ((0, jsx_runtime_1.jsxs)(box_1.default, { centerV: true, display: "inline-flex", children: [title, isSortable && (0, jsx_runtime_1.jsx)(tableSortIcon_1.default, { ml: 1, my: 0.5, order: sortOrder })] })) })));
});
exports.Th.displayName = 'Th';
exports.default = exports.Th;
//# sourceMappingURL=th.js.map