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