import { jsx as t, jsxs as l, Fragment as S } from "react/jsx-runtime";
import { useState as T, useRef as V, isValidElement as v } from "react";
import n from "prop-types";
import { TableFilter as ue, TableViewCol as pe } from "mui-datatables";
import me, { PrintContextConsumer as fe } from "react-to-print";
import { MoreVert as he, CloudDownload as be, ViewColumn as Ce, FilterList as we, Print as $ } from "@mui/icons-material";
import { Popover as xe, Tooltip as D, IconButton as R, ListItemIcon as L, ListItemText as j, MenuItem as U, Menu as ge, LinearProgress as ye, useTheme as Te, useMediaQuery as ve } from "@mui/material";
import { useDeepCompareEffect as Re } from "ahooks";
import { handleCSVDownload as ke } from "./utils.js";
import Fe from "./TableSearch.js";
import { useDatatableContext as Pe } from "./DatatableContext.js";
import { styled as z } from "../Theme/index.js";
function Ie() {
  const s = Te();
  return ve(s.breakpoints.down("sm"));
}
function Be({ ...s }) {
  const [h, d] = T(null), k = V(null), F = Ie(), N = V(Math.random().toString(32).slice(2)), [O, A] = T(!1), { customPreButtons: b, customButtons: C, loading: u, disabled: w } = Pe(), {
    data: Q = [],
    options: o,
    components: p = {},
    columns: P,
    filterList: G = [],
    filterData: H = [],
    filterUpdate: J,
    resetFilters: K,
    updateFilterByType: W,
    toggleViewColumn: X,
    updateColumns: Y,
    title: I = "",
    searchText: Z = null,
    searchTextUpdate: _,
    searchClose: ee
  } = s, [B, m] = T({}), te = o.customToolbar ? o.customToolbar(s) : "", { search: oe, downloadCsv: ne, print: f, viewColumns: ie, filterTable: re } = o.textLabels.toolbar, le = o.search === !1 || o.search === "false", x = o.print === !1 || o.print === "false", se = p.TableFilter || ue, ae = p.TableViewCol || pe;
  Re(() => {
    (u || w) && m({});
  }, [u, w]);
  const q = (e) => /* @__PURE__ */ t(me, { content: () => s.tableRef(), children: /* @__PURE__ */ t(fe, { children: e }) }), g = (e) => `toolbar-pop-${N.current}-${e}`, a = [];
  o.download === !1 || o.download === "false" || a.push({
    icon: /* @__PURE__ */ t(be, {}),
    title: ne,
    onClick: () => {
      ke(s);
    }
  }), o.viewColumns === !1 || o.viewColumns === "false" || a.push({
    icon: /* @__PURE__ */ t(Ce, {}),
    title: ie,
    // eslint-disable-next-line react/no-unstable-nested-components
    popRender() {
      return /* @__PURE__ */ t(
        ae,
        {
          data: Q,
          columns: P,
          options: o,
          onColumnUpdate: X,
          updateColumns: Y,
          components: p
        }
      );
    }
  }), o.filter === !1 || o.filter === "false" || a.push({
    icon: /* @__PURE__ */ t(we, {}),
    title: re,
    // eslint-disable-next-line react/no-unstable-nested-components
    popRender() {
      return /* @__PURE__ */ t(
        se,
        {
          customFooter: o.customFilterDialogFooter,
          columns: P,
          options: o,
          filterList: G,
          filterData: H,
          onFilterUpdate: J,
          onFilterReset: K,
          updateFilterByType: W,
          components: p
        }
      );
    }
  });
  const E = [...b, !x, ...a, ...C].filter((e) => !!e).length > 1 && F, M = [], ce = [...b, ...a, ...C].map((e, c) => {
    if (v(e))
      return e;
    const i = g(c);
    if (e.icon) {
      const { popRender: r, icon: y } = e;
      return r && M.push(
        /* @__PURE__ */ t(
          xe,
          {
            open: !!B[i],
            anchorEl: () => B[i],
            onClose: () => {
              m({});
            },
            anchorOrigin: {
              vertical: "bottom",
              horizontal: "right"
            },
            children: /* @__PURE__ */ t("div", { children: r() })
          },
          i
        )
      ), /* @__PURE__ */ t(D, { title: e.title, children: /* @__PURE__ */ t(
        R,
        {
          "data-testid": `${e.title}-iconButton`,
          id: `btn-${i}`,
          "aria-label": e.title,
          onClick: () => {
            e.onClick && e.onClick(), r && m({
              [i]: document.getElementById(`btn-${i}`)
            });
          },
          children: /* @__PURE__ */ t("div", { className: "custom-toolbar-icon", children: y })
        }
      ) }, i);
    }
    return e;
  }), de = [...b, ...a, ...C].map((e, c) => {
    const i = g(c);
    let r;
    if (v(e))
      r = e;
    else if (e.icon) {
      const { icon: y } = e;
      r = /* @__PURE__ */ l(S, { children: [
        /* @__PURE__ */ t(L, { children: y }),
        /* @__PURE__ */ t(j, { children: e.title })
      ] });
    }
    return /* @__PURE__ */ t(
      U,
      {
        onClick: () => {
          d(null), e.onClick && e.onClick(), e.popRender && m({
            [i]: k.current
          });
        },
        children: r
      },
      i
    );
  });
  return /* @__PURE__ */ l("div", { children: [
    /* @__PURE__ */ l(qe, { children: [
      /* @__PURE__ */ t(
        "div",
        {
          className: `custom-toobar-title ${F && O && v(I) ? "toobar-title-hidden" : ""}`,
          children: /* @__PURE__ */ t("div", { className: "custom-toobar-title-inner", children: /* @__PURE__ */ t("span", { children: I }) })
        }
      ),
      /* @__PURE__ */ l("div", { className: `custom-toobar-btns ${u || w ? "toobar-btns-disabled" : ""}`, children: [
        !le && /* @__PURE__ */ t(
          Fe,
          {
            search: oe,
            options: o,
            searchText: Z,
            searchTextUpdate: _,
            searchClose: ee,
            onSearchOpen: A
          }
        ),
        !E && /* @__PURE__ */ l(S, { children: [
          !x && q(({ handlePrint: e }) => /* @__PURE__ */ t("span", { children: /* @__PURE__ */ t(D, { title: f, children: /* @__PURE__ */ t(
            R,
            {
              "data-testid": `${f}-iconButton`,
              "aria-label": f,
              disabled: o.print === "disabled",
              onClick: e,
              children: /* @__PURE__ */ t($, {})
            }
          ) }) })),
          ce
        ] }),
        E && /* @__PURE__ */ t(
          R,
          {
            ref: k,
            "aria-haspopup": "true",
            "aria-expanded": h ? "true" : void 0,
            onClick: (e) => d(e.currentTarget),
            style: { flexShrink: 0 },
            children: /* @__PURE__ */ t(he, {})
          }
        )
      ] }),
      te
    ] }),
    /* @__PURE__ */ l(
      ge,
      {
        anchorEl: h,
        open: !!h,
        onClose: () => d(null),
        slotProps: {
          list: {
            "aria-labelledby": "more-button"
          }
        },
        children: [
          !x && q(({ handlePrint: e }) => /* @__PURE__ */ l(
            U,
            {
              onClick: () => {
                d(null), e();
              },
              children: [
                /* @__PURE__ */ t(L, { children: /* @__PURE__ */ t($, { fontSize: "small" }) }),
                /* @__PURE__ */ t(j, { children: f })
              ]
            }
          )),
          de
        ]
      }
    ),
    M.map((e, c) => /* @__PURE__ */ t("div", { children: e }, g(c))),
    /* @__PURE__ */ t(Ee, { children: u && /* @__PURE__ */ t(ye, { className: "toolbar-progress" }) })
  ] });
}
Be.propTypes = {
  data: n.array,
  options: n.object.isRequired,
  components: n.object,
  columns: n.array.isRequired,
  filterList: n.array,
  filterData: n.array,
  filterUpdate: n.func.isRequired,
  resetFilters: n.func.isRequired,
  updateFilterByType: n.func.isRequired,
  toggleViewColumn: n.func.isRequired,
  updateColumns: n.func.isRequired,
  title: n.any,
  searchText: n.any,
  searchTextUpdate: n.func.isRequired,
  searchClose: n.func.isRequired,
  tableRef: n.func.isRequired
};
const qe = z("div")`
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .custom-toobar-title {
    flex: 1 1 auto;
    font-size: 18px;
    font-weight: 800;
    transition: all ease 0.3s;
    &-inner {
      width: 100%;
      span {
        display: inline-block;
        max-width: 100%;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }
    }
  }
  .custom-toobar-btns {
    display: flex;
    justify-content: center;
    align-items: center;
    &.toobar-btns-disabled {
      position: relative;
      opacity: 0.5;
      &:after {
        position: absolute;
        display: block;
        z-index: 2;
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;
        content: '';
        cursor: not-allowed;
      }
    }
    .custom-toolbar-icon {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 24px;
      height: 24px;
      font-size: 20px;
      overflow: hidden;
    }
  }
  .toobar-title-hidden {
    opacity: 0;
    cursor: none;
  }
`, Ee = z("div")`
  width: 100%;
  height: 2px;
  .toolbar-progress {
    height: 2px;
  }
`;
export {
  Be as default
};
