import { Icon, TableHeaderCell, Dropdown, ActionableBox, Box } from "@contember/ui";
import { useMemo, createElement } from "react";
import { cycleOrderDirection } from "./DataGridOrderDirection.js";
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
function DataGridHeaderCell({
  ascOrderIcon,
  descOrderIcon,
  emptyFilterArtifact,
  environment,
  filterArtifact,
  filterRenderer,
  hasFilter,
  header,
  headerJustification,
  orderState,
  setFilter,
  setOrderBy,
  shrunk
}) {
  const buttonProps = useMemo(() => ({
    intent: hasFilter ? "primary" : "default",
    distinction: "seamless",
    size: "small",
    children: /* @__PURE__ */ jsx(Icon, {
      blueprintIcon: "filter",
      alignWithLowercase: true,
      style: {
        opacity: hasFilter ? "1" : "0.5"
      }
    })
  }), [hasFilter]);
  return /* @__PURE__ */ jsx(TableHeaderCell, {
    scope: "col",
    justification: headerJustification,
    shrunk,
    children: /* @__PURE__ */ jsxs("span", {
      style: {
        display: "flex",
        justifyContent: "flex-start",
        gap: ".25em"
      },
      children: [/* @__PURE__ */ jsxs("span", {
        onClick: (e) => setOrderBy(cycleOrderDirection(orderState?.direction ?? null), e.ctrlKey || e.metaKey),
        style: {
          cursor: "pointer"
        },
        children: [header, "\xA0", orderState && /* @__PURE__ */ jsxs(Fragment, {
          children: [{
            asc: ascOrderIcon ?? defaultAscIcon,
            desc: descOrderIcon ?? defaultDescIcon
          }[orderState.direction], orderState.index !== void 0 ? `(${orderState.index + 1})` : null]
        })]
      }), filterRenderer && /* @__PURE__ */ jsx(Dropdown, {
        buttonProps,
        renderContent: ({
          requestClose
        }) => /* @__PURE__ */ jsx(ActionableBox, {
          onRemove: () => {
            setFilter(void 0);
            requestClose();
          },
          children: /* @__PURE__ */ jsx(Box, {
            heading: /* @__PURE__ */ jsxs(Fragment, {
              children: ["Filter: ", header]
            }),
            children: createElement(filterRenderer, {
              filter: filterArtifact === void 0 ? emptyFilterArtifact : filterArtifact,
              setFilter,
              environment
            })
          })
        })
      })]
    })
  });
}
const defaultAscIcon = /* @__PURE__ */ jsx(Icon, {
  blueprintIcon: "caret-up",
  size: "small",
  alignWithLowercase: true
});
const defaultDescIcon = /* @__PURE__ */ jsx(Icon, {
  blueprintIcon: "caret-down",
  size: "small",
  alignWithLowercase: true
});
export {
  DataGridHeaderCell
};
//# sourceMappingURL=DataGridHeaderCell.js.map
