import { jsxs as z, jsx as d } from "react/jsx-runtime";
/* empty css                           */
import { useInsideClickCallback as Pe } from "./index-use-inside-click-callback.js";
import { useOutsideCallback as ve } from "./index-use-outside-callback.js";
import { dFlex as Se } from "./index-general.module.js";
import { classNames as P } from "./index-classNames.js";
import { dateIsSameDay as se, dateIsBefore as xe } from "./index-date-helpers.js";
import { translationService as G } from "./index-translation.service.js";
import { ComponentContainer as Ae } from "./index-ComponentContainer.js";
import { ensureIsRealDate as Fe, formatDateForComparison as V } from "./index-date-range-picker-utils.js";
import { useDateRangePickerStaticRanges as be } from "./index-DateRangePicker.config.js";
import { dateRangePickerFormatter as E } from "./index-DateRangePicker.formatter.js";
import { DateRangePickerBase as oe } from "./index-DateRangePickerBase.js";
import { DateRangePickerBaseInputAdornment as ce } from "./index-DateRangePickerBaseInputAdornment.js";
import { DateRangePickerIMaskInput as Ne } from "./index-DateRangePickerIMaskInput.js";
import { Input as le } from "./index-Input.js";
import H from "./index-moment.js";
import { useReducer as ye, useRef as v, useState as o, useMemo as Te, useEffect as h } from "react";
import { a as de } from "./index-react-spring_web.modern.js";
import we from "./index-Popper.js";
import $e from "./index-Fade.js";
import { useSpring as fe } from "./index-react-spring_core.modern.js";
const nt = (J) => {
  const {
    maxDate: K,
    minDate: T,
    onChange: m,
    onStateChange: Q,
    onDateRangeSelected: S,
    ranges: ue,
    startDate: f,
    endDate: u,
    testId: R,
    className: pe,
    disabled: i
  } = J, [, me] = ye((e) => !e, !1), C = v({}), [Me, Oe] = o(""), L = v(ue ?? be()), [s, W] = o(J.state ?? "date-range-picker"), [X, ge] = o(null), [Z, x] = o(!1), [_, A] = o(!1), [ee, c] = o(!1), [F, w] = o({
    startDate: f,
    endDate: u
  }), [p, B] = o({
    startDate: f,
    endDate: u
  }), [De, $] = o(""), [ke, Y] = o(""), [U, g] = o(!1), M = v(null), b = v(null), N = v(null), j = v(null);
  ve(j, () => {
    c(!1);
  }), Pe(j, () => {
    c(!0);
  });
  const q = Te(() => Fe(T), [T]), [I, he] = o(L.current ?? []);
  h(() => {
    if (f && u && H(f).isValid() && H(u).isValid()) {
      const {
        range: e,
        label: a,
        replacements: t,
        startDate: r,
        endDate: n
      } = I.find((l) => se(l.startDate, f) && se(l.endDate, u)) ?? {};
      if (!p.endDate && !p.startDate && (B({
        startDate: f,
        endDate: u
      }), m && m({
        startDate: f,
        endDate: u
      }, "AUTOMATIC")), !e) {
        !p.endDate && !p.startDate && (W("static-range-picker"), $(E.formatDateRangePickerInput(f, u)));
        return;
      }
      Y(G.get(a ?? "", t).value);
    }
  }, [f, u]), h(() => {
    var e;
    if (q) {
      const a = (e = L.current) == null ? void 0 : e.filter((t) => {
        const r = t.range().startDate, n = t.range().endDate;
        return !q || !r || !n ? !0 : xe(q, r);
      });
      he(a);
    }
  }, [L.current]), h(() => {
    (Z || _ || ee) && !i ? g(!0) : (c(!1), g(!1));
  }, [Z, _, ee, i]), h(() => {
    U && setTimeout(() => {
      me();
    }, 0);
  }, [U]), h(() => {
    let {
      range: e,
      label: a,
      replacements: t
    } = I.find((D) => D.isDefault) ?? {}, r = !1;
    (!e || !a) && ({
      range: e,
      label: a,
      replacements: t
    } = I[0]);
    const {
      startDate: n,
      endDate: l
    } = e();
    if (Y(G.get(a ?? "", t).value), !p.endDate && !p.startDate && (B({
      startDate: n,
      endDate: l
    }), r = !0, m && m({
      startDate: n,
      endDate: l
    }, "AUTOMATIC")), !F.endDate && !F.startDate && !r) {
      const D = /* @__PURE__ */ new Date(), k = D, y = D;
      w({
        startDate: k,
        endDate: y
      }), m && m({
        startDate: k,
        endDate: y
      }, "AUTOMATIC"), $(E.formatDateRangePickerInput(k, y));
    }
  }, [I]), h(() => {
    M.current && ge(M.current);
  }, [M.current]), h(() => {
    w(p);
  }, [p]);
  const Re = fe({
    height: 48,
    width: s === "date-range-picker" ? "52px" : "185px"
  }), Ce = fe({
    height: 48,
    width: s === "static-range-picker" ? "52px" : "185px"
  }), te = (e) => {
    if (!i) {
      if (e === "left" && s === "static-range-picker") {
        c(!0), x(!0);
        return;
      }
      if (e === "right" && s === "date-range-picker") {
        c(!0), A(!0);
        return;
      }
      W((a) => {
        const t = a === "static-range-picker" ? "date-range-picker" : "static-range-picker";
        return setTimeout(() => {
          var r, n;
          t === "date-range-picker" ? (r = N == null ? void 0 : N.current) == null || r.focus() : (n = b == null ? void 0 : b.current) == null || n.focus();
        }, 0), Q && Q(t), t;
      });
    }
  }, ae = (e, a) => {
    if (i)
      return;
    const {
      startDate: t,
      endDate: r
    } = e.selection;
    if (c(!0), a === "left") {
      if (t === void 0 && r === void 0) {
        c(!1);
        return;
      }
      w({
        startDate: t,
        endDate: r
      });
      const n = E.formatDateRangePickerInput(t, r);
      $(n);
    }
    if (a === "right") {
      B({
        startDate: t,
        endDate: r
      });
      const n = I.find((l) => V(l.range().startDate) === V(t) && V(l.range().endDate) === V(r));
      n != null && n.label && Y(G.get(n.label, n.replacements).value);
    }
    m && m({
      startDate: t,
      endDate: r
    }, "MANUAL");
  }, O = (e, a) => {
    const t = e ?? C.current.startDate, r = a ?? C.current.endDate;
    i || S && (e && (C.current.startDate = e), a && (C.current.endDate = a), t && r && (S && S(t, r), C.current.startDate = void 0, C.current.endDate = void 0, g(!1), c(!1), x(!1), A(!1)), !t && !r && (S && S(void 0, void 0), g(!1), c(!1), x(!1), A(!1)));
  }, Ie = P(Se, i ? "date-range-picker-disabled" : "");
  return z(Ae, {
    className: pe,
    testId: `${R}-container`,
    children: [z("div", {
      "data-testid": `${R}-references`,
      ref: M,
      style: {
        cursor: i ? "not-allowed" : "pointer"
      },
      className: Ie,
      children: [d(de.div, {
        "data-testid": `${R}-left-reference-container`,
        style: Re,
        onClick: () => te("left"),
        className: P(s === "date-range-picker" ? "input-closed" : ""),
        children: d(le, {
          id: "datepicker-reference-input",
          testId: `${R}-datepicker-reference-input`,
          value: De,
          type: "text",
          disabled: i,
          onFocus: () => {
            i || (x(!0), c(!1));
          },
          onBlur: () => x(!1),
          onChange: (e) => {
            var l;
            if (i) return;
            const a = (D, k) => {
              var ie;
              if (!k)
                return !1;
              const y = (ie = F[D]) == null ? void 0 : ie.getFullYear(), re = H(`${k.day} ${k.month} ${y}`, "D M YYYY"), ne = {
                ...F
              };
              re.isValid() && (ne[D] = re.toDate(), w(ne));
            }, t = ((l = e == null ? void 0 : e.target) == null ? void 0 : l.value) ?? "", {
              startDate: r,
              endDate: n
            } = E.formatStringToDates(t);
            a("startDate", r), a("endDate", n), $(t);
          },
          inputRef: b,
          textFieldProps: {
            InputProps: {
              inputComponent: Ne
            },
            inputProps: {
              readOnly: s === "date-range-picker" || i,
              "data-testid": "datepicker-reference"
            }
          },
          className: P("input-left", s === "date-range-picker" ? "input-closed" : ""),
          endAdornment: d(ce, {
            onClick: () => {
              var e;
              i || (e = b.current) == null || e.focus();
            }
          })
        })
      }), d(de.div, {
        "data-testid": `${R}-right-reference-container`,
        onClick: () => te("right"),
        style: Ce,
        className: P(s === "static-range-picker" ? "input-closed" : ""),
        children: d(le, {
          testId: `${R}-rangeselector-reference-input`,
          value: ke,
          type: "text",
          disabled: i,
          onFocus: () => {
            i || (A(!0), c(!1));
          },
          onBlur: () => A(!1),
          onChange: (e) => {
          },
          inputRef: N,
          textFieldProps: {
            inputProps: {
              readOnly: !0,
              "data-testid": "rangeselector-reference-input"
            }
          },
          className: P("input-right", s === "static-range-picker" ? "input-closed" : ""),
          endAdornment: d(ce, {
            onClick: () => {
              var e;
              i || (e = N.current) == null || e.focus();
            }
          })
        })
      })]
    }), d(we, {
      open: U && !!X && !i,
      anchorEl: X,
      transition: !0,
      className: "date-range-picker-popper",
      children: ({
        TransitionProps: e
      }) => d($e, {
        ...e,
        timeout: 350,
        children: d("div", {
          className: P("date-range-picker-dropdown", s === "date-range-picker" ? "date-range-picker-dropdown-predefined" : ""),
          children: z("div", {
            ref: j,
            style: {
              width: 238,
              top: 20
            },
            children: [d(oe, {
              onChange: (a) => ae(a, "left"),
              view: "calendar",
              date: F,
              condition: s === "static-range-picker",
              maxDate: K,
              minDate: T,
              onSelectionComplete: () => {
                g(!1), g(!1);
              },
              onDateClick: (a, t, r) => {
                r === "single" && s === "static-range-picker" && O && O(a, t);
              }
            }), d(oe, {
              onChange: (a) => ae(a, "right"),
              initialDate: p,
              view: "predefined",
              staticRanges: I,
              condition: s === "date-range-picker",
              maxDate: K,
              onSelectionComplete: () => {
                g(!1), g(!1);
              },
              minDate: T,
              onDateClick: (a, t, r) => {
                r === "range" && s === "date-range-picker" && O && O(a, t);
              }
            })]
          })
        })
      })
    })]
  });
};
export {
  nt as DateRangePicker
};
