import { ref as B, computed as C, defineComponent as j, useSlots as O, inject as z, provide as J, watch as Q, openBlock as f, createBlock as m, unref as t, normalizeClass as U, withCtx as u, mergeProps as $, createCommentVNode as p, createElementBlock as Z, createElementVNode as A, renderSlot as G, createVNode as I, createTextVNode as k, toDisplayString as w } from "vue";
import { a as q } from "./index-CLv1CFWG.js";
import { SCREEN_SIZES as H } from "./constants.js";
import { t as d } from "./locale.js";
import { _ as K } from "./Box.vue_vue_type_script_setup_true_lang-DYXudgbs.js";
import _ from "./Button.js";
import { cloneDeep as L } from "es-toolkit";
import "./Module.keys-DcqBbvvT.js";
import P from "./Icon.js";
import { D as R } from "./DataView.keys-aSOnA4AD.js";
import X from "./FilterChip.js";
import { _ as Y } from "./Label.vue_vue_type_script_setup_true_lang-WIpehdiu.js";
import ee from "./Modal.js";
import te from "./SearchBar.js";
import { D as le } from "./DataViewFilters.keys-BLu07FiP.js";
import { i as W } from "./isDefined-DzVx0B6k.js";
var ae = /* @__PURE__ */ ((l) => (l.Cascade = "cascade", l.Nested = "nested", l))(ae || {});
function he({
  schema: l,
  dataViewRef: N
}) {
  const e = B({}), o = B({});
  for (const a in l)
    e.value[a] = l[a].defaultValue, o.value[a] = l[a].defaultValue;
  const y = N;
  function h() {
    e.value = L(o.value), y.value.updateCurrentFilters(e.value, { shouldEmit: !0 });
  }
  function V() {
    for (const a in l)
      e.value[a] = l[a].defaultValue, o.value[a] = l[a].defaultValue;
    y.value.updateCurrentFilters(e.value, { shouldEmit: !0 });
  }
  function b(a) {
    for (const i in l)
      l[i].group === a && (e.value[i] = l[i].defaultValue, o.value[i] = l[i].defaultValue);
    y.value.updateCurrentFilters(e.value, { shouldEmit: !0 });
  }
  function E() {
    o.value = L(e.value);
  }
  const g = C(() => {
    var i;
    const a = {};
    for (const c in l) {
      const n = l[c], r = e.value[c];
      (((i = n.isActive) == null ? void 0 : i.call(n, r)) || W(r)) && n.group && (a[n.group] = (a[n.group] ?? 0) + 1);
    }
    return a;
  }), S = C(() => {
    var i;
    let a = 0;
    for (const c in l) {
      const n = l[c], r = e.value[c];
      (((i = n.isActive) == null ? void 0 : i.call(n, r)) || W(r)) && (a += 1);
    }
    return a;
  });
  return {
    applyFilters: h,
    resetAllFilters: V,
    // @ts-expect-error "could be instantiated with a different subtype": TODO: figure out how to resolve the types
    resetFilterGroup: b,
    undoWorkingFilters: E,
    activeFiltersCounts: g,
    totalActiveFiltersCount: S,
    appliedFilters: e,
    workingFilters: o
  };
}
const se = {
  key: 1,
  class: "col-span-12 row-start-2 md:col-start-7 md:row-start-1 lg:col-span-8 lg:col-start-5"
}, re = { class: "hidden md:block" }, ie = { class: "flex gap-4" }, oe = { class: "inline-flex items-center gap-3" }, ne = { class: "flex justify-end gap-gutter" }, Se = /* @__PURE__ */ j({
  __name: "DataViewFilters",
  props: {
    filtersLabelText: { default: d("ll.filterBy") },
    searchBarProps: { default: void 0 },
    showSearch: { type: Boolean, default: !0 },
    drawerStyle: { default: "cascade" },
    drawerProps: { default: void 0 },
    showDrawerPreviousButton: { type: Boolean, default: !1 },
    useFiltersInstance: { default: void 0 },
    onApply: { type: Function, default: void 0 },
    activeGroup: { default: void 0 }
  },
  emits: ["open-drawer", "dismiss", "previous", "reset-group", "reset-all"],
  setup(l, { emit: N }) {
    const e = l, o = N, y = O(), h = q(`(min-width: ${H.lg})`), V = C(() => e.drawerStyle === "nested" && !e.activeGroup), b = C(() => e.drawerStyle === "nested" && e.activeGroup), {
      density: E,
      isLoading: g,
      isWithinModule: S,
      currentSearch: a,
      updateCurrentSearch: i
    } = z(R.key, R.defaults);
    J(le.key, {
      useFiltersInstance: e.useFiltersInstance,
      drawerStyle: e.drawerStyle
    });
    const c = C(() => {
      var s;
      return ((s = e.useFiltersInstance) == null ? void 0 : s.totalActiveFiltersCount.value) ?? 0;
    }), n = C(
      () => {
        var s;
        return Number(e.activeGroup && ((s = e.useFiltersInstance) == null ? void 0 : s.activeFiltersCounts.value[e.activeGroup])) || 0;
      }
    ), r = B(!1);
    async function D() {
      var v, F;
      const { preventDismiss: s } = await ((v = e.onApply) == null ? void 0 : v.call(e)) || ((F = e.useFiltersInstance) == null ? void 0 : F.applyFilters()) || {};
      s || (r.value = !1);
    }
    function M() {
      var s;
      e.activeGroup && ((s = e.useFiltersInstance) == null || s.resetFilterGroup(e.activeGroup), o("reset-group"), r.value = !1);
    }
    function x() {
      var s;
      (s = e.useFiltersInstance) == null || s.resetAllFilters(), o("reset-all"), r.value = !1;
    }
    function T() {
      var s;
      (s = e.useFiltersInstance) == null || s.undoWorkingFilters(), r.value = !1, o("dismiss");
    }
    return Q(r, () => {
      r.value && o("open-drawer");
    }), (s, v) => (f(), m(K, {
      class: U(["stash-data-view-filters grid grid-cols-12 gap-6 p-3", { "lg:p-6": t(E) === "comfortable", "mb-3": !t(S) }]),
      radius: t(S) ? "none" : "rounded",
      "data-test": "stash-data-view-filters",
      "disable-padding": ""
    }, {
      default: u(() => [
        e.showSearch ? (f(), m(te, $({
          key: 0,
          class: "col-span-12 md:col-span-6 lg:col-span-4",
          "data-test": "stash-data-view-filters|search-bar",
          "is-loading": t(g),
          label: t(d)("ll.search"),
          "model-value": t(a)
        }, e.searchBarProps, {
          onSearch: v[0] || (v[0] = (F) => t(i)(F, { shouldEmit: !0 }))
        }), null, 16, ["is-loading", "label", "model-value"])) : p("", !0),
        y.default ? (f(), Z("div", se, [
          A("div", re, [
            G(s.$slots, "filters-label", {}, () => [
              I(Y, null, {
                default: u(() => [
                  k(w(e.filtersLabelText), 1)
                ]),
                _: 1
              })
            ])
          ]),
          A("div", ie, [
            I(X, {
              secondary: "",
              class: "!md:inline-flex !flex w-full justify-center gap-4 md:w-auto",
              "data-test": "stash-data-view-filters|drawer-toggle-button",
              "filter-count": c.value,
              onClick: v[1] || (v[1] = (F) => r.value = !0)
            }, {
              default: u(() => [
                A("span", oe, [
                  I(P, {
                    name: "filter-line",
                    class: "text-ice-700"
                  }),
                  A("span", null, w(t(d)("ll.filters")), 1)
                ])
              ]),
              _: 1
            }, 8, ["filter-count"]),
            t(h) ? G(s.$slots, "default", { key: 0 }) : p("", !0),
            c.value > 0 && t(h) ? (f(), m(_, {
              key: 1,
              inline: "",
              onClick: x
            }, {
              default: u(() => [
                k(w(t(d)("ll.resetAll")), 1)
              ]),
              _: 1
            })) : p("", !0)
          ])
        ])) : p("", !0),
        y.drawer ? (f(), m(ee, $({
          key: 2,
          "data-test": "stash-data-view-filters|drawer",
          "disable-body-padding": "",
          position: "right",
          size: "narrow",
          "is-open": r.value,
          title: t(d)("ll.allFilters")
        }, e.drawerProps, { onDismiss: T }), {
          headerAction: u(() => [
            b.value ? (f(), m(_, {
              key: 0,
              icon: "",
              class: "text-ice-100",
              "data-test": "stash-data-view-filters|drawer-previous-button",
              "aria-label": t(d)("ll.previous"),
              title: t(d)("ll.previous"),
              onClick: v[2] || (v[2] = (F) => o("previous"))
            }, {
              default: u(() => [
                I(P, { name: "chevron-left" })
              ]),
              _: 1
            }, 8, ["aria-label", "title"])) : p("", !0)
          ]),
          footer: u(() => [
            A("div", ne, [
              c.value === 0 ? (f(), m(_, {
                key: 0,
                secondary: "",
                onClick: T
              }, {
                default: u(() => [
                  k(w(t(d)("ll.cancel")), 1)
                ]),
                _: 1
              })) : p("", !0),
              (V.value || e.drawerStyle === "cascade") && c.value > 0 ? (f(), m(_, {
                key: 1,
                secondary: "",
                disabled: t(g),
                onClick: x
              }, {
                default: u(() => [
                  k(w(t(d)("ll.resetAll")), 1)
                ]),
                _: 1
              }, 8, ["disabled"])) : p("", !0),
              b.value && n.value > 0 ? (f(), m(_, {
                key: 2,
                secondary: "",
                disabled: t(g),
                onClick: M
              }, {
                default: u(() => [
                  k(w(t(d)("ll.reset")), 1)
                ]),
                _: 1
              }, 8, ["disabled"])) : p("", !0),
              b.value || e.drawerStyle === "cascade" ? (f(), m(_, {
                key: 3,
                disabled: t(g),
                onClick: D
              }, {
                default: u(() => [
                  k(w(t(d)("ll.apply")), 1)
                ]),
                _: 1
              }, 8, ["disabled"])) : p("", !0)
            ])
          ]),
          default: u(() => [
            G(s.$slots, "drawer")
          ]),
          _: 3
        }, 16, ["is-open", "title"])) : p("", !0)
      ]),
      _: 3
    }, 8, ["class", "radius"]));
  }
});
export {
  le as DATA_VIEW_FILTERS_UTILS_INJECTION,
  ae as DrawerStyle,
  Se as default,
  he as useFilters
};
//# sourceMappingURL=DataViewFilters.js.map
