import { useMemo, createElement, Fragment } from "react";
import { Icon, Dropdown, Box, Table, TableRow, TableHeaderCell, TableCell, Button, ButtonGroup } from "@contember/ui";
import { EmptyMessage } from "../../helpers/EmptyMessage.js";
function DataGridFullFilters({
  desiredState,
  environment,
  formatMessage,
  setFilter
}) {
  const remainingColumns = Array.from(desiredState.columns).filter(([key, column]) => column.enableFiltering !== false && !(key in desiredState.filterArtifacts));
  const hasAnyFilters = Object.keys(desiredState.filterArtifacts).length > 0;
  const columnFilteringButtonProps = useMemo(() => ({
    intent: hasAnyFilters ? "primary" : "default",
    distinction: "seamless",
    children: /* @__PURE__ */ createElement(Fragment, null, /* @__PURE__ */ createElement(Icon, {
      blueprintIcon: "filter",
      alignWithLowercase: true,
      style: {
        marginRight: "0.2em",
        opacity: hasAnyFilters ? "1" : "0.8"
      }
    }), formatMessage("dataGrid.columnFiltering.showMenuButton.text"))
  }), [formatMessage, hasAnyFilters]);
  const filterButtonBrops = useMemo(() => ({
    distinction: "seamless",
    flow: "block",
    style: { marginTop: hasAnyFilters ? "1em" : 0 },
    children: /* @__PURE__ */ createElement(Fragment, null, /* @__PURE__ */ createElement(Icon, {
      alignWithLowercase: true,
      blueprintIcon: "add",
      style: { marginRight: "0.2em" }
    }), formatMessage("dataGrid.columnFiltering.addFilterButton.text"))
  }), [formatMessage, hasAnyFilters]);
  return /* @__PURE__ */ createElement(Dropdown, {
    alignment: "end",
    buttonProps: columnFilteringButtonProps,
    renderContent: ({ update: updateOuterDropdown }) => /* @__PURE__ */ createElement(Box, {
      heading: formatMessage("dataGrid.columnFiltering.heading")
    }, hasAnyFilters || /* @__PURE__ */ createElement(EmptyMessage, null, formatMessage("dataGrid.columnFiltering.emptyMessage.text")), hasAnyFilters && /* @__PURE__ */ createElement(Table, {
      tableHead: /* @__PURE__ */ createElement(TableRow, null, /* @__PURE__ */ createElement(TableHeaderCell, {
        scope: "col",
        justification: "justifyStart",
        shrunk: true
      }, formatMessage("dataGrid.columnFiltering.columnColumn.text")), /* @__PURE__ */ createElement(TableHeaderCell, {
        scope: "col",
        justification: "justifyStart"
      }, formatMessage("dataGrid.columnFiltering.filterColumn.text")), /* @__PURE__ */ createElement(TableHeaderCell, {
        scope: "col",
        shrunk: true
      }, "\xA0"))
    }, Array.from(Object.entries(desiredState.filterArtifacts), ([key, filterArtifact]) => {
      const column = desiredState.columns.get(key);
      const filterRenderer = column.enableFiltering !== false ? column.filterRenderer : void 0;
      if (!filterRenderer) {
        return /* @__PURE__ */ createElement(Fragment, {
          key
        });
      }
      return /* @__PURE__ */ createElement(TableRow, {
        key
      }, /* @__PURE__ */ createElement(TableCell, {
        justification: "justifyStart"
      }, column.header), /* @__PURE__ */ createElement(TableCell, null, createElement(filterRenderer, {
        filter: filterArtifact,
        setFilter: (newArtifact) => setFilter(key, newArtifact),
        environment
      })), /* @__PURE__ */ createElement(TableCell, {
        shrunk: true,
        justification: "justifyEnd"
      }, /* @__PURE__ */ createElement(Button, {
        onClick: () => {
          updateOuterDropdown();
          setFilter(key, void 0);
        },
        distinction: "seamless",
        size: "small",
        bland: true
      }, /* @__PURE__ */ createElement(Icon, {
        blueprintIcon: "trash",
        size: "small"
      }))));
    })), !!remainingColumns.length && /* @__PURE__ */ createElement(Dropdown, {
      alignment: "center",
      buttonProps: filterButtonBrops
    }, ({ requestClose }) => /* @__PURE__ */ createElement(ButtonGroup, {
      orientation: "vertical"
    }, remainingColumns.map(([key, column]) => /* @__PURE__ */ createElement(Button, {
      key,
      distinction: "seamless",
      flow: "generousBlock",
      justification: "justifyStart",
      onClick: () => {
        requestClose();
        updateOuterDropdown();
        if (column.enableFiltering === false) {
          return;
        }
        setFilter(key, column.emptyFilter);
      }
    }, column.header)))))
  });
}
export { DataGridFullFilters };
//# sourceMappingURL=DataGridFullFilters.js.map
