import kn from "../../@babel/runtime/helpers/esm/classCallCheck.mjs";
import Dn from "../../@babel/runtime/helpers/esm/createClass.mjs";
import Ee from "../../@babel/runtime/helpers/esm/assertThisInitialized.mjs";
import _n from "../../@babel/runtime/helpers/esm/inherits.mjs";
import Pn from "../../@babel/runtime/helpers/esm/createSuper.mjs";
import X from "../../@babel/runtime/helpers/esm/extends.mjs";
import w from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import wn from "../../@babel/runtime/helpers/esm/typeof.mjs";
import E from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import Sn from "../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import v from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import G from "../../classnames/index.mjs";
import Me from "../../rc-util/es/hooks/useMergedState.mjs";
import En from "../../rc-util/es/pickAttrs.mjs";
import Mn from "../../rc-util/es/raf.mjs";
import { warningOnce as J } from "../../rc-util/es/warning.mjs";
import * as i from "react";
import { useRef as b, useEffect as de, useState as ye } from "react";
import { useCellRender as yn } from "./hooks/useCellRender.mjs";
import Fr from "./hooks/useHoverValue.mjs";
import Br from "./hooks/usePickerInput.mjs";
import Tn from "./hooks/usePresets.mjs";
import bn from "./hooks/useRangeDisabled.mjs";
import xn from "./hooks/useRangeOpen.mjs";
import Nn from "./hooks/useRangeViewDates.mjs";
import Wr from "./hooks/useTextValueMapping.mjs";
import Kr from "./hooks/useValueTexts.mjs";
import In from "./PanelContext.mjs";
import On from "./PickerPanel.mjs";
import Hn from "./PickerTrigger.mjs";
import Ln from "./PresetPanel.mjs";
import An from "./RangeContext.mjs";
import { formatValue as Te, parseValue as Fn, getClosingViewDate as be, isSameWeek as Bn, isSameQuarter as Wn, isSameDate as Kn, isEqual as Yr } from "./utils/dateUtil.mjs";
import { getClearIcon as Yn } from "./utils/getClearIcon.mjs";
import $n from "./utils/getExtraFooter.mjs";
import Un from "./utils/getRanges.mjs";
import { getValue as c, updateValues as x, toArray as qn } from "./utils/miscUtil.mjs";
import { getInputSize as zn, elementsContains as jn, getDefaultFormat as Qn } from "./utils/uiUtil.mjs";
import { legacyPropsWarning as Gn } from "./utils/warnUtil.mjs";
function $r(g, V) {
  return g && g[0] && g[1] && V.isAfter(g[0], g[1]) ? [g[1], g[0]] : g;
}
function Ur(g, V, D, p) {
  return !!(g || p && p[V] || D[(V + 1) % 2]);
}
function Jn(g) {
  var V, D, p, r = g, q = r.prefixCls, u = q === void 0 ? "rc-picker" : q, qr = r.id, zr = r.style, jr = r.className, Qr = r.popupStyle, Gr = r.dropdownClassName, Jr = r.transitionName, Xr = r.dropdownAlign, Zr = r.getPopupContainer, f = r.generateConfig, _ = r.locale, xe = r.placeholder, et = r.autoFocus, N = r.disabled, rt = r.format, Ne = r.picker, d = Ne === void 0 ? "date" : Ne, M = r.showTime, tt = r.use12Hours, Ie = r.separator, nt = Ie === void 0 ? "~" : Ie, Z = r.value, at = r.defaultValue, ot = r.defaultPickerValue, ut = r.open, it = r.defaultOpen, z = r.disabledDate, Oe = r.disabledTime, He = r.dateRender, Le = r.monthCellRender, lt = r.cellRender, Ae = r.panelRender, st = r.presets, ct = r.ranges, pe = r.allowEmpty, Fe = r.allowClear, Be = r.suffixIcon, We = r.clearIcon, Ke = r.pickerRef, Ye = r.inputReadOnly, ft = r.mode, dt = r.renderExtraFooter, $e = r.onChange, pt = r.onOpenChange, Ue = r.onPanelChange, qe = r.onCalendarChange, ze = r.onFocus, me = r.onBlur, je = r.onMouseDown, mt = r.onMouseUp, vt = r.onMouseEnter, gt = r.onMouseLeave, Qe = r.onClick, ve = r.onOk, ge = r.onKeyDown, Ct = r.components, Ge = r.order, L = r.direction, ht = r.activePickerIndex, Je = r.autoComplete, Xe = Je === void 0 ? "off" : Je, ee = r.changeOnBlur, W = d === "date" && !!M || d === "time", re = b(null), te = b(null), K = b(null), ne = b(null), ae = b(null), y = b(null), A = b(null), Y = b(null);
  process.env.NODE_ENV !== "production" && Gn(g);
  var I = qn(Qn(rt, d, M, tt)), Ze = function(e, n) {
    return e && e[n] ? Te(e[n], {
      generateConfig: f,
      locale: _,
      format: I[0]
    }) : "";
  }, oe = b(null), m = i.useMemo(function() {
    return Array.isArray(N) ? N : [N || !1, N || !1];
  }, [N]), Rt = Me(null, {
    value: Z,
    defaultValue: at,
    postState: function(e) {
      return d === "time" && !Ge ? e : $r(e, f);
    }
  }), er = v(Rt, 2), C = er[0], Vt = er[1], kt = Nn({
    values: C,
    picker: d,
    defaultDates: ot,
    generateConfig: f
  }), rr = v(kt, 2), Dt = rr[0], ue = rr[1], _t = Me(C, {
    postState: function(e) {
      var n = e;
      if (m[0] && m[1])
        return n;
      for (var a = 0; a < 2; a += 1)
        m[a] && !n && !c(n, a) && !c(pe, a) && (n = x(n, f.getNow(), a));
      return n;
    }
  }), tr = v(_t, 2), l = tr[0], $ = tr[1], Pt = Me([d, d], {
    value: ft
  }), nr = v(Pt, 2), j = nr[0], ar = nr[1];
  de(function() {
    ar([d, d]);
  }, [d]);
  var wt = function(e, n) {
    ar(e), Ue && Ue(n, e);
  }, St = xn(it, ut, ht, ee, W, y, A, c(l, 0), c(l, 1), m, pt), ie = v(St, 4), h = ie[0], t = ie[1], Et = ie[2], S = ie[3], Mt = h && t === 0, yt = h && t === 1, Tt = bn({
    picker: d,
    selectedValue: l,
    locale: _,
    disabled: m,
    disabledDate: z,
    generateConfig: f
  }, !h || Et), or = v(Tt, 2), ur = or[0], ir = or[1], bt = ye(0), lr = v(bt, 2), xt = lr[0], Nt = lr[1];
  de(function() {
    !h && re.current && Nt(re.current.offsetWidth);
  }, [h]);
  function sr(o) {
    S(!0, o, "open"), Mn(function() {
      var e, n = [y, A][o];
      (e = n.current) === null || e === void 0 || e.focus();
    }, 0);
  }
  function F(o, e, n) {
    var a = o, s = c(a, 0), R = c(a, 1);
    s && R && f.isAfter(s, R) && (// WeekPicker only compare week
    d === "week" && !Bn(f, _.locale, s, R) || // QuotaPicker only compare week
    d === "quarter" && !Wn(f, s, R) || // Other non-TimePicker compare date
    d !== "week" && d !== "quarter" && d !== "time" && !Kn(f, s, R) ? e === 0 ? (a = [s, null], R = null) : (s = null, a = [null, R]) : (d !== "time" || Ge !== !1) && (a = $r(a, f))), $(a);
    var P = Ze(a, 0), T = Ze(a, 1);
    if (qe) {
      var H = {
        range: e === 0 ? "start" : "end"
      };
      qe(a, [P, T], H);
    }
    if (!n) {
      var Q = Ur(s, 0, m, pe), B = Ur(R, 1, m, pe), k = a === null || Q && B;
      k && (Vt(a), $e && (!Yr(f, c(C, 0), s) || !Yr(f, c(C, 1), R)) && $e(a, [P, T]));
    }
  }
  var It = function(e) {
    return h && oe.current && oe.current.onKeyDown ? oe.current.onKeyDown(e) : (J(!1, "Picker not correct forward KeyDown operation. Please help to fire issue about this."), !1);
  }, cr = {
    formatList: I,
    generateConfig: f,
    locale: _
  }, Ot = Kr(c(l, 0), cr), fr = v(Ot, 2), le = fr[0], Ht = fr[1], Lt = Kr(c(l, 1), cr), dr = v(Lt, 2), se = dr[0], At = dr[1], pr = function(e, n) {
    var a = Fn(e, {
      locale: _,
      formatList: I,
      generateConfig: f
    }), s = n === 0 ? ur : ir;
    a && !s(a) && ($(x(l, a, n)), ue(a, n));
  }, Ft = Wr({
    valueTexts: le,
    onTextChange: function(e) {
      return pr(e, 0);
    }
  }), Ce = v(Ft, 3), ce = Ce[0], mr = Ce[1], vr = Ce[2], Bt = Wr({
    valueTexts: se,
    onTextChange: function(e) {
      return pr(e, 1);
    }
  }), he = v(Bt, 3), fe = he[0], gr = he[1], Cr = he[2], Wt = ye(null), hr = v(Wt, 2), Kt = hr[0], Yt = hr[1], $t = ye(null), Rr = v($t, 2), U = Rr[0], Vr = Rr[1], Ut = Fr(ce, {
    formatList: I,
    generateConfig: f,
    locale: _
  }), Re = v(Ut, 3), kr = Re[0], qt = Re[1], Ve = Re[2], zt = Fr(fe, {
    formatList: I,
    generateConfig: f,
    locale: _
  }), ke = v(zt, 3), Dr = ke[0], jt = ke[1], De = ke[2], Qt = function(e) {
    Vr(x(l, e, t)), t === 0 ? qt(e) : jt(e);
  }, Gt = function() {
    Vr(x(l, null, t)), t === 0 ? Ve() : De();
  }, Jt = i.useState(h), _r = v(Jt, 2), Xt = _r[0], Zt = _r[1];
  i.useEffect(function() {
    Zt(h);
  }, [h]);
  var en = function(e) {
    if (Xt) {
      if (W) {
        var n = t ? 0 : 1, a = c(l, n);
        a && F(l, n, !0);
      } else if (ee) {
        var s = c(l, t);
        s && F(l, t);
      }
    }
    return me == null ? void 0 : me(e);
  }, Pr = function(e, n) {
    return {
      blurToCancel: !ee && W,
      changeOnBlur: ee,
      forwardKeyDown: It,
      onBlur: en,
      isClickOutside: function(s) {
        var R = [K.current, ne.current, re.current];
        return !jn([].concat(Sn(W ? [R[t]] : R), [te.current]), s);
      },
      onFocus: function(s) {
        ze && ze(s);
      },
      triggerOpen: function(s) {
        s ? S(s, e, "open") : S(
          s,
          // Close directly if no selected value provided
          c(l, e) ? e : !1,
          "blur"
        );
      },
      onSubmit: function() {
        if (
          // When user typing disabledDate with keyboard and enter, this value will be empty
          !l || // Normal disabled check
          z && z(l[e])
        )
          return !1;
        F(l, e), n(), S(!1, t, "confirm");
      },
      onCancel: function() {
        S(!1, e, "cancel"), $(C), n();
      }
    };
  }, wr = {
    onKeyDown: function(e, n) {
      ge == null || ge(e, n);
    }
  }, rn = Br(E(E({}, Pr(0, vr)), {}, {
    open: Mt,
    value: ce
  }, wr)), Sr = v(rn, 2), tn = Sr[0], Er = Sr[1], Mr = Er.focused, yr = Er.typing, nn = Br(E(E({}, Pr(1, Cr)), {}, {
    open: yt,
    value: fe
  }, wr)), Tr = v(nn, 2), an = Tr[0], br = Tr[1], xr = br.focused, Nr = br.typing, on = function(e) {
    Qe && Qe(e), !h && !y.current.contains(e.target) && !A.current.contains(e.target) && (m[0] ? m[1] || sr(1) : sr(0));
  }, un = function(e) {
    je && je(e), h && (Mr || xr) && !y.current.contains(e.target) && !A.current.contains(e.target) && e.preventDefault();
  }, ln = C && C[0] ? Te(C[0], {
    locale: _,
    format: "YYYYMMDDHHmmss",
    generateConfig: f
  }) : "", sn = C && C[1] ? Te(C[1], {
    locale: _,
    format: "YYYYMMDDHHmmss",
    generateConfig: f
  }) : "";
  de(function() {
    h || ($(C), !le.length || le[0] === "" ? mr("") : Ht !== ce && vr(), !se.length || se[0] === "" ? gr("") : At !== fe && Cr());
  }, [h, le, se]), de(function() {
    $(C);
  }, [ln, sn]);
  var _e = yn({
    cellRender: lt,
    monthCellRender: Le,
    dateRender: He
  }), cn = i.useMemo(function() {
    if (_e)
      return function(o, e) {
        return _e(o, E(E({}, e), {}, {
          range: t ? "end" : "start"
        }));
      };
  }, [t, _e]);
  process.env.NODE_ENV !== "production" && (Z && Array.isArray(N) && (c(N, 0) && !c(Z, 0) || c(N, 1) && !c(Z, 1)) && J(!1, "`disabled` should not set with empty `value`. You should set `allowEmpty` or `value` instead."), J(!He, "'dateRender' is deprecated. Please use 'cellRender' instead."), J(!Le, "'monthCellRender' is deprecated. Please use 'cellRender' instead."), J(!We, "`clearIcon` will be removed in future. Please use `allowClear` instead.")), Ke && (Ke.current = {
    focus: function() {
      y.current && y.current.focus();
    },
    blur: function() {
      y.current && y.current.blur(), A.current && A.current.blur();
    }
  });
  var fn = Tn(st, ct);
  function Pe() {
    var o = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : !1, e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, n = null;
    h && U && U[0] && U[1] && f.isAfter(U[1], U[0]) && (n = U);
    var a = M;
    if (M && wn(M) === "object" && M.defaultValue) {
      var s = M.defaultValue;
      a = E(E({}, M), {}, {
        defaultValue: c(s, t) || void 0
      });
    }
    return /* @__PURE__ */ i.createElement(An.Provider, {
      value: {
        inRange: !0,
        panelPosition: o,
        rangedValue: Kt || l,
        hoverRangedValue: n
      }
    }, /* @__PURE__ */ i.createElement(On, X({}, g, e, {
      cellRender: cn,
      showTime: a,
      mode: j[t],
      generateConfig: f,
      style: void 0,
      direction: L,
      disabledDate: t === 0 ? ur : ir,
      disabledTime: function(P) {
        return Oe ? Oe(P, t === 0 ? "start" : "end") : !1;
      },
      className: G(w({}, "".concat(u, "-panel-focused"), t === 0 ? !yr : !Nr)),
      value: c(l, t),
      locale: _,
      tabIndex: -1,
      onPanelChange: function(P, T) {
        t === 0 && Ve(!0), t === 1 && De(!0), wt(x(j, T, t), x(l, P, t));
        var H = P;
        o === "right" && j[t] === T && (H = be(H, T, f, -1)), ue(H, t);
      },
      onOk: null,
      onSelect: void 0,
      onChange: void 0,
      defaultValue: t === 0 ? c(l, 1) : c(l, 0)
      // defaultPickerValue={undefined}
    })));
  }
  var O = 0, Ir = 0;
  if (t && K.current && ae.current && te.current && Y.current) {
    O = K.current.offsetWidth + ae.current.offsetWidth;
    var dn = Y.current.offsetLeft > O ? Y.current.offsetLeft - O : Y.current.offsetLeft, Or = te.current.offsetWidth, Hr = Y.current.offsetWidth;
    Or && Hr && O > Or - Hr - (L === "rtl" ? 0 : dn) && (Ir = O);
  }
  var pn = L === "rtl" ? {
    right: O
  } : {
    left: O
  };
  function mn() {
    var o, e = $n(u, j[t], dt), n = Un({
      prefixCls: u,
      components: Ct,
      needConfirmButton: W,
      okDisabled: !c(l, t) || z && z(l[t]),
      locale: _,
      // rangeList,
      onOk: function() {
        var k = c(l, t);
        k && (F(l, t), ve == null || ve(l), S(!1, t, "confirm"));
      }
    });
    if (d !== "time" && !M) {
      var a = Dt(t), s = be(a, d, f), R = j[t], P = R === d, T = Pe(P ? "left" : !1, {
        pickerValue: a,
        onPickerValueChange: function(k) {
          ue(k, t);
        }
      }), H = Pe("right", {
        pickerValue: s,
        onPickerValueChange: function(k) {
          ue(be(k, d, f, -1), t);
        }
      });
      L === "rtl" ? o = /* @__PURE__ */ i.createElement(i.Fragment, null, H, P && T) : o = /* @__PURE__ */ i.createElement(i.Fragment, null, T, P && H);
    } else
      o = Pe();
    var Q = /* @__PURE__ */ i.createElement("div", {
      className: "".concat(u, "-panel-layout")
    }, /* @__PURE__ */ i.createElement(Ln, {
      prefixCls: u,
      presets: fn,
      onClick: function(k) {
        F(k, null), S(!1, t, "preset");
      },
      onHover: function(k) {
        Yt(k);
      }
    }), /* @__PURE__ */ i.createElement("div", null, /* @__PURE__ */ i.createElement("div", {
      className: "".concat(u, "-panels")
    }, o), (e || n) && /* @__PURE__ */ i.createElement("div", {
      className: "".concat(u, "-footer")
    }, e, n)));
    return Ae && (Q = Ae(Q)), /* @__PURE__ */ i.createElement("div", {
      className: "".concat(u, "-panel-container"),
      style: {
        marginLeft: Ir
      },
      ref: te,
      onMouseDown: function(k) {
        k.preventDefault();
      }
    }, Q);
  }
  var vn = /* @__PURE__ */ i.createElement("div", {
    className: G("".concat(u, "-range-wrapper"), "".concat(u, "-").concat(d, "-range-wrapper")),
    style: {
      minWidth: xt
    }
  }, /* @__PURE__ */ i.createElement("div", {
    ref: Y,
    className: "".concat(u, "-range-arrow"),
    style: pn
  }), mn()), Lr;
  Be && (Lr = /* @__PURE__ */ i.createElement("span", {
    className: "".concat(u, "-suffix"),
    onMouseDown: function(e) {
      e.preventDefault();
    }
  }, Be));
  var gn = Yn(u, Fe, We), Cn = /* @__PURE__ */ i.createElement("span", {
    onMouseDown: function(e) {
      e.preventDefault(), e.stopPropagation();
    },
    onMouseUp: function(e) {
      e.preventDefault(), e.stopPropagation();
      var n = C;
      m[0] || (n = x(n, null, 0)), m[1] || (n = x(n, null, 1)), F(n, null), S(!1, t, "clear");
    },
    className: "".concat(u, "-clear"),
    role: "button"
  }, gn), hn = Fe && (c(C, 0) && !m[0] || c(C, 1) && !m[1]), Ar = {
    size: zn(d, I[0], f)
  }, we = 0, Se = 0;
  K.current && ne.current && ae.current && (t === 0 ? Se = K.current.offsetWidth : (we = O, Se = ne.current.offsetWidth));
  var Rn = L === "rtl" ? {
    right: we
  } : {
    left: we
  }, Vn = function(e, n) {
    var a = x(l, e, t);
    if (n === "submit" || n !== "key" && !W) {
      F(a, t), t === 0 ? Ve() : De();
      var s = t === 0 ? 1 : 0;
      m[s] ? S(!1, !1, "confirm") : S(!1, t, "confirm");
    } else
      $(a);
  };
  return /* @__PURE__ */ i.createElement(In.Provider, {
    value: {
      operationRef: oe,
      hideHeader: d === "time",
      onDateMouseEnter: Qt,
      onDateMouseLeave: Gt,
      hideRanges: !0,
      onSelect: Vn,
      open: h
    }
  }, /* @__PURE__ */ i.createElement(Hn, {
    visible: h,
    popupElement: vn,
    popupStyle: Qr,
    prefixCls: u,
    dropdownClassName: Gr,
    dropdownAlign: Xr,
    getPopupContainer: Zr,
    transitionName: Jr,
    range: !0,
    direction: L
  }, /* @__PURE__ */ i.createElement("div", X({
    ref: re,
    className: G(u, "".concat(u, "-range"), jr, (V = {}, w(V, "".concat(u, "-disabled"), m[0] && m[1]), w(V, "".concat(u, "-focused"), t === 0 ? Mr : xr), w(V, "".concat(u, "-rtl"), L === "rtl"), V)),
    style: zr,
    onClick: on,
    onMouseEnter: vt,
    onMouseLeave: gt,
    onMouseDown: un,
    onMouseUp: mt
  }, En(g, {
    aria: !0,
    data: !0
  })), /* @__PURE__ */ i.createElement("div", {
    className: G("".concat(u, "-input"), (D = {}, w(D, "".concat(u, "-input-active"), t === 0), w(D, "".concat(u, "-input-placeholder"), !!kr), D)),
    ref: K
  }, /* @__PURE__ */ i.createElement("input", X({
    id: qr,
    disabled: m[0],
    readOnly: Ye || typeof I[0] == "function" || !yr,
    value: kr || ce,
    onChange: function(e) {
      mr(e.target.value);
    },
    autoFocus: et,
    placeholder: c(xe, 0) || "",
    ref: y
  }, tn, Ar, {
    autoComplete: Xe
  }))), /* @__PURE__ */ i.createElement("div", {
    className: "".concat(u, "-range-separator"),
    ref: ae
  }, nt), /* @__PURE__ */ i.createElement("div", {
    className: G("".concat(u, "-input"), (p = {}, w(p, "".concat(u, "-input-active"), t === 1), w(p, "".concat(u, "-input-placeholder"), !!Dr), p)),
    ref: ne
  }, /* @__PURE__ */ i.createElement("input", X({
    disabled: m[1],
    readOnly: Ye || typeof I[0] == "function" || !Nr,
    value: Dr || fe,
    onChange: function(e) {
      gr(e.target.value);
    },
    placeholder: c(xe, 1) || "",
    ref: A
  }, an, Ar, {
    autoComplete: Xe
  }))), /* @__PURE__ */ i.createElement("div", {
    className: "".concat(u, "-active-bar"),
    style: E(E({}, Rn), {}, {
      width: Se,
      position: "absolute"
    })
  }), Lr, hn && Cn)));
}
var Ha = /* @__PURE__ */ function(g) {
  _n(D, g);
  var V = Pn(D);
  function D() {
    var p;
    kn(this, D);
    for (var r = arguments.length, q = new Array(r), u = 0; u < r; u++)
      q[u] = arguments[u];
    return p = V.call.apply(V, [this].concat(q)), w(Ee(p), "pickerRef", /* @__PURE__ */ i.createRef()), w(Ee(p), "focus", function() {
      p.pickerRef.current && p.pickerRef.current.focus();
    }), w(Ee(p), "blur", function() {
      p.pickerRef.current && p.pickerRef.current.blur();
    }), p;
  }
  return Dn(D, [{
    key: "render",
    value: function() {
      return /* @__PURE__ */ i.createElement(Jn, X({}, this.props, {
        pickerRef: this.pickerRef
      }));
    }
  }]), D;
}(i.Component);
export {
  Ha as default
};
