import { r as H, c as N, h as n, H as V, g as E } from "./index.61732cef.js";
import "react";
import "react-dom";
function w(e, t) {
  const s = new Date(e);
  return s.setDate(s.getDate() + t), s;
}
function K(e, t, s) {
  const a = [], i = S(e), o = i.getDay() === 0 ? 7 : i.getDay(), r = M(e), l = r.getDay() === 0 ? 7 : r.getDay(), d = s === 1 ? 7 : s - 1, u = [], b = [];
  {
    let m = (7 - s + o) % 7, v = k(i);
    for (; m > 0; )
      u.push(v), v = k(v), m -= 1;
    u.reverse();
    let f = (7 - l + d) % 7, p = y(r);
    for (; f > 0; )
      b.push(p), p = y(p), f -= 1;
  }
  let h = i;
  for (; h.getMonth() === e.getMonth(); )
    a.push(h), h = y(h);
  return [...u, ...a, ...b];
}
function S(e) {
  return g(new Date(`${String(P(e)).padStart(4, "0")}-${String(W(e)).padStart(2, "0")}-01`));
}
function D(e) {
  if (e instanceof Date)
    return `${e.getFullYear()}-${String(e.getMonth() + 1).padStart(2, "0")}-${String(e.getDate()).padStart(2, "0")}`;
}
function M(e) {
  const t = S(e);
  return t.setMonth(t.getMonth() + 1), t.setDate(t.getDate() - 1), t;
}
function W(e) {
  return e.getMonth() + 1;
}
function j(e) {
  return new Array(12).fill(void 0).map((t, s) => {
    const a = g(new Date(`2006-${String(s + 1).padStart(2, "0")}-01`));
    return Intl.DateTimeFormat(e, {
      month: "long"
    }).format(a);
  });
}
function y(e) {
  return w(e, 1);
}
function B(e) {
  const t = new Date(e);
  return t.setDate(1), t.setMonth(t.getMonth() + 1), t;
}
function O(e) {
  const t = new Date(e);
  return t.setFullYear(t.getFullYear() + 1), t;
}
function k(e) {
  return C(e, 1);
}
function T(e) {
  const t = new Date(e);
  return t.setDate(1), t.setMonth(t.getMonth() - 1), t;
}
function F(e) {
  const t = new Date(e);
  return t.setFullYear(t.getFullYear() - 1), t;
}
function U(e, t) {
  return new Array(7).fill(void 0).map((s, a) => (e + a) % 7 + 1).map((s) => {
    const a = g(new Date(`2006-01-0${s}`));
    return [
      Intl.DateTimeFormat(t, {
        weekday: "short"
      }).format(a).slice(0, 3),
      Intl.DateTimeFormat(t, {
        weekday: "long"
      }).format(a)
    ];
  });
}
function P(e) {
  return e.getFullYear();
}
function A(e, t) {
  if (!e || !t || !t.from || !t.to)
    return !1;
  const s = t.from < t.to ? t.from : t.to, a = t.from < t.to ? t.to : t.from;
  return e >= s && e <= a;
}
function c(e, t) {
  return !e || !t ? !1 : e.getFullYear() === t.getFullYear() && e.getMonth() === t.getMonth() && e.getDate() === t.getDate();
}
function g(e) {
  const t = new Date(e);
  return t.setMinutes(t.getMinutes() + t.getTimezoneOffset()), t;
}
function C(e, t) {
  const s = new Date(e);
  return s.setDate(s.getDate() - t), s;
}
const z = () => ".visually-hidden.sc-wc-datepicker{position:absolute;overflow:hidden;width:1px;height:1px;white-space:nowrap;clip:rect(0 0 0 0);clip-path:inset(50%)}", q = {
  clearButton: "Clear value",
  monthSelect: "Select month",
  nextMonthButton: "Next month",
  nextYearButton: "Next year",
  picker: "Choose date",
  previousMonthButton: "Previous month",
  previousYearButton: "Previous year",
  todayButton: "Show today",
  yearSelect: "Select year"
}, G = class {
  constructor(e) {
    H(this, e), this.selectDate = N(this, "selectDate", 7), this.changeMonth = N(this, "changeMonth", 7), this.disabled = !1, this.disableDate = () => !1, this.elementClassName = "wc-datepicker", this.firstDayOfWeek = 0, this.labels = q, this.locale = (navigator == null ? void 0 : navigator.language) || "en-US", this.showClearButton = !1, this.showMonthStepper = !0, this.showTodayButton = !1, this.showYearStepper = !1, this.startDate = D(new Date()), this.maxSearchDays = 365, this.goToRangeStartOnSelect = !0, this.init = () => {
      this.currentDate = this.startDate ? g(new Date(this.startDate)) : new Date(), this.updateWeekdays();
    }, this.getAvailableDate = (t, s) => {
      let a, i = !1;
      switch (s) {
        case "previousDay":
          a = k(t);
          break;
        case "nextDay":
          a = y(t);
          break;
        case "previousSameWeekDay":
          a = C(t, 7);
          break;
        case "nextSameWeekDay":
          a = w(t, 7);
          break;
        case "firstOfMonth":
          a = S(t);
          break;
        case "lastOfMonth":
          a = M(t);
          break;
        case "previousMonth":
          a = T(t);
          break;
        case "nextMonth":
          a = B(t);
          break;
        case "previousYear":
          a = F(t);
          break;
        case "nextYear":
          a = O(t);
          break;
      }
      for (; this.disableDate(a) && !i; ) {
        switch (s) {
          case "previousDay":
          case "lastOfMonth":
            a = k(a);
            break;
          case "nextDay":
          case "firstOfMonth":
          case "previousMonth":
          case "nextMonth":
          case "previousYear":
          case "nextYear":
            a = y(a);
            break;
          case "previousSameWeekDay":
            a = C(a, 7);
            break;
          case "nextSameWeekDay":
            a = w(a, 7);
            break;
        }
        switch (s) {
          case "firstOfMonth":
          case "lastOfMonth":
          case "previousYear":
          case "nextYear":
            i = a.getMonth() !== t.getMonth();
            break;
          case "previousMonth":
            i = a.getMonth() !== t.getMonth() - 1;
            break;
          case "nextMonth":
            i = a.getMonth() !== t.getMonth() + 1;
            break;
          default:
            i = !A(a, {
              from: C(t, this.maxSearchDays),
              to: w(t, this.maxSearchDays)
            });
            break;
        }
      }
      return i ? t : a;
    }, this.nextMonth = () => {
      this.updateCurrentDate(B(this.currentDate));
    }, this.nextYear = () => {
      this.updateCurrentDate(O(this.currentDate));
    }, this.previousMonth = () => {
      this.updateCurrentDate(T(this.currentDate));
    }, this.previousYear = () => {
      this.updateCurrentDate(F(this.currentDate));
    }, this.showToday = () => {
      this.updateCurrentDate(new Date());
    }, this.clear = () => {
      this.value = void 0, this.selectDate.emit(void 0);
    }, this.onClick = (t) => {
      if (this.disabled)
        return;
      const s = t.target.closest("[data-date]");
      if (!Boolean(s))
        return;
      const a = g(new Date(s.dataset.date));
      this.updateCurrentDate(a), this.onSelectDate(a);
    }, this.onMonthSelect = (t) => {
      const s = +t.target.value - 1, a = this.currentDate.getDate(), i = new Date(this.currentDate.getFullYear(), s, 1), o = M(i).getDate(), r = Math.min(a, o), l = new Date(this.currentDate.getFullYear(), s, r);
      this.updateCurrentDate(l);
    }, this.onYearSelect = (t) => {
      let s = +t.target.value;
      const a = t.target;
      isNaN(s) ? (s = new Date().getFullYear(), a.value = String(s)) : s < 0 ? (s = 0, a.value = String(s)) : s > 9999 && (s = 9999, a.value = String(s));
      const i = this.currentDate.getDate(), o = this.currentDate.getMonth(), r = new Date();
      r.setFullYear(s, o, 1);
      const l = M(r).getDate(), d = Math.min(i, l), u = new Date();
      u.setFullYear(s, o, d), this.updateCurrentDate(u);
    }, this.onKeyDown = (t) => {
      this.disabled || (t.code === "ArrowLeft" ? (t.preventDefault(), this.updateCurrentDate(this.getAvailableDate(this.currentDate, "previousDay"), !0)) : t.code === "ArrowRight" ? (t.preventDefault(), this.updateCurrentDate(this.getAvailableDate(this.currentDate, "nextDay"), !0)) : t.code === "ArrowUp" ? (t.preventDefault(), this.updateCurrentDate(this.getAvailableDate(this.currentDate, "previousSameWeekDay"), !0)) : t.code === "ArrowDown" ? (t.preventDefault(), this.updateCurrentDate(this.getAvailableDate(this.currentDate, "nextSameWeekDay"), !0)) : t.code === "PageUp" ? (t.preventDefault(), t.shiftKey ? this.updateCurrentDate(this.getAvailableDate(this.currentDate, "previousYear"), !0) : this.updateCurrentDate(this.getAvailableDate(this.currentDate, "previousMonth"), !0)) : t.code === "PageDown" ? (t.preventDefault(), t.shiftKey ? this.updateCurrentDate(this.getAvailableDate(this.currentDate, "nextYear"), !0) : this.updateCurrentDate(this.getAvailableDate(this.currentDate, "nextMonth"), !0)) : t.code === "Home" ? (t.preventDefault(), this.updateCurrentDate(this.getAvailableDate(this.currentDate, "firstOfMonth"), !0)) : t.code === "End" ? (t.preventDefault(), this.updateCurrentDate(this.getAvailableDate(this.currentDate, "lastOfMonth"), !0)) : (t.code === "Space" || t.code === "Enter") && (t.preventDefault(), this.onSelectDate(this.currentDate)));
    }, this.onMouseEnter = (t) => {
      if (this.disabled)
        return;
      const s = g(new Date(t.target.closest("td").dataset.date));
      this.hoveredDate = s;
    }, this.onMouseLeave = () => {
      this.hoveredDate = void 0;
    };
  }
  componentWillLoad() {
    this.init();
  }
  watchFirstDayOfWeek() {
    this.updateWeekdays();
  }
  watchLocale() {
    Boolean(this.locale) || (this.locale = (navigator == null ? void 0 : navigator.language) || "en-US"), this.updateWeekdays();
  }
  watchRange() {
    this.value = void 0, this.selectDate.emit(void 0);
  }
  watchStartDate() {
    this.currentDate = this.startDate ? g(new Date(this.startDate)) : new Date();
  }
  watchValue() {
    !Boolean(this.value) || (Array.isArray(this.value) ? this.currentDate = this.value.length > 1 && !this.goToRangeStartOnSelect ? this.value[1] : this.value[0] : this.value instanceof Date && (this.currentDate = this.value));
  }
  componentDidRender() {
    this.moveFocusAfterMonthChanged && (this.focusDate(this.currentDate), this.moveFocusAfterMonthChanged = !1);
  }
  updateWeekdays() {
    this.weekdays = U(this.firstDayOfWeek === 0 ? 7 : this.firstDayOfWeek, this.locale);
  }
  getClassName(e) {
    return Boolean(e) ? `${this.elementClassName}__${e}` : this.elementClassName;
  }
  getCalendarRows() {
    const e = K(this.currentDate, !0, this.firstDayOfWeek === 0 ? 7 : this.firstDayOfWeek), t = [];
    for (let s = 0; s < e.length; s += 7) {
      const a = e.slice(s, s + 7);
      t.push(a);
    }
    return t;
  }
  getTitle() {
    if (!!Boolean(this.value))
      if (this.isRangeValue(this.value)) {
        const e = Intl.DateTimeFormat(this.locale, {
          day: "numeric",
          month: "long",
          year: "numeric"
        }).format(this.value[0]), t = this.value[1] ? Intl.DateTimeFormat(this.locale, {
          day: "numeric",
          month: "long",
          year: "numeric"
        }).format(this.value[1]) : void 0;
        return Boolean(t) ? `${e} - ${t}` : e;
      } else
        return Intl.DateTimeFormat(this.locale, {
          day: "numeric",
          month: "long",
          year: "numeric"
        }).format(this.value);
  }
  focusDate(e) {
    var t;
    (t = this.el.querySelector(`[data-date="${D(e)}"]`)) === null || t === void 0 || t.focus();
  }
  updateCurrentDate(e, t) {
    const s = e.getMonth(), a = e.getFullYear();
    if (a > 9999 || a < 0)
      return;
    (s !== this.currentDate.getMonth() || a !== this.currentDate.getFullYear()) && (this.changeMonth.emit({
      month: W(e),
      year: P(e),
      day: e.getDate()
    }), t && (this.moveFocusAfterMonthChanged = !0)), this.currentDate = e, t && this.focusDate(this.currentDate);
  }
  onSelectDate(e) {
    var t, s;
    if (!this.disableDate(e))
      if (this.isRangeValue(this.value)) {
        const a = ((t = this.value) === null || t === void 0 ? void 0 : t[0]) === void 0 || this.value.length === 2 ? [e] : [this.value[0], e];
        a.length === 2 && a[0] > a[1] && a.reverse();
        const i = a[1] === void 0 ? [D(a[0])] : [D(a[0]), D(a[1])];
        this.value = a, this.selectDate.emit(i);
      } else {
        if (((s = this.value) === null || s === void 0 ? void 0 : s.getTime()) === e.getTime())
          return;
        this.value = e, this.selectDate.emit(D(e));
      }
  }
  isRangeValue(e) {
    return this.range;
  }
  render() {
    const e = this.showTodayButton || this.showClearButton;
    return n(V, null, n("div", { "aria-disabled": String(this.disabled), "aria-label": this.labels.picker, class: {
      [this.getClassName()]: !0,
      [`${this.getClassName()}--disabled`]: this.disabled
    }, role: "group" }, n("div", { class: this.getClassName("header") }, n("span", { "aria-atomic": "true", "aria-live": "polite", class: "visually-hidden" }, this.getTitle()), this.showYearStepper && n("button", { "aria-label": this.labels.previousYearButton, class: this.getClassName("previous-year-button"), disabled: this.disabled, innerHTML: this.previousYearButtonContent || void 0, onClick: this.previousYear, type: "button" }, n("svg", { fill: "none", height: "24", "stroke-linecap": "round", "stroke-linejoin": "round", "stroke-width": "2", stroke: "currentColor", viewBox: "0 0 24 24", width: "24" }, n("polyline", { points: "11 17 6 12 11 7" }), n("polyline", { points: "18 17 13 12 18 7" }))), this.showMonthStepper && n("button", { "aria-label": this.labels.previousMonthButton, class: this.getClassName("previous-month-button"), disabled: this.disabled, innerHTML: this.previousMonthButtonContent || void 0, onClick: this.previousMonth, type: "button" }, n("svg", { fill: "none", height: "24", "stroke-linecap": "round", "stroke-linejoin": "round", "stroke-width": "2", stroke: "currentColor", viewBox: "0 0 24 24", width: "24" }, n("polyline", { points: "15 18 9 12 15 6" }))), n("span", { class: this.getClassName("current-month") }, n("select", { title: this.labels.monthSelect, "aria-label": this.labels.monthSelect, class: this.getClassName("month-select"), disabled: this.disabled, name: "month", onChange: this.onMonthSelect }, j(this.locale).map((t, s) => n("option", { key: t, selected: this.currentDate.getMonth() === s, value: s + 1 }, t))), n("input", { title: this.labels.yearSelect, "aria-label": this.labels.yearSelect, class: this.getClassName("year-select"), disabled: this.disabled, max: 9999, maxLength: 4, min: 1, name: "year", onChange: this.onYearSelect, type: "number", value: this.currentDate.getFullYear() })), this.showMonthStepper && n("button", { "aria-label": this.labels.nextMonthButton, class: this.getClassName("next-month-button"), disabled: this.disabled, innerHTML: this.nextMonthButtonContent || void 0, onClick: this.nextMonth, type: "button" }, n("svg", { fill: "none", height: "24", "stroke-linecap": "round", "stroke-linejoin": "round", "stroke-width": "2", stroke: "currentColor", viewBox: "0 0 24 24", width: "24" }, n("polyline", { points: "9 18 15 12 9 6" }))), this.showYearStepper && n("button", { "aria-label": this.labels.nextYearButton, class: this.getClassName("next-year-button"), disabled: this.disabled, innerHTML: this.nextYearButtonContent || void 0, onClick: this.nextYear, type: "button" }, n("svg", { fill: "none", height: "24", "stroke-linecap": "round", "stroke-linejoin": "round", "stroke-width": "2", stroke: "currentColor", viewBox: "0 0 24 24", width: "24" }, n("polyline", { points: "13 17 18 12 13 7" }), n("polyline", { points: "6 17 11 12 6 7" })))), n("div", { class: this.getClassName("body") }, n("table", { class: this.getClassName("calendar"), onKeyDown: this.onKeyDown, role: "grid" }, n("thead", { class: this.getClassName("calendar-header") }, n("tr", { class: this.getClassName("weekday-row") }, this.weekdays.map((t) => n("th", { "aria-label": t[1], abbr: t[1], class: this.getClassName("weekday"), key: t[0], scope: "col" }, n("span", null, t[0]))))), n("tbody", null, this.getCalendarRows().map((t) => {
      const s = `row-${t[0].getMonth()}-${t[0].getDate()}`;
      return n("tr", { class: this.getClassName("calendar-row"), key: s }, t.map((a) => {
        var i, o, r, l, d;
        const u = c(a, this.currentDate), b = a.getMonth() !== this.currentDate.getMonth(), h = Array.isArray(this.value) ? c(a, this.value[0]) || c(a, this.value[1]) : c(a, this.value), x = this.isRangeValue ? A(a, {
          from: (i = this.value) === null || i === void 0 ? void 0 : i[0],
          to: ((o = this.value) === null || o === void 0 ? void 0 : o[1]) || this.hoveredDate || this.currentDate
        }) : !1, m = Boolean((r = this.value) === null || r === void 0 ? void 0 : r[0]) ? [
          (l = this.value) === null || l === void 0 ? void 0 : l[0],
          ((d = this.value) === null || d === void 0 ? void 0 : d[1]) || this.hoveredDate
        ].sort((_, I) => _ - I) : [], v = this.range && c(m[0], a), Y = this.range && c(m[1], a), f = c(a, new Date()), p = this.disableDate(a), $ = `cell-${a.getMonth()}-${a.getDate()}`, L = {
          [this.getClassName("date")]: !0,
          [this.getClassName("date--current")]: u,
          [this.getClassName("date--disabled")]: p,
          [this.getClassName("date--overflowing")]: b,
          [this.getClassName("date--today")]: f,
          [this.getClassName("date--selected")]: h,
          [this.getClassName("date--in-range")]: x,
          [this.getClassName("date--start")]: v,
          [this.getClassName("date--end")]: Y
        }, R = h ? "strong" : f ? "em" : "span";
        return n("td", { "aria-disabled": String(p), "aria-selected": h ? "true" : void 0, "aria-current": f ? "date" : h ? "true" : void 0, class: L, "data-date": D(a), key: $, onClick: this.onClick, onMouseEnter: this.onMouseEnter, onMouseLeave: this.onMouseLeave, role: "gridcell", tabIndex: c(a, this.currentDate) && !this.disabled ? 0 : -1 }, n(R, { "aria-hidden": "true" }, a.getDate()), n("span", { class: "visually-hidden" }, Intl.DateTimeFormat(this.locale, {
          day: "numeric",
          month: "long",
          year: "numeric"
        }).format(a)));
      }));
    })))), e && n("div", { class: this.getClassName("footer") }, this.showTodayButton && n("button", { class: this.getClassName("today-button"), disabled: this.disabled, innerHTML: this.todayButtonContent || void 0, onClick: this.showToday, type: "button" }, this.labels.todayButton), this.showClearButton && n("button", { class: this.getClassName("clear-button"), disabled: this.disabled, innerHTML: this.clearButtonContent || void 0, onClick: this.clear, type: "button" }, this.labels.clearButton))));
  }
  get el() {
    return E(this);
  }
  static get watchers() {
    return {
      firstDayOfWeek: [{
        watchFirstDayOfWeek: 0
      }],
      locale: [{
        watchLocale: 0
      }],
      range: [{
        watchRange: 0
      }],
      startDate: [{
        watchStartDate: 0
      }],
      value: [{
        watchValue: 0
      }]
    };
  }
};
G.style = z();
export {
  G as wc_datepicker
};
