import { TIME_PICKER_VISIBLE_SEGMENTS as c, TIME_PICKER_DEFAULT_VALUE_PARTS as M } from "./constants.js";
import { getMaskedFieldSectionRangeFromSelection as N, getMaskedFieldNextEditablePosition as b, getMaskedFieldSectionsFromSelection as I, getAdjacentMaskedFieldSectionRange as R, getMaskedFieldBoundarySectionRange as V } from "../../utils/maskedFieldSections.js";
function j() {
  return { ...M };
}
function y(e, t) {
  return e.hours24 === t.hours24 && e.minutes === t.minutes && e.seconds === t.seconds;
}
function E(e, t) {
  if (!/^\d+$/.test(e))
    return null;
  const n = t * 2;
  return e.length !== n ? null : Array.from({ length: t }, (r, i) => e.slice(i * 2, i * 2 + 2));
}
function A(e, t) {
  const n = e.trim();
  if (!n)
    return null;
  const r = c[t].length, i = n.match(/\b(am|pm)\b/i), o = n.replace(/\b(am|pm)\b/gi, "").replace(/\s+/g, "");
  if (!o)
    return null;
  const s = o.includes(":") ? o.split(":").filter(Boolean) : E(o, r) ?? [o];
  if (s.length > r || s.some((f) => !/^\d{1,2}$/.test(f)))
    return null;
  const [l = "0", S = "0", h = "0"] = s, u = Number(l), a = Number(S), d = Number(h);
  if (Number.isNaN(u) || Number.isNaN(a) || Number.isNaN(d) || a > 59 || d > 59)
    return null;
  if (i) {
    if (u < 1 || u > 12)
      return null;
    const T = i[1].toUpperCase() === "PM" ? u % 12 + 12 : u % 12;
    return {
      hours24: String(T).padStart(2, "0"),
      minutes: String(a).padStart(2, "0"),
      seconds: String(d).padStart(2, "0")
    };
  }
  return u < 0 || u > 23 ? null : {
    hours24: String(u).padStart(2, "0"),
    minutes: String(a).padStart(2, "0"),
    seconds: String(d).padStart(2, "0")
  };
}
function F(e, t) {
  if (t === "military")
    return {
      hours: e.hours24,
      minutes: e.minutes,
      seconds: e.seconds,
      period: Number(e.hours24) >= 12 ? "PM" : "AM"
    };
  const n = Number(e.hours24), r = n >= 12 ? "PM" : "AM", i = n % 12 || 12;
  return {
    hours: String(i).padStart(2, "0"),
    minutes: e.minutes,
    seconds: e.seconds,
    period: r
  };
}
function C(e, t) {
  if (t === "military")
    return {
      hours24: e.hours,
      minutes: e.minutes,
      seconds: e.seconds
    };
  const n = Number(e.hours) % 12, r = e.period === "PM" ? n + 12 : n;
  return {
    hours24: String(r).padStart(2, "0"),
    minutes: e.minutes,
    seconds: e.seconds
  };
}
function $(e, t, n) {
  const r = F(e, t), o = c[n].map((s) => r[s]).join(":");
  return t === "military" ? o : `${o} ${r.period}`;
}
function L(e, t, n) {
  const i = c[n].map((o) => e[o]).join(":");
  return t === "military" ? i : `${i} ${e.period}`;
}
function w(e, t) {
  const n = [...c[e]];
  return t === "ampm" && n.push("period"), n;
}
function p(e, t) {
  const n = [];
  let r = 0;
  return c[e].forEach((i, o, s) => {
    n.push({
      segment: i,
      start: r,
      end: r + 2
    }), r += 2, o < s.length - 1 && (r += 1);
  }), t === "ampm" && (r += 1, n.push({
    segment: "period",
    start: r,
    end: r + 2
  })), n;
}
function m(e, t) {
  return p(e, t).map((n) => ({
    end: n.end,
    section: n.segment,
    start: n.start
  }));
}
function D(e, t, n, r) {
  return N(
    m(n, r),
    e,
    t
  );
}
function z(e, t, n, r) {
  return R(
    m(n, r),
    e,
    t
  );
}
function U(e, t, n) {
  return V(
    m(t, n),
    e
  );
}
function H(e, t, n, r = !0) {
  return b(
    m(t, n),
    e,
    (i) => i.section === "period" ? r ? [i.start] : [] : [i.start, i.start + 1]
  );
}
function g(e, t, n, r) {
  return `${e.slice(0, t)}${r}${e.slice(n)}`;
}
function K(e, t, n, r, i) {
  return p(n, r).reduce((o, s) => t.includes(s.segment) ? g(
    o,
    s.start,
    s.end,
    _(s.segment)
  ) : o, e);
}
function q(e, t, n, r, i, o) {
  return I(
    m(i, o),
    t,
    n,
    r
  );
}
function G(e, t, n, r) {
  if (n > t)
    return /[:\s]/.test(e.slice(t, n));
  const i = r === "Backspace" ? t - 1 : t;
  return i < 0 || i >= e.length ? !1 : /[:\s]/.test(e[i] ?? "");
}
function _(e, t, n) {
  return e === "period" ? "AM" : "00";
}
function k(e, t, n, r) {
  if (t === "period")
    return e === "PM" ? "AM" : "PM";
  const i = t === "hours" ? r === "ampm" ? 12 : 23 : 59, s = (Number(e) + n + i + 1) % (i + 1);
  return String(s).padStart(e.length, "0");
}
function P(e, t, n, r, i, o) {
  const s = D(
    t,
    n,
    i,
    o
  );
  return s ? {
    nextValue: g(
      e,
      s.start,
      s.end,
      k(
        e.slice(s.start, s.end),
        s.section,
        r,
        o
      )
    ),
    sectionRange: s
  } : null;
}
function J(e, t) {
  return /^[\d:\sapm]*$/i.test(e);
}
function O(e, t, n) {
  if (!e.trim())
    return "";
  const r = A(e, n);
  return r ? $(r, t, n) : "";
}
export {
  y as areTimeValuePartsEqual,
  j as createDefaultTimeValueParts,
  L as formatDisplayPartsValue,
  $ as formatTimeValue,
  C as fromDisplayParts,
  z as getAdjacentTimeInputSectionRange,
  _ as getDefaultDisplaySegmentValue,
  H as getNextEditableTimePosition,
  q as getSegmentsToResetFromSelection,
  U as getTimeInputBoundarySectionRange,
  D as getTimeInputSectionRangeFromSelection,
  p as getTimeInputSegmentRanges,
  w as getVisibleSegments,
  J as isAllowedTimeInputValue,
  G as isProtectedTimeCharacterSelection,
  O as normalizeTimeValue,
  A as parseTimeValue,
  g as replaceTimeInputSubstring,
  K as resetTimeInputSegments,
  E as splitTimeDigits,
  P as stepTimeInputSection,
  F as toDisplayParts
};
