import { jsxs as $, Fragment as Te, jsx as p } from "react/jsx-runtime";
import { u as Qe, a as Oe, b as We, c as qe, F as Fe, s as Ne, d as Ve } from "../vendor.js";
import { useRef as Ye, useState as k, useMemo as Ee, useCallback as m, useEffect as _e } from "react";
import { t as j } from "../global.module.js";
import { b as Ke } from "../tools/icons.js";
import { c as ze, s as He, i as D, l as Le, b as K, a as Ue } from "../tools/tools.js";
import { D as z } from "./date-range-picker-date.js";
import { D as H } from "./date-range-picker-datetime.js";
import { D as L } from "./date-range-picker-month.js";
import { D as U } from "./date-range-picker-quarter.js";
import { D as G } from "./date-range-picker-time.js";
import { D as J } from "./date-range-picker-week.js";
import { D as X } from "./date-range-picker-year.js";
import { f as Ge } from "../lite.js";
const Je = /* @__PURE__ */ new Date(), h = function(Z) {
  const {
    as: I = "div",
    className: v,
    disabled: P,
    dropOldClass: M,
    locale: R,
    onChange: c,
    onInputValueAvailable: a,
    pickerType: l = "date",
    placeholder: A,
    readOnly: b,
    style: w,
    value: T,
    variables: Q,
    ...B
  } = Z, C = Ye(null), [ee, f] = k(""), [O, u] = k(!1), [te, W] = k([]), [se, q] = k([]), [ae, F] = k([]), [oe, S] = k([]), [re, ce] = k([]), [ne, N] = k([]), [ie, V] = k([]), de = A ?? `Please select a ${l}`, { context: Y, floatingStyles: le, refs: E } = Qe({
    middleware: [
      Ne({
        apply({ availableHeight: o, elements: e, rects: t }) {
          Object.assign(e.floating.style, {
            maxHeight: `${Math.max(0, o)}px`,
            minWidth: `${t.reference.width}px`
          });
        }
      })
    ],
    onOpenChange: u,
    open: O,
    whileElementsMounted: Ve
  }), me = Oe(Y, {
    enabled: !(P || b),
    toggle: !1
  }), fe = We(Y), { getFloatingProps: pe, getReferenceProps: ue } = qe([me, fe]), ke = Ee(() => {
    const o = ze(!M && "position-relative", v), e = He((t, s) => ({ tKey: Ue(s) }), Q, w);
    return {
      className: o,
      style: e
    };
  }, [v, M, w, Q]), _ = m(() => {
    var o;
    (o = C.current) == null || o.focus();
  }, []), d = m(() => {
    var o;
    (o = C.current) == null || o.blur();
  }, []), De = m(() => {
    P || b || (_(), u((o) => !o));
  }, [P, _, b]), y = m(
    (o, e) => {
      const t = e ?? l, s = o ?? T ?? [], n = D(Le(s[0], s[1]), t);
      if (n) {
        const i = n.formattedValue, r = n.computedDate, x = a == null ? void 0 : a(i, r, t);
        if (typeof x == "boolean" && !x) {
          f("");
          return;
        }
        f(i.join(" ~ ")), t === "date" ? W(r) : t === "week" ? q(r) : t === "month" ? F(r) : t === "quarter" ? S(r) : t === "year" ? ce(r) : t === "datetime" ? N(r) : t === "time" && V(r);
      } else o && f(o.join(" ~ "));
    },
    [a, l, T]
  ), he = m(
    (o) => {
      const e = o.target.value.trim();
      if (e) {
        let t = null, s = null;
        const n = /^\d{4}-\d{2}-\d{2} ~ \d{4}-\d{2}-\d{2}$/.test(e), i = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} ~ \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(
          e
        ), r = /^\d{4}-\d{2} ~ \d{4}-\d{2}$/.test(e), x = /^\d{4} Q[1-4] ~ \d{4} Q[1-4]$/.test(e), Se = /^\d{2}:\d{2}:\d{2} ~ \d{2}:\d{2}:\d{2}$/.test(e), $e = /^\d{4}-\d{2}-\d{2} ~ \d{4}-\d{2}-\d{2}$/.test(e), je = /^\d{4} ~ \d{4}$/.test(e);
        if (n ? (s = e.split(" ~ "), t = "date") : i ? (s = e.split(" ~ "), t = "datetime") : r ? (s = e.split(" ~ ").map((g) => `${g}-01`), t = "month") : x ? (s = e.split(" ~ ").map((g) => {
          const [ve, Me] = g.split(" "), we = (parseInt(Me.replace("Q", ""), 10) - 1) * 3 + 1;
          return `${ve}-${String(we).padStart(2, "0")}-01`;
        }), t = "quarter") : Se ? (s = e.split(" ~ ").map((g) => Ge(Je, "yyyy-MM-dd") + " " + g), t = "time") : $e ? (s = e.split(" ~ "), t = "week") : je && (s = e.split(" ~ ").map((g) => `${g}-01-01`), t = "year"), s && t === l) {
          y(s, t);
          return;
        }
      }
      f(e);
    },
    [y, l]
  ), ge = m(
    (o) => {
      const e = D(o.date, "date");
      if (!e)
        return;
      const t = e.formattedValue, s = e.computedDate, n = a == null ? void 0 : a(t, s, "date");
      if (typeof n == "boolean" && !n)
        return;
      const i = t, r = s;
      f(i.join(" ~ ")), W(r), u(!1), d(), c == null || c(i, r, "date");
    },
    [c, d, a]
  ), Re = m(
    (o) => {
      const e = D(o.date, "week");
      if (!e)
        return;
      const t = e.formattedValue, s = e.computedDate, n = a == null ? void 0 : a(t, s, "week");
      if (typeof n == "boolean" && !n)
        return;
      const i = t, r = s;
      f(i.join(" ~ ")), q(r), u(!1), d(), c == null || c(i, r, "week");
    },
    [c, d, a]
  ), Pe = m(
    (o) => {
      const e = D(o.date, "month");
      if (!e)
        return;
      const t = e.formattedValue, s = e.computedDate, n = a == null ? void 0 : a(t, s, "month");
      if (typeof n == "boolean" && !n)
        return;
      const i = t, r = s;
      f(i.join(" ~ ")), F(r), u(!1), d(), c == null || c(i, r, "month");
    },
    [c, d, a]
  ), be = m(
    (o) => {
      const e = D(o.date, "quarter");
      if (!e)
        return;
      const t = e.formattedValue, s = e.computedDate, n = a == null ? void 0 : a(t, s, "quarter");
      if (typeof n == "boolean" && !n)
        return;
      const i = t, r = s;
      f(i.join(" ~ ")), S(r), u(!1), d(), c == null || c(i, r, "quarter");
    },
    [c, d, a]
  ), ye = m(
    (o) => {
      const e = D(o.date, "year");
      if (!e)
        return;
      const t = e.formattedValue, s = e.computedDate, n = a == null ? void 0 : a(t, s, "year");
      if (typeof n == "boolean" && !n)
        return;
      const i = t, r = s;
      f(i.join(" ~ ")), S(r), u(!1), d(), c == null || c(i, r, "year");
    },
    [c, d, a]
  ), xe = m(
    (o) => {
      const e = D(o.date, "datetime");
      if (!e)
        return;
      const t = e.formattedValue, s = e.computedDate, n = a == null ? void 0 : a(t, s, "datetime");
      if (typeof n == "boolean" && !n)
        return;
      const i = t, r = s;
      f(i.join(" ~ ")), N(r), u(!1), d(), c == null || c(i, r, "datetime");
    },
    [c, d, a]
  ), Ce = m(
    (o) => {
      const e = D(o.date, "time");
      if (!e)
        return;
      const t = e.formattedValue, s = e.computedDate, n = a == null ? void 0 : a(t, s, "time");
      if (typeof n == "boolean" && !n)
        return;
      const i = t, r = s;
      f(i.join(" ~ ")), V(r), u(!1), d(), c == null || c(i, r, "time");
    },
    [c, d, a]
  );
  return _e(() => {
    y();
  }, [y]), /* @__PURE__ */ $(Te, { children: [
    /* @__PURE__ */ $(I, { ...B, ...ke, ...ue(), ref: E.setReference, children: [
      /* @__PURE__ */ p(
        "input",
        {
          className: K(null, "form-control", j.brlPe9),
          disabled: P,
          onChange: he,
          placeholder: de,
          readOnly: b,
          ref: C,
          type: "text",
          value: ee
        }
      ),
      /* @__PURE__ */ p(
        "div",
        {
          className: K(
            null,
            "position-absolute top-50 translate-middle-y d-inline-flex",
            j.brlEnd3,
            P || b ? "text-secondary" : j.brlCursorPointer
          ),
          onClick: De,
          children: /* @__PURE__ */ p(Ke, {})
        }
      )
    ] }),
    O && /* @__PURE__ */ p(Fe, { children: /* @__PURE__ */ $("div", { className: "card card-body shadow", ...pe(), ref: E.setFloating, style: le, children: [
      l === "date" && /* @__PURE__ */ p(z, { locale: R, onDateClick: ge, selectedDate: te }),
      l === "week" && /* @__PURE__ */ p(J, { locale: R, onDateClick: Re, selectedDate: se }),
      l === "month" && /* @__PURE__ */ p(L, { locale: R, onDateClick: Pe, selectedDate: ae }),
      l === "quarter" && /* @__PURE__ */ p(U, { locale: R, onDateClick: be, selectedDate: oe }),
      l === "year" && /* @__PURE__ */ p(X, { locale: R, onDateClick: ye, selectedDate: re }),
      l === "datetime" && /* @__PURE__ */ p(H, { locale: R, onDateClick: xe, selectedDate: ne }),
      l === "time" && /* @__PURE__ */ p(G, { onDateClick: Ce, selectedDate: ie })
    ] }) })
  ] });
};
h.Date = z;
h.Week = J;
h.Month = L;
h.Quarter = U;
h.Year = X;
h.Time = G;
h.Datetime = H;
h.displayName = "BRL.DateRangePicker";
export {
  h as D
};
//# sourceMappingURL=date-range-picker.js.map
