import { defineComponent as U, useCssModule as J, ref as l, inject as W, computed as n, watch as z, provide as G, openBlock as I, createElementBlock as Z, renderSlot as q, createBlock as H, normalizeClass as K, unref as Q, createCommentVNode as X } from "vue";
import { cloneDeep as Y } from "es-toolkit";
import { M as V } from "./Module.keys-DcqBbvvT.js";
import w from "./Paginate.js";
import { a as O, D as tt } from "./DataView.keys-aSOnA4AD.js";
import { _ as et } from "./_plugin-vue_export-helper-CHgC5LLL.js";
function x(e) {
  const o = e[0] === "-" ? "desc" : "asc";
  return { id: o === "asc" ? e : e.slice(1), order: o };
}
function at({ id: e, order: o }) {
  return `${o === "desc" ? "-" : ""}${e}`;
}
function nt({ newId: e, newOrder: o, oldId: f, oldOrder: t }) {
  if (e) {
    if (e !== f)
      return at({ id: e, order: o || "asc" });
    if (!o && !t)
      return e;
    if (!(!o && t === "desc")) {
      if (!o && t === "asc")
        return `-${e}`;
      if (o === "asc" && !t)
        return e;
      if (!(o === "asc" && t === "asc")) {
        if (o === "asc" && t === "desc")
          return e;
        if (o === "desc" && !t)
          return `-${e}`;
        if (!(o === "desc" && t === "desc") && o === "desc" && t === "asc")
          return `-${e}`;
      }
    }
  }
}
var ot = /* @__PURE__ */ ((e) => (e.Table = "table", e))(ot || {});
const it = /* @__PURE__ */ U({
  __name: "DataView",
  props: {
    currentPage: { default: 1 },
    currentSearch: { default: void 0 },
    currentSort: { default: void 0 },
    data: { default: () => [] },
    density: { default: "comfortable" },
    pageSize: { default: O },
    disablePagination: { type: Boolean, default: !1 },
    hideBottomPagination: { type: Boolean, default: !1 },
    totalDataCount: { default: 0 },
    hasNextPage: { type: Boolean, default: void 0 },
    variant: { default: void 0 },
    isLoading: { type: Boolean }
  },
  emits: ["update", "update:currentFilters", "update:currentPage", "update:currentSearch", "update:currentSort"],
  setup(e, { expose: o, emit: f }) {
    const t = e, d = f, $ = J(), P = l(null), C = W(V.key, V.defaults), L = n(() => {
      var a;
      if (t.variant)
        return t.variant;
      if (((a = C.variant) == null ? void 0 : a.value) === "table")
        return "table";
    }), _ = n(() => C.variant !== void 0), T = n(() => {
      var a;
      return (a = C.includeDataviewPagination) == null ? void 0 : a.value;
    }), u = l(1), B = n(() => !t.disablePagination && t.totalDataCount > 0), E = n(() => t.totalDataCount === 1 / 0 && !t.hasNextPage), v = n(() => Math.ceil(t.totalDataCount / t.pageSize)), r = function(a, { shouldEmit: i } = {}) {
      var c, h;
      u.value = a, i && (S(), d("update:currentPage", a, t.pageSize)), Number((c = P.value) == null ? void 0 : c.getBoundingClientRect().top) < 0 && ((h = P.value) == null || h.scrollIntoView({ behavior: "smooth", block: "start" }));
    }, D = function({ shouldEmit: a } = {}) {
      const i = Math.max(u.value - 1, 1);
      r(i, { shouldEmit: a });
    }, b = function({ shouldEmit: a } = {}) {
      const i = Math.min(u.value + 1, v.value);
      r(i, { shouldEmit: a });
    };
    z(
      () => t.currentPage,
      () => {
        r(t.currentPage);
      },
      { immediate: !0 }
    );
    const s = l(void 0), N = n(
      () => s.value ? x(s.value).id : void 0
    ), M = n(
      () => s.value ? x(s.value).order : void 0
    ), g = function(a, { sortOrder: i, shouldEmit: c } = {}) {
      const { id: h, order: R } = x(s.value || ""), k = nt({ newId: a, newOrder: i, oldId: h, oldOrder: R });
      s.value = k, r(1), c && (S(), d("update:currentSort", k));
    };
    z(
      () => t.currentSort,
      () => {
        g(t.currentSort);
      },
      { immediate: !0 }
    );
    const p = l(""), j = l(!1), A = l(!1), m = function(a, { shouldEmit: i } = {}) {
      p.value = a || "", r(1), i && (S(), d("update:currentSearch", a));
    };
    z(
      () => t.currentSearch,
      () => {
        m(t.currentSearch);
      },
      { immediate: !0 }
    );
    const y = l({}), F = function(a, { shouldEmit: i } = {}) {
      y.value = a, r(1), i && (S(), d("update:currentFilters", a));
    };
    function S() {
      d("update", {
        page: u.value,
        pageSize: t.pageSize,
        ordering: s.value,
        search: p.value,
        filters: Y(y.value)
      });
    }
    return G(
      tt.key,
      Object.freeze({
        // Pagination
        currentPage: n(() => u.value),
        isPaginationEnabled: B,
        isPaginateNextDisabled: E,
        pageCount: v,
        pageSize: n(() => t.pageSize),
        totalDataCount: n(() => t.totalDataCount),
        setPage: r,
        goPrevPage: D,
        goNextPage: b,
        // Sorting
        currentSortId: N,
        currentSortOrder: M,
        updateCurrentSort: g,
        // Search
        currentSearch: n(() => p.value),
        updateCurrentSearch: m,
        // Miscellaneous
        density: n(() => t.density),
        hasToolbar: j,
        isEmpty: n(() => t.data.length === 0),
        isSelectable: A,
        isLoading: n(() => t.isLoading),
        isWithinModule: _,
        variant: L
      })
    ), o({
      page: n(() => u.value),
      pageSize: n(() => t.pageSize),
      setPage: r,
      goPrevPage: D,
      goNextPage: b,
      ordering: n(() => s.value),
      updateCurrentSort: g,
      search: n(() => p.value),
      updateCurrentSearch: m,
      filters: n(() => y.value),
      updateCurrentFilters: F
    }), (a, i) => (I(), Z("div", {
      ref_key: "rootRef",
      ref: P,
      class: "stash-data-view relative",
      "data-test": "stash-data-view"
    }, [
      q(a.$slots, "default", {
        currentPage: u.value,
        currentSortId: N.value,
        currentSortOrder: M.value,
        data: e.data,
        goPrevPage: D,
        goNextPage: b,
        pageCount: v.value,
        setPage: r,
        totalDataCount: t.totalDataCount,
        updateCurrentSort: g,
        updateCurrentSearch: m
      }),
      B.value && (!_.value || T.value) && v.value > 1 && !t.hideBottomPagination ? (I(), H(w, {
        key: 0,
        class: K(["mt-6", Q($).paginate]),
        "current-page": u.value,
        "is-next-disabled": e.isLoading || E.value,
        "list-length": t.totalDataCount,
        "page-size": t.pageSize,
        variant: t.totalDataCount === 1 / 0 ? "stats" : "standard",
        onSetPage: i[0] || (i[0] = (c) => r(c, { shouldEmit: !0 }))
      }, null, 8, ["class", "current-page", "is-next-disabled", "list-length", "page-size", "variant"])) : X("", !0)
    ], 512));
  }
}), rt = "_paginate_0j6w6", ut = {
  paginate: rt
}, st = {
  $style: ut
}, pt = /* @__PURE__ */ et(it, [["__cssModules", st]]);
export {
  tt as DATA_VIEW_INJECTION,
  O as DEFAULT_PAGE_SIZE,
  ot as DataViewVariant,
  pt as default,
  x as deserializeSortValue,
  nt as getNextSort,
  at as serializeSortValue
};
//# sourceMappingURL=DataView.js.map
