import { useCallbackRef } from "../utils/react.js";
import { cs } from "../utils/styles.js";
import "../utils/index.js";
import { useStyleConfig } from "../core/theme.js";
import { styled } from "../core/styled.js";
import { vui } from "../core/vui.js";
import box_default from "../box/box.js";
import "../box/index.js";
import "../core/index.js";
import { useTableContext } from "./context.js";
import tableSortIcon_default from "./tableSortIcon.js";
import { jsx, jsxs } from "react/jsx-runtime";

//#region src/table/th.tsx
const ThBase = styled.thBox`
  font-weight: demi;
  text-align: left;
  transition-duration: fast;
`;
/** Displays Table heading with support for sort. */
const Th = vui((props, ref) => {
	const { children, className, disabled, field, isInteractive: isInteractiveProp, isSortable, onClick: onClickProp, title, ...rest } = props;
	const tableContext = useTableContext();
	const styles = useStyleConfig("Table", tableContext);
	const { onSortChange, sort } = tableContext || {};
	const handleClick = useCallbackRef((e) => {
		onClickProp?.(e);
		if (isSortable && field && onSortChange) onSortChange(field);
	});
	const onClick = onClickProp || isSortable ? handleClick : void 0;
	const isActive = !disabled && sort?.key === field;
	const sortOrder = isActive ? sort.order : void 0;
	const isInteractive = isInteractiveProp ?? onClick !== void 0;
	const activeProps = isActive ? { bg: "skyBlue.active" } : {};
	const disabledProps = disabled ? {
		bg: "disabled.bg",
		color: "disabled.color",
		cursor: "not-allowed"
	} : {};
	const hoverBg = isSortable ? "skyBlue.hover" : void 0;
	const interactiveProps = !disabled && isInteractive ? {
		cursor: "pointer",
		onClick,
		tabIndex: 0,
		userSelect: "none"
	} : {};
	return /* @__PURE__ */ jsx(ThBase, {
		className: cs("vui-th", className),
		"data-active": isActive,
		"data-disabled": disabled,
		hoverBg,
		px: 2,
		ref,
		...styles.th,
		...activeProps,
		...disabledProps,
		...interactiveProps,
		...rest,
		children: children ?? /* @__PURE__ */ jsxs(box_default, {
			centerV: true,
			display: "inline-flex",
			children: [title, isSortable && /* @__PURE__ */ jsx(tableSortIcon_default, {
				ml: 1,
				my: .5,
				order: sortOrder
			})]
		})
	});
});
Th.displayName = "Th";
var th_default = Th;

//#endregion
export { Th, ThBase, th_default as default };
globalThis.__vuiVersion__ = "4.0.0-beta.1"
//# sourceMappingURL=th.js.map