import { differenceInCalendarDays as T } from "date-fns";
import { normalizeSameMomentDateTimeRangeValue as M, createDateRangeValue as h } from "../../DateRangeCalendarPicker/helpers.js";
import { getMaskedFieldSectionRangeFromSelection as N, getMaskedFieldNextEditablePosition as _, getMaskedFieldSectionsFromSelection as A, getAdjacentMaskedFieldSectionRange as p, getMaskedFieldBoundarySectionRange as y } from "../../../utils/maskedFieldSections.js";
import { DATE_TIME_RANGE_PICKER_RANGE_SEPARATOR as R, DATE_TIME_RANGE_PICKER_DAY_LABEL as b, DATE_TIME_RANGE_PICKER_DAYS_LABEL as I, DATE_TIME_RANGE_PICKER_SELECTED_RANGE_LABEL as S, DATE_TIME_RANGE_PICKER_AMPM_MASK as $, DATE_TIME_RANGE_PICKER_MILITARY_MASK as P, DATE_TIME_RANGE_PICKER_SECTION_DEFAULTS as i } from "../constants.js";
const s = /* @__PURE__ */ Symbol("INVALID_DATE_TIME_SIDE");
function g(e) {
  return `${e.startDate?.getTime() ?? "null"}:${e.endDate?.getTime() ?? "null"}`;
}
function H(e) {
  return M(
    h(
      e.startDate ? new Date(e.startDate) : null,
      e.endDate ? new Date(e.endDate) : null
    )
  );
}
function L(e, n) {
  return g(e) === g(n);
}
function w(e) {
  return e === "ampm" ? $ : P;
}
function m(e) {
  const n = [], a = [
    { replacement: i.day, section: "startDay" },
    { literal: "/" },
    { replacement: i.month, section: "startMonth" },
    { literal: "/" },
    { replacement: i.year, section: "startYear" },
    { literal: " " },
    { replacement: i.hours, section: "startHours" },
    { literal: ":" },
    { replacement: i.minutes, section: "startMinutes" },
    { literal: ":" },
    { replacement: i.seconds, section: "startSeconds" },
    ...e === "ampm" ? [
      { literal: " " },
      {
        replacement: i.period,
        section: "startPeriod"
      }
    ] : [],
    { literal: R },
    { replacement: i.day, section: "endDay" },
    { literal: "/" },
    { replacement: i.month, section: "endMonth" },
    { literal: "/" },
    { replacement: i.year, section: "endYear" },
    { literal: " " },
    { replacement: i.hours, section: "endHours" },
    { literal: ":" },
    { replacement: i.minutes, section: "endMinutes" },
    { literal: ":" },
    { replacement: i.seconds, section: "endSeconds" },
    ...e === "ampm" ? [
      { literal: " " },
      {
        replacement: i.period,
        section: "endPeriod"
      }
    ] : []
  ];
  let r = 0;
  return a.forEach((o) => {
    if ("literal" in o) {
      r += o.literal.length;
      return;
    }
    n.push({
      end: r + o.replacement.length,
      replacement: o.replacement,
      section: o.section,
      start: r
    }), r += o.replacement.length;
  }), n;
}
function C(e, n, a) {
  return N(
    m(a),
    e,
    n
  );
}
function B(e, n, a) {
  return p(
    m(a),
    e,
    n
  );
}
function V(e, n) {
  return y(
    m(n),
    e
  );
}
function j(e, n, a = !0) {
  return _(
    m(n),
    e,
    (r) => r.section.endsWith("Period") ? a ? [r.start] : [] : Array.from({ length: r.end - r.start }, (o, t) => r.start + t)
  );
}
function E(e, n, a, r) {
  return `${e.slice(0, n)}${r}${e.slice(a)}`;
}
function k(e, n, a, r) {
  return A(
    m(r),
    e,
    n,
    a,
    {
      includeAdjacentWhenNoDirectHit: !0
    }
  );
}
function U(e, n, a) {
  return m(a).reduce((r, o) => n.includes(o.section) ? E(
    r,
    o.start,
    o.end,
    o.replacement
  ) : r, e);
}
function O(e, n, a) {
  const r = m(a).filter((t) => t.section.endsWith("Period"));
  if (r.length === 0)
    return null;
  const o = r.find((t) => t.start < n && t.end > e) ?? r.find((t) => e >= t.start && e <= t.end);
  return o || (r.find((t) => t.start >= e) ?? r[0]);
}
function F(e, n, a, r) {
  if (n.endsWith("Period"))
    return e === "PM" ? "AM" : "PM";
  const o = n.endsWith("Month") ? 12 : n.endsWith("Day") ? 31 : n.endsWith("Year") ? 9999 : n.endsWith("Minutes") || n.endsWith("Seconds") ? 59 : n.endsWith("Hours") ? r === "ampm" ? 12 : 23 : 0, u = (Number(e) + a + o + 1) % (o + 1);
  return String(u).padStart(e.length, "0");
}
function q(e, n, a, r, o) {
  const t = C(
    n,
    a,
    o
  );
  return t ? {
    nextValue: E(
      e,
      t.start,
      t.end,
      F(
        e.slice(t.start, t.end),
        t.section,
        r,
        o
      )
    ),
    sectionRange: t
  } : null;
}
function z(e, n) {
  return /^[\d/:\sapm-]*$/i.test(e);
}
function D(e, n) {
  if (!e)
    return n === "ampm" ? "00/00/0000 00:00:00 AM" : "00/00/0000 00:00:00";
  const a = String(e.getDate()).padStart(2, "0"), r = String(e.getMonth() + 1).padStart(2, "0"), o = String(e.getFullYear()).padStart(4, "0"), t = String(e.getMinutes()).padStart(2, "0"), u = String(e.getSeconds()).padStart(2, "0");
  if (n === "ampm") {
    const c = e.getHours() >= 12 ? "PM" : "AM", l = e.getHours() % 12 || 12;
    return `${a}/${r}/${o} ${String(l).padStart(2, "0")}:${t}:${u} ${c}`;
  }
  const d = String(e.getHours()).padStart(2, "0");
  return `${a}/${r}/${o} ${d}:${t}:${u}`;
}
function J(e, n) {
  return `${D(e.startDate, n)}${R}${D(e.endDate, n)}`;
}
function f(e, n) {
  if (e.isEmpty)
    return null;
  if (e.day < 1 || e.day > 31 || e.month < 1 || e.month > 12 || e.year < 1 || e.minutes > 59 || e.seconds > 59)
    return s;
  let a = e.hours;
  if (n === "ampm") {
    if (e.hours < 1 || e.hours > 12 || !e.period)
      return s;
    a = e.period === "PM" ? e.hours % 12 + 12 : e.hours % 12;
  } else if (e.hours > 23)
    return s;
  const r = new Date(
    e.year,
    e.month - 1,
    e.day,
    a,
    e.minutes,
    e.seconds,
    0
  );
  return Number.isNaN(r.getTime()) || r.getFullYear() !== e.year || r.getMonth() !== e.month - 1 || r.getDate() !== e.day ? s : r;
}
function Q(e, n) {
  const a = e.trim(), r = a.match(
    /^(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2}):(\d{2}) - (\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2}):(\d{2})$/
  ), o = a.match(
    /^(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2}):(\d{2}) (AM|PM) - (\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2}):(\d{2}) (AM|PM)$/i
  );
  if (n === "military" && !r || n === "ampm" && !o)
    return null;
  const t = n === "ampm" ? o : r;
  if (!t)
    return null;
  const u = n === "ampm" ? {
    day: Number(t[1]),
    hours: Number(t[4]),
    isEmpty: t[1] === "00" && t[2] === "00" && t[3] === "0000" && t[4] === "00" && t[5] === "00" && t[6] === "00",
    minutes: Number(t[5]),
    month: Number(t[2]),
    period: t[7].toUpperCase(),
    seconds: Number(t[6]),
    year: Number(t[3])
  } : {
    day: Number(t[1]),
    hours: Number(t[4]),
    isEmpty: t[1] === "00" && t[2] === "00" && t[3] === "0000" && t[4] === "00" && t[5] === "00" && t[6] === "00",
    minutes: Number(t[5]),
    month: Number(t[2]),
    seconds: Number(t[6]),
    year: Number(t[3])
  }, d = n === "ampm" ? {
    day: Number(t[8]),
    hours: Number(t[11]),
    isEmpty: t[8] === "00" && t[9] === "00" && t[10] === "0000" && t[11] === "00" && t[12] === "00" && t[13] === "00",
    minutes: Number(t[12]),
    month: Number(t[9]),
    period: t[14].toUpperCase(),
    seconds: Number(t[13]),
    year: Number(t[10])
  } : {
    day: Number(t[7]),
    hours: Number(t[10]),
    isEmpty: t[7] === "00" && t[8] === "00" && t[9] === "0000" && t[10] === "00" && t[11] === "00" && t[12] === "00",
    minutes: Number(t[11]),
    month: Number(t[8]),
    seconds: Number(t[12]),
    year: Number(t[9])
  }, c = f(u, n), l = f(d, n);
  return c === s || l === s || !c && l ? null : h(c, l);
}
function X(e, n, a) {
  return e.find((o) => L(o.resolveValue(a), n))?.id ?? null;
}
function x(e) {
  return !e.startDate || !e.endDate ? 0 : Math.max(0, T(e.endDate, e.startDate));
}
function Z(e, n) {
  const a = x(e), r = a === 1 ? n?.dayLabel ?? b : n?.daysLabel ?? I;
  return `${n?.selectedRangeLabel ?? S}: ${a} ${r}`;
}
export {
  L as areDateTimeRangeValuesEqual,
  H as cloneDateTimeRangeValue,
  Z as formatDateTimeRangeSelectedDaysText,
  D as formatDateTimeRangeSide,
  J as formatDateTimeRangeValue,
  B as getAdjacentDateTimeRangeSectionRange,
  V as getDateTimeRangeBoundarySectionRange,
  w as getDateTimeRangeInputMask,
  j as getDateTimeRangeNextEditablePosition,
  C as getDateTimeRangeSectionRangeFromSelection,
  m as getDateTimeRangeSectionRanges,
  k as getDateTimeRangeSectionsFromSelection,
  x as getDateTimeRangeSelectedDayCount,
  g as getDateTimeRangeValueKey,
  z as isAllowedDateTimeRangeFieldValue,
  Q as parseDateTimeRangeInput,
  E as replaceDateTimeRangeInputSubstring,
  U as resetDateTimeRangeInputSections,
  O as resolveDateTimeRangePeriodRange,
  X as resolveDateTimeRangeShortcutId,
  q as stepDateTimeRangeInputSection
};
