import ee from "./index-extends.js";
import * as s from "react";
import o from "./index-_virtual16.js";
import { clsx as te } from "./index-clsx.m.js";
import { PickersYear as re } from "./index-PickersYear.js";
import { useNow as oe, useUtils as E, useDefaultDates as se } from "./index-useUtils.js";
import { WrapperVariantContext as ae } from "./index-WrapperVariantContext.js";
import { getYearPickerUtilityClass as ne } from "./index-yearPickerClasses.js";
import { parseNonNullablePickerDate as O } from "./index-date-utils.js";
import { jsx as S } from "react/jsx-runtime";
import ce from "./index-useTheme.js";
import le from "./index-useControlled2.js";
import ue from "./index-useForkRef.js";
import ie from "./index-useThemeProps.js";
import fe from "./index-composeClasses.js";
import me from "./index-styled.js";
const de = (d) => {
  const {
    classes: n
  } = d;
  return fe({
    root: ["root"]
  }, ne, n);
};
function pe(d, n) {
  const p = E(), a = se(), c = ie({
    props: d,
    name: n
  });
  return ee({
    disablePast: !1,
    disableFuture: !1
  }, c, {
    minDate: O(p, c.minDate, a.minDate),
    maxDate: O(p, c.maxDate, a.maxDate)
  });
}
const be = me("div", {
  name: "MuiYearPicker",
  slot: "Root",
  overridesResolver: (d, n) => n.root
})({
  display: "flex",
  flexDirection: "row",
  flexWrap: "wrap",
  overflowY: "auto",
  height: "100%",
  padding: "0 4px",
  maxHeight: "304px"
}), he = /* @__PURE__ */ s.forwardRef(function(n, p) {
  const a = oe(), c = ce(), r = E(), y = pe(n, "MuiYearPicker"), {
    autoFocus: k,
    className: M,
    date: i,
    disabled: U,
    disableFuture: v,
    disablePast: H,
    maxDate: b,
    minDate: h,
    onChange: I,
    readOnly: j,
    shouldDisableYear: w,
    disableHighlightToday: N,
    onYearFocus: x,
    hasFocus: q,
    onFocusedViewChange: F
  } = y, T = y, K = de(T), C = s.useMemo(() => i ?? r.startOfYear(a), [a, r, i]), f = s.useMemo(() => i != null ? r.getYear(i) : N ? null : r.getYear(a), [a, i, r, N]), W = s.useContext(ae), _ = s.useRef(null), [Y, A] = s.useState(() => f || r.getYear(a)), [z, B] = le({
    name: "YearPicker",
    state: "hasFocus",
    controlled: q,
    default: k
  }), D = s.useCallback((e) => {
    B(e), F && F(e);
  }, [B, F]), P = s.useCallback((e) => !!(H && r.isBeforeYear(e, a) || v && r.isAfterYear(e, a) || h && r.isBeforeYear(e, h) || b && r.isAfterYear(e, b) || w && w(e)), [v, H, b, h, a, w, r]), L = (e, t, u = "finish") => {
    if (j)
      return;
    const g = r.setYear(C, t);
    I(g, u);
  }, l = s.useCallback((e) => {
    P(r.setYear(C, e)) || (A(e), D(!0), x == null || x(e));
  }, [P, r, C, D, x]);
  s.useEffect(() => {
    A((e) => f !== null && e !== f ? f : e);
  }, [f]);
  const R = W === "desktop" ? 4 : 3, V = s.useCallback((e, t) => {
    switch (e.key) {
      case "ArrowUp":
        l(t - R), e.preventDefault();
        break;
      case "ArrowDown":
        l(t + R), e.preventDefault();
        break;
      case "ArrowLeft":
        l(t + (c.direction === "ltr" ? -1 : 1)), e.preventDefault();
        break;
      case "ArrowRight":
        l(t + (c.direction === "ltr" ? 1 : -1)), e.preventDefault();
        break;
    }
  }, [l, c.direction, R]), G = s.useCallback((e, t) => {
    l(t);
  }, [l]), J = s.useCallback((e, t) => {
    Y === t && D(!1);
  }, [Y, D]), Q = r.getYear(a), m = s.useRef(null), X = ue(p, m);
  return s.useEffect(() => {
    if (k || m.current === null)
      return;
    const e = m.current.querySelector('[tabindex="0"]');
    if (!e)
      return;
    const t = e.offsetHeight, u = e.offsetTop, g = m.current.clientHeight, Z = m.current.scrollTop, $ = u + t;
    t > g || u < Z || (m.current.scrollTop = $ - g / 2 - t / 2);
  }, [k]), /* @__PURE__ */ S(be, {
    ref: X,
    className: te(K.root, M),
    ownerState: T,
    children: r.getYearRange(h, b).map((e) => {
      const t = r.getYear(e), u = t === f;
      return /* @__PURE__ */ S(re, {
        selected: u,
        value: t,
        onClick: L,
        onKeyDown: V,
        autoFocus: z && t === Y,
        ref: u ? _ : void 0,
        disabled: U || P(e),
        tabIndex: t === Y ? 0 : -1,
        onFocus: G,
        onBlur: J,
        "aria-current": Q === t ? "date" : void 0,
        children: r.format(e, "year")
      }, r.format(e, "year"));
    })
  });
});
process.env.NODE_ENV !== "production" && (he.propTypes = {
  // ----------------------------- Warning --------------------------------
  // | These PropTypes are generated from the TypeScript type definitions |
  // | To update them edit the TypeScript types and run "yarn proptypes"  |
  // ----------------------------------------------------------------------
  autoFocus: o.bool,
  classes: o.object,
  className: o.string,
  date: o.any,
  disabled: o.bool,
  /**
   * If `true` future days are disabled.
   * @default false
   */
  disableFuture: o.bool,
  /**
   * If `true`, today's date is rendering without highlighting with circle.
   * @default false
   */
  disableHighlightToday: o.bool,
  /**
   * If `true` past days are disabled.
   * @default false
   */
  disablePast: o.bool,
  hasFocus: o.bool,
  /**
   * Maximal selectable date. @DateIOType
   */
  maxDate: o.any,
  /**
   * Minimal selectable date. @DateIOType
   */
  minDate: o.any,
  onChange: o.func.isRequired,
  onFocusedDayChange: o.func,
  onFocusedViewChange: o.func,
  onYearFocus: o.func,
  readOnly: o.bool,
  /**
   * Disable specific years dynamically.
   * Works like `shouldDisableDate` but for year selection view @DateIOType.
   * @template TDate
   * @param {TDate} year The year to test.
   * @returns {boolean} Returns `true` if the year should be disabled.
   */
  shouldDisableYear: o.func
});
export {
  he as YearPicker
};
