import { defineComponent as pe, ref as me, reactive as ye, computed as f, unref as a, watchEffect as L, watch as de, openBlock as k, createElementBlock as ve, withModifiers as O, normalizeStyle as H, normalizeClass as B, createElementVNode as W, renderSlot as xe, createBlock as I, mergeProps as T, createCommentVNode as _ } from "vue";
import { wait as R } from "@aplus-frontend/utils";
import { useElementSize as j, useRafFn as ge } from "@vueuse/core";
import { isString as F } from "lodash-unified";
import "../config-provider/index.mjs";
import "../scroll-bar/index.mjs";
import { ensureRangeValue as n, getScrollable as N } from "../scroll-bar/utils/index.mjs";
import "../utils/index.mjs";
import { useDevWarning as we } from "../utils/warning.mjs";
import { useScrollBack as Be } from "./hooks/use-scroll-back.mjs";
import { useScrollSmooth as Se } from "./hooks/use-scroll-smooth.mjs";
import be from "./hooks/use-scroll-wrapper.mjs";
import Pe from "./style/index.mjs";
import { getElementScrollOffset as ze } from "./utils/index.mjs";
import { useNamespace as ke } from "../config-provider/hooks/use-namespace.mjs";
import { isDef as u } from "../utils/is.mjs";
import q from "../scroll-bar/internal.vue.mjs";
const _e = /* @__PURE__ */ pe({
  name: "ScrollView",
  __name: "index",
  props: {
    width: {},
    heigth: {},
    height: {},
    showXBar: { type: Boolean, default: !0 },
    showYBar: { type: Boolean, default: !0 },
    autoPlay: { type: [Boolean, Object] },
    xBarProps: { default: () => ({}) },
    yBarProps: { default: () => ({}) },
    onWheel: {}
  },
  setup(g, { expose: G }) {
    const o = g, { deprecated: J } = we("ScrollView");
    J(o.heigth === void 0, "heigth", "height");
    const K = {
      direction: "vertical",
      rate: 1,
      repeat: !0
    }, { b: Q, e: S, em: V } = ke("scroll-view"), U = Pe("scroll-view"), h = me(), c = ye({ x: 30, y: 30 });
    let b = !1;
    const {
      scrollOffset: r,
      maxScrollDistance: i,
      xBarRef: Z,
      yBarRef: ee,
      containerRef: s,
      scrollYBar: p,
      scrollXBar: m,
      updateMaxScrollDistance: te
    } = be(), { width: re, height: oe } = j(h), { width: ae, height: le } = j(s), { start: $ } = Be({
      scrollOffset: r,
      maxScrollDistance: i,
      onScroll: (e) => {
        e === "horizontal" ? m() : p();
      },
      async onScrollEnd() {
        await R(200), P();
      }
    }), { start: E } = Se({
      scrollOffset: r,
      maxScrollDistance: i,
      onScroll(e, t) {
        D.value && (y(), b = !0), e && m(), t && p();
      },
      async onScrollEnd() {
        b && (await R(200), P(), b = !1);
      }
    }), { resume: P, pause: y, isActive: D } = ge(
      () => {
        if (!o.autoPlay) {
          y();
          return;
        }
        const e = o.autoPlay === !0 ? {} : o.autoPlay, t = {
          ...K,
          ...e
        };
        if (t.direction === "horizontal") {
          if (r.x === i.x) {
            y(), t.repeat && $("horizontal");
            return;
          }
          r.x = n(
            r.x + t.rate,
            0,
            i.x
          ), m();
        } else {
          if (r.y === i.y) {
            y(), t.repeat && $("vertical");
            return;
          }
          r.y = n(
            r.y + t.rate,
            0,
            i.y
          ), p();
        }
      },
      { immediate: !0 }
    ), d = f(() => o.showXBar && c.x !== 100), w = f(() => o.showYBar && c.y !== 100), v = f(() => o.heigth ?? o.height), ie = f(() => ({
      width: u(o.width) ? F(o.width) ? o.width : Number.isNaN(o.width) ? void 0 : `${o.width}px` : void 0,
      height: u(v.value) ? F(v.value) ? v.value : Number.isNaN(v.value) ? void 0 : `${v.value}px` : void 0,
      padding: `${o.xBarProps.trackWidth || 10}px ${o.yBarProps.trackWidth || 10}px`
    }));
    async function z() {
      D.value && (y(), await R(500), P());
    }
    async function ne(e) {
      if (a(w))
        e.preventDefault(), e.stopPropagation();
      else
        return;
      r.y = n(
        r.y + e.deltaY,
        0,
        i.y
      ), p(), o.onWheel?.("vertical", r.y), z();
    }
    function se(e) {
      if (a(d))
        e.preventDefault(), e.stopPropagation();
      else
        return;
      r.x = n(
        r.x + e.deltaX,
        0,
        i.x
      ), m(), o.onWheel?.("horizontal", r.x), z();
    }
    function X() {
      if (o.showYBar) {
        const { visibleSize: e, totalSize: t } = N(
          s.value,
          "vertical"
        );
        c.y = n(
          Math.min(e / (t || 1) * 100)
        );
      }
      if (o.showXBar) {
        const { visibleSize: e, totalSize: t } = N(
          s.value,
          "horizontal"
        );
        c.x = n(
          Math.min(e / (t || 1) * 100)
        );
      }
    }
    function C(e, t) {
      const l = a(s), x = a(h);
      if (!l || !x) return;
      const { visibleSize: Y, totalSize: M } = N(l, t);
      if (M <= Y) return;
      const A = (M - Y) * e / 100, he = t === "vertical";
      if (z(), he) {
        r.y = A;
        return;
      }
      r.x = A;
    }
    L(
      () => {
        a(s.value) && X();
      },
      { flush: "post" }
    ), de(
      () => [
        re.value,
        oe.value,
        ae.value,
        le.value
      ],
      () => {
        X(), te();
      }
    ), L(() => {
      h.value && (h.value.style.transform = `translate3d(-${r.x}px, -${r.y}px, 0px)`);
    });
    function ce(e, t) {
      u(e) && d.value && (r.x = n(e, 0, i.x), m()), u(t) && w.value && (r.y = n(t, 0, i.y), p());
    }
    function fe({ smooth: e, left: t, top: l }) {
      e ? E({ x: t, y: l }) : (u(t) && (r.x = t), u(l) && (r.y = l));
    }
    function ue(e, t = "start") {
      const l = ze(
        s.value,
        e,
        t
      ), x = {};
      d.value && (x.x = n(
        r.x + l.x,
        0,
        i.x
      )), w.value && (x.y = n(
        r.y + l.y,
        0,
        i.y
      )), E(x);
    }
    return G({
      scrollTop: f(() => r.y),
      scrollLeft: f(() => r.x),
      scroll: ce,
      scrollTo: fe,
      scrollIntoView: ue
    }), (e, t) => (k(), ve("div", {
      ref_key: "containerRef",
      ref: s,
      class: B([a(Q)(), a(U)]),
      style: H(ie.value),
      onWheel: [
        O(ne, ["exact"]),
        O(se, ["shift"])
      ]
    }, [
      W("div", {
        ref_key: "contentRef",
        ref: h,
        class: B(a(S)("content"))
      }, [
        xe(e.$slots, "default")
      ], 2),
      W("div", {
        class: B([a(S)("bar-wrapper"), a(V)("bar-wrapper", "vertical")]),
        style: H(`height: calc(100% - ${d.value ? g.xBarProps.trackWidth || 10 : 0}px)`)
      }, [
        w.value ? (k(), I(a(q), T({
          key: 0,
          ref_key: "yBarRef",
          ref: ee
        }, g.yBarProps, {
          "bar-length": c.y,
          onScroll: t[0] || (t[0] = (l) => C(l, "vertical"))
        }), null, 16, ["bar-length"])) : _("", !0)
      ], 6),
      W("div", {
        class: B([a(S)("bar-wrapper"), a(V)("bar-wrapper", "horizontal")])
      }, [
        d.value ? (k(), I(a(q), T({
          key: 0,
          ref_key: "xBarRef",
          ref: Z
        }, g.xBarProps, {
          direction: "horizontal",
          "bar-length": c.x,
          onScroll: t[1] || (t[1] = (l) => C(l, "horizontal"))
        }), null, 16, ["bar-length"])) : _("", !0)
      ], 2)
    ], 38));
  }
});
export {
  _e as default
};
