import { a as H } from "../../../../@react-spring/web/dist/react-spring-web.esm.mjs";
import { useSize as $ } from "ahooks";
import y, { useRef as R } from "react";
import { bound as w } from "../../utils/bound.mjs";
import { getScaleX as Y, create as T, getTranslateX as k, getTranslateY as A, translate as b, scale as W, apply as C } from "../../utils/matrix.mjs";
import { rubberbandIfOutOfBounds as B } from "../../utils/rubberband.mjs";
import { useDragAndPinch as j } from "../../utils/use-drag-and-pinch.mjs";
import { useSpring as q } from "../../../../@react-spring/core/dist/react-spring-core.esm.mjs";
const z = "adm-image-viewer", et = (d) => {
  const {
    dragLockRef: g,
    maxZoom: I,
    imageRender: E,
    index: D
  } = d, L = R([]), S = R(null), M = R(null), [{
    matrix: u
  }, p] = q(() => ({
    matrix: T(),
    config: {
      tension: 200
    }
  })), n = $(S), s = $(M), O = R(!1), Z = (t) => {
    if (!n || !s)
      return {
        x: {
          position: 0,
          minX: 0,
          maxX: 0
        },
        y: {
          position: 0,
          minY: 0,
          maxY: 0
        }
      };
    const r = -n.width / 2, c = -n.height / 2, e = -s.width / 2, o = -s.height / 2, a = Y(t), m = a * s.width, l = a * s.height, f = r - (m - n.width), i = r, X = c - (l - n.height), v = c, [h, x] = C(t, [e, o]);
    return {
      x: {
        position: h,
        minX: f,
        maxX: i
      },
      y: {
        position: x,
        minY: X,
        maxY: v
      }
    };
  }, _ = (t, r, c, e = 0) => [t <= r - e, t >= c + e], N = (t, r, c = !1) => {
    if (!n || !s)
      return t;
    const e = Y(t), o = e * s.width, a = e * s.height, {
      x: {
        position: m,
        minX: l,
        maxX: f
      },
      y: {
        position: i,
        minY: X,
        maxY: v
      }
    } = Z(t);
    if (r === "translate") {
      let h = m, x = i;
      return o > n.width ? h = c ? w(m, l, f) : B(m, l, f, e * 50) : h = -o / 2, a > n.height ? x = c ? w(i, X, v) : B(i, X, v, e * 50) : x = -a / 2, b(t, h - m, x - i);
    }
    if (r === "scale" && c) {
      const [h, x] = [o > n.width ? w(m, l, f) : -o / 2, a > n.height ? w(i, X, v) : -a / 2];
      return b(t, h - m, x - i);
    }
    return t;
  };
  j({
    onDrag: (t) => {
      var r;
      if (t.first) {
        const {
          x: {
            position: e,
            minX: o,
            maxX: a
          }
        } = Z(u.get());
        L.current = _(e, o, a);
        return;
      }
      if (t.pinching)
        return t.cancel();
      if (t.tap && t.elapsedTime > 0 && t.elapsedTime < 1e3) {
        (r = d.onTap) === null || r === void 0 || r.call(d);
        return;
      }
      const c = Y(u.get());
      if (g && (g.current = c !== 1), !O.current && c <= 1)
        p.start({
          matrix: T()
        });
      else {
        const e = u.get(), o = [t.offset[0] - k(e), t.offset[1] - A(e)], a = b(e, ...t.last ? [o[0] + t.velocity[0] * t.direction[0] * 200, o[1] + t.velocity[1] * t.direction[1] * 200] : o);
        p.start({
          matrix: N(a, "translate", t.last),
          immediate: !t.last
        });
        const {
          x: {
            position: m,
            minX: l,
            maxX: f
          }
        } = Z(a);
        t.last && L.current.some((i) => i) && _(m, l, f).some((i) => i) && (g && (g.current = !1), p.start({
          matrix: T()
        }));
      }
    },
    onPinch: (t) => {
      var r;
      O.current = !t.last;
      const [c] = t.offset;
      if (c < 0)
        return;
      let e;
      I === "auto" ? e = n && s ? Math.max(n.height / s.height, n.width / s.width) : 1 : e = I;
      const o = t.last ? w(c, 1, e) : c;
      if ((r = d.onZoomChange) === null || r === void 0 || r.call(d, o), t.last && o <= 1)
        p.start({
          matrix: T()
        }), g && (g.current = !1);
      else {
        if (!n)
          return;
        const a = u.get(), m = Y(a), l = t.origin[0] - n.width / 2, f = t.origin[1] - n.height / 2;
        let i = b(a, -l, -f);
        i = W(i, o / m), i = b(i, l, f), p.start({
          matrix: N(i, "scale", t.last),
          immediate: !t.last
        }), g && (g.current = !0);
      }
    }
  }, {
    target: S,
    drag: {
      from: () => [k(u.get()), A(u.get())],
      pointer: {
        touch: !0
      }
    },
    pinch: {
      from: () => [Y(u.get()), 0],
      pointer: {
        touch: !0
      }
    }
  });
  const P = typeof E == "function" && E(d.image, {
    index: D
  });
  return y.createElement("div", {
    className: `${z}-slide`
  }, y.createElement("div", {
    className: `${z}-control`,
    ref: S
  }, y.createElement(H.div, {
    className: `${z}-image-wrapper`,
    style: {
      matrix: u
    }
  }, P || y.createElement("img", {
    ref: M,
    src: d.image,
    draggable: !1,
    alt: d.image
  }))));
};
export {
  et as Slide
};
