import { jsxs as d, jsx as n, Fragment as ee } from "react/jsx-runtime";
import { useState as V, useRef as y, useEffect as P } from "react";
import { IMaskInput as se, IMask as I } from "react-imask";
import { noop as te } from "es-toolkit/compat";
import { isNil as ne } from "es-toolkit/predicate";
import O from "moment";
import oe from "tiny-invariant";
import re from "../selects/BaseSelectDropdown.js";
import ae from "../../hooks/useClickOutside.js";
import ie from "../../hooks/useMergeRefs.js";
import T from "../../utils/classNames.js";
const b = 15, ce = /^(?:\d|[01]\d|2[0-3]):[0-5]\d$/, C = (e) => e >= 10 ? `${e}` : `0${e}`, p = (e, t) => `${C(e)}:${C(t)}`, D = (e, t = b) => Math.floor(e.minutes() / t) * t, le = (e) => Number.isInteger(e) && e > 0 && e <= 60, me = (e) => le(e) ? e : b, de = (e) => {
  const t = [];
  for (let a = 0; a < 1440; a += e) {
    const f = Math.floor(a / 60), i = a % 60;
    t.push(p(f, i));
  }
  return t;
}, ue = () => {
  const e = /* @__PURE__ */ new Date(), t = e.getHours(), r = e.getMinutes();
  return p(t, r);
}, S = (e) => ce.test(e), R = (e) => {
  if (!!ne(e))
    return !1;
  const r = e === "" || S(e);
  return r || oe(!1, 'Wrong time format. Please pass in value with the following format: "hh:mm".'), r;
}, be = (e) => {
  const {
    value: t,
    onChange: r = te,
    alwaysShowMask: a = !0,
    showIcon: f = !1,
    dropdown: i = !1,
    minuteOffset: $ = b,
    disabled: F = !1,
    errorMessage: h,
    warningMessage: g,
    messageWhiteSpace: E = "normal",
    className: H = "",
    inputProps: k = {},
    ...A
  } = e, W = R(t) ? t : ue(), [c, l] = V(W), [x, N] = V(!1), m = me($), u = F || !!k.disabled, j = y(null), w = y(!1), M = y(!1), B = ae(() => {
    i && N(!1);
  }), L = ie(j, B);
  P(() => {
    R(t) && l(t);
  }, [t]), P(() => (w.current = !0, () => {
    w.current = !1;
  }), []);
  const U = (s) => {
    const o = s.target;
    (!o.value || o.value.startsWith("-")) && setTimeout(() => {
      o.setSelectionRange(0, 0);
    }, 0), k.onFocus?.(s), i && !u && N(!0);
  }, z = (s, o) => {
    if (M.current) {
      M.current = !1;
      return;
    }
    if (l(s), !!w.current) {
      if (o.masked.isComplete) {
        r(s);
        return;
      }
      o.unmaskedValue === "" && r("");
    }
  }, _ = S(c), q = () => {
    const s = O(`2020-01-01 ${c}`), o = D(s, m);
    s.minute(o).second(0), s.add(m, "minutes");
    const v = p(s.hours(), s.minutes());
    l(v);
  }, G = () => {
    const s = O(`2020-01-01 ${c}`), o = D(s, m);
    s.minute(o).second(0), s.subtract(m, "minutes");
    const v = p(s.hours(), s.minutes());
    l(v);
  }, { className: J, ...K } = k, Q = de(m).map((s) => ({
    id: s,
    label: s,
    selected: s === c
  })), X = (s) => {
    !s || u || (M.current = !0, l(s.id), r(s.id), N(!1));
  }, Y = h || g, Z = T("TimePicker", i && "dropdown", x && "open", H);
  return /* @__PURE__ */ d("div", { ...A, className: Z, ref: L, children: [
    /* @__PURE__ */ d("div", { className: "input-group", children: [
      f && /* @__PURE__ */ n("span", { className: "input-group-addon", children: /* @__PURE__ */ n("span", { className: "rioglyph rioglyph-time-alt" }) }),
      /* @__PURE__ */ d("div", { className: "form-control-feedback-wrapper", children: [
        /* @__PURE__ */ n(
          se,
          {
            ...K,
            className: T("TimePickerInput", "form-control", J),
            disabled: u,
            mask: "HH:MM",
            blocks: {
              HH: {
                mask: I.MaskedRange,
                from: 0,
                to: 23,
                maxLength: 2
              },
              MM: {
                mask: I.MaskedRange,
                from: 0,
                to: 59,
                maxLength: 2
              }
            },
            value: c,
            onAccept: z,
            onFocus: U,
            lazy: !a,
            placeholderChar: "-",
            overwrite: !0
          }
        ),
        Y && /* @__PURE__ */ d(ee, { children: [
          h && /* @__PURE__ */ n("span", { className: "form-control-feedback rioglyph rioglyph-error-sign" }),
          g && /* @__PURE__ */ n("span", { className: "form-control-feedback rioglyph rioglyph-warning-sign" }),
          /* @__PURE__ */ n("span", { className: `help-block white-space-${E}`, children: /* @__PURE__ */ n("span", { children: h || g }) })
        ] })
      ] }),
      _ && /* @__PURE__ */ d(
        "div",
        {
          className: T(
            "TimePickerIncreaseButton input-group-addon",
            u && "pointer-events-none bg-lightest"
          ),
          children: [
            /* @__PURE__ */ n(
              "div",
              {
                className: "text-color-gray hover-text-color-dark display-grid place-items-center cursor-pointer",
                onClick: G,
                children: /* @__PURE__ */ n("div", { className: "rioglyph rioglyph-chevron-left scale-90" })
              }
            ),
            /* @__PURE__ */ n(
              "div",
              {
                className: "text-color-gray hover-text-color-dark margin-left-5 display-grid place-items-center cursor-pointer",
                onClick: q,
                children: /* @__PURE__ */ n("div", { className: "rioglyph rioglyph-chevron-right scale-90" })
              }
            )
          ]
        }
      )
    ] }),
    i && /* @__PURE__ */ n(
      re,
      {
        isOpen: x,
        options: Q,
        onSelect: X,
        dropdownClassName: "max-height-200 overflow-auto width-100pct"
      }
    )
  ] });
};
export {
  be as default
};
//# sourceMappingURL=TimePicker.js.map
