import * as r from "react";
import { useContext as I, useMemo as O } from "react";
import { FormItemInputContext as x } from "../form/context.mjs";
import "../radio/index.mjs";
import b from "../select/index.mjs";
import S from "../radio/group.mjs";
import E from "../radio/radioButton.mjs";
const j = 10, w = 20;
function R(i) {
  const {
    fullscreen: u,
    validRange: o,
    generateConfig: e,
    locale: a,
    prefixCls: n,
    value: s,
    onChange: g,
    divRef: h
  } = i, m = e.getYear(s || e.getNow());
  let l = m - j, d = l + w;
  o && (l = e.getYear(o[0]), d = e.getYear(o[1]) + 1);
  const C = a && a.year === "年" ? "年" : "", p = [];
  for (let t = l; t < d; t++)
    p.push({
      label: `${t}${C}`,
      value: t
    });
  return /* @__PURE__ */ r.createElement(b, {
    size: u ? void 0 : "small",
    options: p,
    value: m,
    className: `${n}-year-select`,
    onChange: (t) => {
      let c = e.setYear(s, t);
      if (o) {
        const [f, M] = o, v = e.getYear(c), Y = e.getMonth(c);
        v === e.getYear(M) && Y > e.getMonth(M) && (c = e.setMonth(c, e.getMonth(M))), v === e.getYear(f) && Y < e.getMonth(f) && (c = e.setMonth(c, e.getMonth(f)));
      }
      g(c);
    },
    getPopupContainer: () => h.current
  });
}
function y(i) {
  const {
    prefixCls: u,
    fullscreen: o,
    validRange: e,
    value: a,
    generateConfig: n,
    locale: s,
    onChange: g,
    divRef: h
  } = i, m = n.getMonth(a || n.getNow());
  let l = 0, d = 11;
  if (e) {
    const [t, c] = e, f = n.getYear(a);
    n.getYear(c) === f && (d = n.getMonth(c)), n.getYear(t) === f && (l = n.getMonth(t));
  }
  const C = s.shortMonths || n.locale.getShortMonths(s.locale), p = [];
  for (let t = l; t <= d; t += 1)
    p.push({
      label: C[t],
      value: t
    });
  return /* @__PURE__ */ r.createElement(b, {
    size: o ? void 0 : "small",
    className: `${u}-month-select`,
    value: m,
    options: p,
    onChange: (t) => {
      g(n.setMonth(a, t));
    },
    getPopupContainer: () => h.current
  });
}
function N(i) {
  const {
    prefixCls: u,
    locale: o,
    mode: e,
    fullscreen: a,
    onModeChange: n
  } = i;
  return /* @__PURE__ */ r.createElement(S, {
    onChange: (s) => {
      let {
        target: {
          value: g
        }
      } = s;
      n(g);
    },
    value: e,
    size: a ? void 0 : "small",
    className: `${u}-mode-switch`
  }, /* @__PURE__ */ r.createElement(E, {
    value: "month"
  }, o.month), /* @__PURE__ */ r.createElement(E, {
    value: "year"
  }, o.year));
}
function G(i) {
  const {
    prefixCls: u,
    fullscreen: o,
    mode: e,
    onChange: a,
    onModeChange: n
  } = i, s = r.useRef(null), g = I(x), h = O(() => Object.assign(Object.assign({}, g), {
    isFormItemInput: !1
  }), [g]), m = Object.assign(Object.assign({}, i), {
    fullscreen: o,
    divRef: s
  });
  return /* @__PURE__ */ r.createElement("div", {
    className: `${u}-header`,
    ref: s
  }, /* @__PURE__ */ r.createElement(x.Provider, {
    value: h
  }, /* @__PURE__ */ r.createElement(R, Object.assign({}, m, {
    onChange: (l) => {
      a(l, "year");
    }
  })), e === "month" && /* @__PURE__ */ r.createElement(y, Object.assign({}, m, {
    onChange: (l) => {
      a(l, "month");
    }
  }))), /* @__PURE__ */ r.createElement(N, Object.assign({}, m, {
    onModeChange: n
  })));
}
export {
  G as default
};
