import { jsxs, Fragment, jsx } from "react/jsx-runtime";
import { Icon, Dropdown, Box, Table, TableRow, TableHeaderCell, TableCell, Button, ButtonGroup } from "@contember/ui";
import { useMemo, Fragment as Fragment$1, createElement } from "react";
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__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(
        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__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(Icon, { alignWithLowercase: true, blueprintIcon: "add", style: { marginRight: "0.2em" } }),
      formatMessage("dataGrid.columnFiltering.addFilterButton.text")
    ] })
  }), [formatMessage, hasAnyFilters]);
  return /* @__PURE__ */ jsx(
    Dropdown,
    {
      alignment: "end",
      buttonProps: columnFilteringButtonProps,
      renderContent: ({ update: updateOuterDropdown }) => /* @__PURE__ */ jsxs(Box, { heading: formatMessage("dataGrid.columnFiltering.heading"), children: [
        hasAnyFilters || /* @__PURE__ */ jsx(EmptyMessage, { children: formatMessage("dataGrid.columnFiltering.emptyMessage.text") }),
        hasAnyFilters && /* @__PURE__ */ jsx(
          Table,
          {
            tableHead: /* @__PURE__ */ jsxs(TableRow, { children: [
              /* @__PURE__ */ jsx(TableHeaderCell, { scope: "col", justification: "justifyStart", shrunk: true, children: formatMessage("dataGrid.columnFiltering.columnColumn.text") }),
              /* @__PURE__ */ jsx(TableHeaderCell, { scope: "col", justification: "justifyStart", children: formatMessage("dataGrid.columnFiltering.filterColumn.text") }),
              /* @__PURE__ */ jsx(TableHeaderCell, { scope: "col", shrunk: true, children: " " })
            ] }),
            children: 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__ */ jsx(Fragment$1, {}, key);
              }
              return /* @__PURE__ */ jsxs(TableRow, { children: [
                /* @__PURE__ */ jsx(TableCell, { justification: "justifyStart", children: column.header }),
                /* @__PURE__ */ jsx(TableCell, { children: createElement(filterRenderer, {
                  filter: filterArtifact,
                  setFilter: (newArtifact) => setFilter(key, newArtifact),
                  environment
                }) }),
                /* @__PURE__ */ jsx(TableCell, { shrunk: true, justification: "justifyEnd", children: /* @__PURE__ */ jsx(
                  Button,
                  {
                    onClick: () => {
                      updateOuterDropdown();
                      setFilter(key, void 0);
                    },
                    distinction: "seamless",
                    size: "small",
                    bland: true,
                    children: /* @__PURE__ */ jsx(Icon, { blueprintIcon: "trash", size: "small" })
                  }
                ) })
              ] }, key);
            })
          }
        ),
        !!remainingColumns.length && /* @__PURE__ */ jsx(
          Dropdown,
          {
            alignment: "center",
            buttonProps: filterButtonBrops,
            children: ({ requestClose }) => /* @__PURE__ */ jsx(ButtonGroup, { orientation: "vertical", children: remainingColumns.map(([key, column]) => /* @__PURE__ */ jsx(
              Button,
              {
                distinction: "seamless",
                flow: "generousBlock",
                justification: "justifyStart",
                onClick: () => {
                  requestClose();
                  updateOuterDropdown();
                  if (column.enableFiltering === false) {
                    return;
                  }
                  setFilter(key, column.emptyFilter);
                },
                children: column.header
              },
              key
            )) })
          }
        )
      ] })
    }
  );
}
export {
  DataGridFullFilters
};
//# sourceMappingURL=DataGridFullFilters.js.map
