import M from "./index-extends.js";
import * as g from "react";
import Re from "./index-areArraysEqual.js";
import $e from "./index-useControlled2.js";
import R from "./index-clamp2.js";
import Fe from "./index-useIsFocusVisible2.js";
import fe from "./index-useForkRef.js";
import Ne from "./index-useEnhancedEffect2.js";
import Q from "./index-useEventCallback2.js";
import U from "./index-ownerDocument2.js";
import W from "./index-extractEventHandlers.js";
import Oe from "./index-visuallyHidden.js";
const _e = 2;
function ge(r, o) {
  return r - o;
}
function de(r, o) {
  var a;
  const {
    index: i
  } = (a = r.reduce((l, x, T) => {
    const p = Math.abs(o - x);
    return l === null || p < l.distance || p === l.distance ? {
      distance: p,
      index: T
    } : l;
  }, null)) != null ? a : {};
  return i;
}
function K(r, o) {
  if (o.current !== void 0 && r.changedTouches) {
    const a = r;
    for (let i = 0; i < a.changedTouches.length; i += 1) {
      const l = a.changedTouches[i];
      if (l.identifier === o.current)
        return {
          x: l.clientX,
          y: l.clientY
        };
    }
    return !1;
  }
  return {
    x: r.clientX,
    y: r.clientY
  };
}
function me(r, o, a) {
  return (r - o) * 100 / (a - o);
}
function Pe(r, o, a) {
  return (a - o) * r + o;
}
function Be(r) {
  if (Math.abs(r) < 1) {
    const a = r.toExponential().split("e-"), i = a[0].split(".")[1];
    return (i ? i.length : 0) + parseInt(a[1], 10);
  }
  const o = r.toString().split(".")[1];
  return o ? o.length : 0;
}
function Ke(r, o, a) {
  const i = Math.round((r - a) / o) * o + a;
  return Number(i.toFixed(Be(o)));
}
function pe({
  values: r,
  newValue: o,
  index: a
}) {
  const i = r.slice();
  return i[a] = o, i.sort(ge);
}
function z({
  sliderRef: r,
  activeIndex: o,
  setActive: a
}) {
  var i, l;
  const x = U(r.current);
  if (!((i = r.current) != null && i.contains(x.activeElement)) || Number(x == null || (l = x.activeElement) == null ? void 0 : l.getAttribute("data-index")) !== o) {
    var T;
    (T = r.current) == null || T.querySelector(`[type="range"][data-index="${o}"]`).focus();
  }
  a && a(o);
}
function j(r, o) {
  return typeof r == "number" && typeof o == "number" ? r === o : typeof r == "object" && typeof o == "object" ? Re(r, o) : !1;
}
const ze = {
  horizontal: {
    offset: (r) => ({
      left: `${r}%`
    }),
    leap: (r) => ({
      width: `${r}%`
    })
  },
  "horizontal-reverse": {
    offset: (r) => ({
      right: `${r}%`
    }),
    leap: (r) => ({
      width: `${r}%`
    })
  },
  vertical: {
    offset: (r) => ({
      bottom: `${r}%`
    }),
    leap: (r) => ({
      height: `${r}%`
    })
  }
}, je = (r) => r;
let q;
function he() {
  return q === void 0 && (typeof CSS < "u" && typeof CSS.supports == "function" ? q = CSS.supports("touch-action", "none") : q = !0), q;
}
function st(r) {
  const {
    "aria-labelledby": o,
    defaultValue: a,
    disabled: i = !1,
    disableSwap: l = !1,
    isRtl: x = !1,
    marks: T = !1,
    max: p = 100,
    min: m = 0,
    name: Z,
    onChange: ee,
    onChangeCommitted: $,
    orientation: X = "horizontal",
    rootRef: be,
    scale: te = je,
    step: I = 1,
    shiftStep: ne = 10,
    tabIndex: ve,
    value: xe
  } = r, H = g.useRef(void 0), [F, D] = g.useState(-1), [ye, L] = g.useState(-1), [re, oe] = g.useState(!1), N = g.useRef(0), [S, O] = $e({
    controlled: xe,
    default: a ?? m,
    name: "Slider"
  }), y = ee && ((e, t, n) => {
    const u = e.nativeEvent || e, c = new u.constructor(u.type, u);
    Object.defineProperty(c, "target", {
      writable: !0,
      value: {
        value: t,
        name: Z
      }
    }), ee(c, t, n);
  }), A = Array.isArray(S);
  let f = A ? S.slice().sort(ge) : [S];
  f = f.map((e) => e == null ? m : R(e, m, p));
  const Y = T === !0 && I !== null ? [...Array(Math.floor((p - m) / I) + 1)].map((e, t) => ({
    value: m + I * t
  })) : T || [], b = Y.map((e) => e.value), {
    isFocusVisibleRef: se,
    onBlur: we,
    onFocus: Ee,
    ref: Te
  } = Fe(), [ue, _] = g.useState(-1), h = g.useRef(null), Ie = fe(Te, h), ie = fe(be, Ie), Se = (e) => (t) => {
    var n;
    const u = Number(t.currentTarget.getAttribute("data-index"));
    Ee(t), se.current === !0 && _(u), L(u), e == null || (n = e.onFocus) == null || n.call(e, t);
  }, He = (e) => (t) => {
    var n;
    we(t), se.current === !1 && _(-1), L(-1), e == null || (n = e.onBlur) == null || n.call(e, t);
  }, ae = (e, t) => {
    const n = Number(e.currentTarget.getAttribute("data-index")), u = f[n], c = b.indexOf(u);
    let s = t;
    if (Y && I == null) {
      const v = b[b.length - 1];
      s > v ? s = v : s < b[0] ? s = b[0] : s = s < u ? b[c - 1] : b[c + 1];
    }
    if (s = R(s, m, p), A) {
      l && (s = R(s, f[n - 1] || -1 / 0, f[n + 1] || 1 / 0));
      const v = s;
      s = pe({
        values: f,
        newValue: s,
        index: n
      });
      let w = n;
      l || (w = s.indexOf(v)), z({
        sliderRef: h,
        activeIndex: w
      });
    }
    O(s), _(n), y && !j(s, S) && y(e, s, n), $ && $(e, s);
  }, ke = (e) => (t) => {
    var n;
    if (I !== null) {
      const u = Number(t.currentTarget.getAttribute("data-index")), c = f[u];
      let s = null;
      (t.key === "ArrowLeft" || t.key === "ArrowDown") && t.shiftKey || t.key === "PageDown" ? s = Math.max(c - ne, m) : ((t.key === "ArrowRight" || t.key === "ArrowUp") && t.shiftKey || t.key === "PageUp") && (s = Math.min(c + ne, p)), s !== null && (ae(t, s), t.preventDefault());
    }
    e == null || (n = e.onKeyDown) == null || n.call(e, t);
  };
  Ne(() => {
    if (i && h.current.contains(document.activeElement)) {
      var e;
      (e = document.activeElement) == null || e.blur();
    }
  }, [i]), i && F !== -1 && D(-1), i && ue !== -1 && _(-1);
  const Me = (e) => (t) => {
    var n;
    (n = e.onChange) == null || n.call(e, t), ae(t, t.target.valueAsNumber);
  }, ce = g.useRef(void 0);
  let P = X;
  x && X === "horizontal" && (P += "-reverse");
  const B = ({
    finger: e,
    move: t = !1
  }) => {
    const {
      current: n
    } = h, {
      width: u,
      height: c,
      bottom: s,
      left: v
    } = n.getBoundingClientRect();
    let w;
    P.indexOf("vertical") === 0 ? w = (s - e.y) / c : w = (e.x - v) / u, P.indexOf("-reverse") !== -1 && (w = 1 - w);
    let d;
    if (d = Pe(w, m, p), I)
      d = Ke(d, I, m);
    else {
      const J = de(b, d);
      d = b[J];
    }
    d = R(d, m, p);
    let E = 0;
    if (A) {
      t ? E = ce.current : E = de(f, d), l && (d = R(d, f[E - 1] || -1 / 0, f[E + 1] || 1 / 0));
      const J = d;
      d = pe({
        values: f,
        newValue: d,
        index: E
      }), l && t || (E = d.indexOf(J), ce.current = E);
    }
    return {
      newValue: d,
      activeIndex: E
    };
  }, C = Q((e) => {
    const t = K(e, H);
    if (!t)
      return;
    if (N.current += 1, e.type === "mousemove" && e.buttons === 0) {
      k(e);
      return;
    }
    const {
      newValue: n,
      activeIndex: u
    } = B({
      finger: t,
      move: !0
    });
    z({
      sliderRef: h,
      activeIndex: u,
      setActive: D
    }), O(n), !re && N.current > _e && oe(!0), y && !j(n, S) && y(e, n, u);
  }), k = Q((e) => {
    const t = K(e, H);
    if (oe(!1), !t)
      return;
    const {
      newValue: n
    } = B({
      finger: t,
      move: !0
    });
    D(-1), e.type === "touchend" && L(-1), $ && $(e, n), H.current = void 0, V();
  }), G = Q((e) => {
    if (i)
      return;
    he() || e.preventDefault();
    const t = e.changedTouches[0];
    t != null && (H.current = t.identifier);
    const n = K(e, H);
    if (n !== !1) {
      const {
        newValue: c,
        activeIndex: s
      } = B({
        finger: n
      });
      z({
        sliderRef: h,
        activeIndex: s,
        setActive: D
      }), O(c), y && !j(c, S) && y(e, c, s);
    }
    N.current = 0;
    const u = U(h.current);
    u.addEventListener("touchmove", C, {
      passive: !0
    }), u.addEventListener("touchend", k, {
      passive: !0
    });
  }), V = g.useCallback(() => {
    const e = U(h.current);
    e.removeEventListener("mousemove", C), e.removeEventListener("mouseup", k), e.removeEventListener("touchmove", C), e.removeEventListener("touchend", k);
  }, [k, C]);
  g.useEffect(() => {
    const {
      current: e
    } = h;
    return e.addEventListener("touchstart", G, {
      passive: he()
    }), () => {
      e.removeEventListener("touchstart", G), V();
    };
  }, [V, G]), g.useEffect(() => {
    i && V();
  }, [i, V]);
  const De = (e) => (t) => {
    var n;
    if ((n = e.onMouseDown) == null || n.call(e, t), i || t.defaultPrevented || t.button !== 0)
      return;
    t.preventDefault();
    const u = K(t, H);
    if (u !== !1) {
      const {
        newValue: s,
        activeIndex: v
      } = B({
        finger: u
      });
      z({
        sliderRef: h,
        activeIndex: v,
        setActive: D
      }), O(s), y && !j(s, S) && y(t, s, v);
    }
    N.current = 0;
    const c = U(h.current);
    c.addEventListener("mousemove", C, {
      passive: !0
    }), c.addEventListener("mouseup", k);
  }, le = me(A ? f[0] : m, m, p), Le = me(f[f.length - 1], m, p) - le, Ae = (e = {}) => {
    const t = W(e), n = {
      onMouseDown: De(t || {})
    }, u = M({}, t, n);
    return M({}, e, {
      ref: ie
    }, u);
  }, Ce = (e) => (t) => {
    var n;
    (n = e.onMouseOver) == null || n.call(e, t);
    const u = Number(t.currentTarget.getAttribute("data-index"));
    L(u);
  }, Ve = (e) => (t) => {
    var n;
    (n = e.onMouseLeave) == null || n.call(e, t), L(-1);
  };
  return {
    active: F,
    axis: P,
    axisProps: ze,
    dragging: re,
    focusedThumbIndex: ue,
    getHiddenInputProps: (e = {}) => {
      var t;
      const n = W(e), u = {
        onChange: Me(n || {}),
        onFocus: Se(n || {}),
        onBlur: He(n || {}),
        onKeyDown: ke(n || {})
      }, c = M({}, n, u);
      return M({
        tabIndex: ve,
        "aria-labelledby": o,
        "aria-orientation": X,
        "aria-valuemax": te(p),
        "aria-valuemin": te(m),
        name: Z,
        type: "range",
        min: r.min,
        max: r.max,
        step: r.step === null && r.marks ? "any" : (t = r.step) != null ? t : void 0,
        disabled: i
      }, e, c, {
        style: M({}, Oe, {
          direction: x ? "rtl" : "ltr",
          // So that VoiceOver's focus indicator matches the thumb's dimensions
          width: "100%",
          height: "100%"
        })
      });
    },
    getRootProps: Ae,
    getThumbProps: (e = {}) => {
      const t = W(e), n = {
        onMouseOver: Ce(t || {}),
        onMouseLeave: Ve(t || {})
      };
      return M({}, e, t, n);
    },
    marks: Y,
    open: ye,
    range: A,
    rootRef: ie,
    trackLeap: Le,
    trackOffset: le,
    values: f,
    getThumbStyle: (e) => ({
      // So the non active thumb doesn't show its label on hover.
      pointerEvents: F !== -1 && F !== e ? "none" : void 0
    })
  };
}
export {
  je as Identity,
  st as useSlider,
  me as valueToPercent
};
