import { defineComponent as ce, shallowRef as L, computed as g, toRef as ue, unref as e, watch as N, openBlock as l, createElementBlock as c, normalizeStyle as B, normalizeClass as i, renderSlot as u, createElementVNode as fe, createVNode as b, createCommentVNode as O, Fragment as A, createBlock as $, withCtx as C, renderList as de, mergeProps as me } from "vue";
import { Empty as M, Spin as ge, Pagination as pe } from "@aplus-frontend/antdv";
import { throttle as Se, isUndefined as j, isNumber as ye, omit as ve } from "lodash-unified";
import { RecycleScroller as he, DynamicScroller as be, DynamicScrollerItem as ke } from "vue-virtual-scroller";
import "vue-virtual-scroller/dist/vue-virtual-scroller.css";
import { useTablePaging as Ie } from "../ap-table/hooks/use-table-paging-ng.mjs";
import "../config-provider/index.mjs";
import { useOfflineList as Pe } from "./hooks/use-offline-list.mjs";
import we from "./style/index.mjs";
import { useNamespace as De } from "../config-provider/hooks/use-namespace.mjs";
const Oe = /* @__PURE__ */ ce({
  name: "ApList",
  __name: "index",
  props: {
    dataSource: {},
    loading: { type: Boolean, default: void 0 },
    onItem: {},
    params: {},
    request: {},
    defaultData: {},
    onLoadingChange: {},
    beforeSearchSubmit: {},
    pagination: { type: [Boolean, Object], default: void 0 },
    manual: { type: Boolean },
    containerStyle: { type: [Boolean, null, String, Object, Array] },
    contentStyle: { type: [Boolean, null, String, Object, Array] },
    footerWarpperStyle: { type: [Boolean, null, String, Object, Array] },
    virtual: { default: void 0 },
    rowKey: {},
    adaptive: { type: Boolean, default: !1 },
    fetchMode: { default: "paging" }
  },
  setup(o, { expose: q }) {
    const K = M.PRESENTED_IMAGE_SIMPLE, a = o, { b: p, e: k, bm: x, m: H } = De("ap-list"), W = we("ap-list"), f = L(), S = L(), y = g(() => a.fetchMode === "paging"), {
      formRef: I,
      submit: G,
      reset: U,
      handleTableChange: J,
      fetchNextPageAndCache: E,
      data: P,
      refresh: Q,
      refreshByDelete: X,
      tableProps: w,
      getPaging: Y,
      setPaging: Z
    } = Ie({
      async request(t) {
        const n = await a.request?.(t);
        return {
          data: n?.data || [],
          total: n?.total || 0
        };
      },
      params: ue(a, "params"),
      defaultData: a.defaultData,
      manual: a.manual,
      pagination: a.pagination,
      formatParams: a.beforeSearchSubmit
    });
    let v = 0;
    function _() {
      const t = f.value;
      if (y.value || !t)
        return;
      const n = t.scrollTop, r = t.scrollHeight - t.clientHeight;
      v >= r && (v = 0), !(v > n) && r - n <= 1 && (v = n, E());
    }
    function z() {
      !y.value && E();
    }
    const T = Se(_, 300), ee = { form: I }, {
      pagination: F,
      dataSource: te,
      onChange: ae
    } = Pe(a);
    function ne(t, n) {
      if (e(h)) {
        ae(t, n);
        return;
      }
      J(
        { current: t, pageSize: n },
        {},
        {},
        { currentDataSource: [], action: "paginate" }
      );
    }
    const d = g(() => e(h) ? e(te) : [...e(P).records || []]), m = g(() => j(a.loading) ? e(P).loading : a.loading), re = g(() => a.pagination === !1 || a.virtual ? !1 : a.pagination?.hideOnSinglePage !== !0 ? !0 : e(h) ? (a.dataSource?.length || 0) > e(F).pageSize : e(P).total > e(w).pagination.pageSize), h = g(() => !j(a.dataSource));
    N(
      () => e(m),
      (t) => {
        a.onLoadingChange?.(t);
      }
    ), N(
      () => a.params,
      () => {
        D();
      },
      {
        deep: !0
      }
    );
    function oe(t) {
      f.value?.querySelector(t || ":first-child")?.scrollIntoView({ behavior: "smooth", block: "center" });
    }
    function le(t = !1) {
      const n = I.value?.apForm;
      return (t ? n?.getFieldsValueTransformed : n?.getFieldsValue)?.() || {};
    }
    function ie(t) {
      I.value?.apForm?.setFieldsValue?.(t);
    }
    function D() {
      y.value || (f.value && (f.value.scrollTop = 0), S.value && S.value.scrollToPosition?.(0));
    }
    function R() {
      D(), G();
    }
    function V() {
      D(), U();
    }
    return q({
      submit: R,
      reset: V,
      refresh: () => Q(),
      refreshByDelete: (t) => X(t),
      scrollToFirstRow: oe,
      getSearchFormValuesIfSetted: le,
      setPaging: Z,
      getPaging: Y,
      setSearchFormValuesIfSetted: ie
    }), (t, n) => (l(), c("div", {
      class: i([e(p)(), { [e(H)("adaptive")]: o.adaptive }, e(W)]),
      style: B(o.containerStyle)
    }, [
      u(t.$slots, "header", {
        formIns: ee,
        submit: R,
        reset: V,
        loading: e(w).loading
      }),
      fe("div", {
        class: i([
          e(p)("content-container"),
          { [e(x)("content-container", "spinning")]: m.value }
        ])
      }, [
        m.value ? (l(), c("div", {
          key: 0,
          class: i(e(k)("spin-wrapper"))
        }, [
          b(e(ge), { spinning: m.value }, null, 8, ["spinning"])
        ], 2)) : O("", !0),
        d.value.length === 0 ? u(t.$slots, "empty", {
          key: 1,
          loading: m.value
        }, () => [
          b(e(M), { image: e(K) }, null, 8, ["image"])
        ]) : (l(), c("div", {
          key: 2,
          ref_key: "contentRef",
          ref: f,
          style: B(o.contentStyle),
          class: i(e(k)("content")),
          onScroll: n[0] || (n[0] = //@ts-ignore
          (...r) => e(T) && e(T)(...r))
        }, [
          o.virtual ? (l(), c(A, { key: 0 }, [
            e(ye)(o.virtual) ? (l(), $(e(he), {
              key: 0,
              ref_key: "scroller",
              ref: S,
              class: i(e(p)("scroll-wrapper")),
              items: d.value,
              "item-size": o.virtual,
              "key-field": a.rowKey || "id",
              onScrollEnd: z
            }, {
              default: C(({ item: r, index: s }) => [
                u(t.$slots, "renderItem", {
                  item: r,
                  index: s
                })
              ]),
              _: 3
            }, 8, ["class", "items", "item-size", "key-field"])) : (l(), $(e(be), {
              key: 1,
              ref_key: "scroller",
              ref: S,
              items: d.value,
              "min-item-size": o.virtual.minItemSize,
              class: i(e(p)("scroll-wrapper")),
              "key-field": a.rowKey || "id",
              onScrollEnd: z
            }, {
              default: C(({ item: r, index: s, active: se }) => [
                b(e(ke), {
                  item: r,
                  active: se,
                  "data-index": s,
                  "size-dependencies": o.virtual.sizeDependencies?.(r)
                }, {
                  default: C(() => [
                    u(t.$slots, "renderItem", {
                      item: r,
                      index: s
                    })
                  ]),
                  _: 2
                }, 1032, ["item", "active", "data-index", "size-dependencies"])
              ]),
              _: 3
            }, 8, ["items", "min-item-size", "class", "key-field"]))
          ], 64)) : u(t.$slots, "renderContent", {
            key: 1,
            dataSource: d.value
          }, () => [
            (l(!0), c(A, null, de(d.value, (r, s) => u(t.$slots, "renderItem", {
              item: r,
              index: s
            })), 256))
          ])
        ], 38))
      ], 2),
      y.value && re.value ? (l(), c("div", {
        key: 0,
        class: i(e(k)("footer")),
        style: B(o.footerWarpperStyle)
      }, [
        b(e(pe), me(
          h.value ? e(F) : e(ve)(e(w).pagination, ["showTotal"]),
          { onChange: ne }
        ), null, 16)
      ], 6)) : O("", !0)
    ], 6));
  }
});
export {
  Oe as default
};
