import { defineComponent as J, ref as o, computed as u, openBlock as v, createElementBlock as m, normalizeClass as c, createElementVNode as n, normalizeStyle as K, renderSlot as d, toDisplayString as E, createVNode as $, unref as b, withCtx as D, createCommentVNode as L } from "vue";
import { DynamicScroller as O, DynamicScrollerItem as Q } from "vue-virtual-scroller";
/* empty css                                      */
const U = { class: "ptr-track" }, W = { class: "ptr-text" }, X = {
  key: 0,
  class: "ptr-scroller-wrap"
}, Z = {
  key: 0,
  class: "ptr-loadmore"
}, ee = {
  key: 1,
  class: "ptr-finished"
}, ie = /* @__PURE__ */ J({
  name: "ClPullToRefresh",
  __name: "Index",
  props: {
    className: String,
    disabled: Boolean,
    // desc: 下拉刷新阈值(px)
    threshold: {
      type: Number,
      default: 60
    },
    // desc: 列表数据，传入则启用虚拟滚动渲染
    list: {
      type: Array,
      default: void 0
    },
    // desc: 列表项最小高度(px)，用于 DynamicScroller 初始估算
    minItemSize: {
      type: Number,
      default: 60
    },
    // desc: 列表项高度依赖项提取函数，用于动态高度重算
    sizeDependencies: {
      type: Function,
      default: void 0
    },
    // desc: 是否已加载完全部数据
    finished: Boolean,
    // desc: 是否正在加载更多
    loading: Boolean,
    // desc: 下拉刷新箭头图标类名（<i> 元素）
    pullIcon: {
      type: String,
      default: "ptr-icon-arrow"
    },
    // desc: 加载更多时的 loading 图标类名（<i> 元素）
    loadingIcon: {
      type: String,
      default: "ptr-icon-spinner"
    },
    // desc: 加载中提示文字
    loadingText: {
      type: String,
      default: "加载中..."
    },
    // desc: 已加载全部提示文字
    finishedText: {
      type: String,
      default: "没有更多了"
    }
  },
  emits: ["refresh", "loadMore"],
  setup(t, { expose: w, emit: B }) {
    const s = t, I = B, C = o(), i = o("idle"), r = o(0), N = o(0), f = o(!1), a = o(!1), x = o(0);
    let l = null, p = 0;
    const h = u(() => s.threshold || 60), g = u(() => Array.isArray(s.list)), Y = u(() => {
      const e = {};
      return s.disabled && (e["ptr-disabled"] = !0), a.value && (e["ptr-is-pulling"] = !0), s.className && (e[s.className] = !0), e;
    }), F = u(() => ({
      height: `${r.value}px`
    })), V = u(() => {
      switch (i.value) {
        case "loading":
          return "加载中...";
        case "ready":
          return "释放即可刷新…";
        case "pulling":
          return "下拉即可刷新…";
        default:
          return "";
      }
    }), _ = () => {
      if (g.value)
        return x.value <= 0;
      const e = C.value;
      return e ? e.scrollTop <= 0 : !0;
    }, A = (e) => "touches" in e ? e.touches[0].clientY : e.clientY, R = (e) => {
      x.value = e?.target?.scrollTop || 0;
    }, q = () => {
      !s.finished && !s.loading && I("loadMore");
    }, j = () => {
      document.addEventListener("mousemove", y), document.addEventListener("mouseup", S);
    }, G = () => {
      document.removeEventListener("mousemove", y), document.removeEventListener("mouseup", S);
    }, M = (e) => {
      s.disabled || i.value === "loading" || (N.value = A(e), f.value = !0, "touches" in e || j());
    }, y = (e) => {
      f.value && (p = A(e) - N.value, p > 0 && _() ? (a.value || (a.value = !0), e.preventDefault(), l === null && (l = requestAnimationFrame(() => {
        l = null;
        const z = p * 0.4;
        r.value = z, i.value = z >= h.value ? "ready" : "pulling";
      }))) : a.value && T());
    }, S = () => {
      f.value && (f.value = !1, G(), l !== null && (cancelAnimationFrame(l), l = null), a.value && i.value === "ready" ? (i.value = "loading", r.value = h.value, I("refresh")) : T());
    }, T = () => {
      l !== null && (cancelAnimationFrame(l), l = null), a.value = !1, r.value = 0, i.value = "idle";
    };
    return w({ finishRefresh: () => {
      T();
    } }), (e, z) => (v(), m("div", {
      class: c(["cl-pulltorefresh", Y.value])
    }, [
      n("div", U, [
        n("div", {
          class: "ptr-indicator",
          style: K(F.value)
        }, [
          n("div", {
            class: c(["ptr-indicator-inner", `ptr-state-${i.value}`])
          }, [
            d(e.$slots, "indicator", {
              state: i.value,
              distance: r.value,
              threshold: h.value
            }, () => [
              n("i", {
                class: c(t.pullIcon)
              }, null, 2),
              n("span", W, E(V.value), 1)
            ])
          ], 2)
        ], 4),
        n("div", {
          ref_key: "refElContent",
          ref: C,
          class: c(["ptr-content", { "ptr-content-virtual": g.value }]),
          onTouchstart: M,
          onTouchmove: y,
          onTouchend: S,
          onMousedown: M
        }, [
          g.value ? (v(), m("div", X, [
            $(b(O), {
              class: "ptr-scroller",
              items: t.list,
              "min-item-size": t.minItemSize,
              "key-field": "id",
              onScroll: R,
              onScrollEnd: q
            }, {
              default: D(({ item: k, index: H, active: P }) => [
                $(b(Q), {
                  item: k,
                  active: P,
                  "size-dependencies": t.sizeDependencies ? t.sizeDependencies(k) : void 0
                }, {
                  default: D(() => [
                    d(e.$slots, "item", {
                      item: k,
                      index: H,
                      active: P
                    })
                  ]),
                  _: 2
                }, 1032, ["item", "active", "size-dependencies"])
              ]),
              after: D(() => [
                t.loading ? (v(), m("div", Z, [
                  d(e.$slots, "loadMore", {}, () => [
                    n("i", {
                      class: c(t.loadingIcon)
                    }, null, 2),
                    n("span", null, E(t.loadingText), 1)
                  ])
                ])) : L("", !0),
                t.finished && !t.loading ? (v(), m("div", ee, [
                  d(e.$slots, "finished", {}, () => [
                    n("span", null, E(t.finishedText), 1)
                  ])
                ])) : L("", !0)
              ]),
              _: 3
            }, 8, ["items", "min-item-size"])
          ])) : d(e.$slots, "default", { key: 1 })
        ], 34)
      ])
    ], 2));
  }
});
export {
  ie as default
};
//# sourceMappingURL=ClPullToRefresh.js.map
