import '@das-fed/upf-ui/packages/components/calendar/style.css';
import { withInstall as ee } from "@das-fed/upf-utils/with-install/index";
import { defineComponent as Q, ref as W, computed as g, watch as ae, nextTick as I, openBlock as u, createElementBlock as f, normalizeClass as x, Fragment as Z, renderList as j, createElementVNode as _, toDisplayString as P, renderSlot as D, createCommentVNode as C, normalizeStyle as te, onMounted as ne, onBeforeUnmount as re, createVNode as se, createSlots as U, withCtx as R, normalizeProps as V, mergeProps as F, guardReactiveProps as J, createBlock as oe } from "vue";
import { t as A, currentLang as K, setI18nRule as de } from "@das-fed/upf-web/packages/i18n/index";
import N from "dayjs";
import { SizeWatcher as le } from "@das-fed/upf-utils/size-watcher";
const he = (a, i) => {
  const d = a.endOf("month"), h = i.startOf("month"), n = d.isSame(h, "week") ? h.add(1, "week") : h;
  return [
    [a, d],
    [n.startOf("week"), i]
  ];
}, ce = (a, i) => {
  const d = a.endOf("month"), h = a.add(1, "month").startOf("month"), o = d.isSame(
    h,
    "week"
  ) ? h.add(1, "week") : h, n = o.endOf("month"), p = i.startOf("month"), M = n.isSame(p, "week") ? p.add(1, "week") : p;
  return [
    [a, d],
    [o.startOf("week"), n],
    [M.startOf("week"), i]
  ];
}, q = (a, i) => {
  const d = a.startOf("week"), h = i.endOf("week"), o = d.get("month"), n = h.get("month");
  return o === n ? [[d, h]] : (o + 1) % 12 === n ? he(d, h) : o + 2 === n || (o + 1) % 11 === n ? ce(d, h) : [];
}, S = (a) => Array.from(Array.from({ length: a }).keys()), ie = (a, i) => {
  const d = a.subtract(1, "month").endOf("month").date();
  return S(i).map((h, o) => d - (i - o - 1));
}, ue = (a) => {
  const i = a.daysInMonth();
  return S(i).map((d, h) => h + 1);
}, me = (a) => S(a.length / 7).map((i) => {
  const d = i * 7;
  return a.slice(d, d + 7);
}), fe = { key: 0 }, pe = { class: "das-calendar-header-box" }, ye = { class: "das-calendar-header-week-line" }, ke = ["onClick"], ge = {
  name: "calendar-table"
}, G = /* @__PURE__ */ Q({
  ...ge,
  props: {
    selectedDay: {},
    date: {},
    firstDayOfWeek: {},
    range: {},
    hideHeader: { type: Boolean },
    parent: {},
    calendartType: {},
    highlightWeek: { type: Boolean },
    highlightDay: { type: Boolean },
    showZhou: { type: Boolean },
    cellHeight: {}
  },
  emits: ["pick"],
  setup(a, { expose: i, emit: d }) {
    const h = d, o = N(), n = a, p = W("128px"), M = W(), Y = 60, $ = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"], O = g(() => !!n.range && !!n.range.length), L = g(() => {
      const e = n.firstDayOfWeek, t = n.showZhou ? "das-calendar-weeks-zhou-" : "das-calendar-weeks-";
      return e === 0 ? $.map((s) => A(`${t}${s}`)) : $.slice(e).concat($.slice(0, e)).map((s) => A(`${t}${s}`));
    }), v = g(() => {
      let e = [];
      if (O.value) {
        const [t, s] = n.range;
        let c = [];
        t.isSame(s, "month") ? c = S(s.date() - t.date() + 1).map((z) => ({
          text: t.date() + z,
          type: "current"
        })) : c = S(t.endOf("month").date() - t.date() + 1).map((z) => ({
          text: t.date() + z,
          type: "current"
        }));
        let k = c.length % 7;
        k = k === 0 ? 0 : 7 - k;
        const w = S(k).map((z, T) => ({
          text: T + 1,
          type: "next"
        }));
        e = c.concat(w);
      } else {
        const t = n.date.startOf("month").day(), s = ie(
          n.date,
          (t - n.firstDayOfWeek + 7) % 7
        ).map((z) => ({
          text: z,
          type: "prev"
        })), c = ue(n.date).map((z) => ({
          text: z,
          type: "current"
        }));
        e = [...s, ...c];
        const k = 7 - (e.length % 7 || 7), w = S(k).map((z, T) => ({
          text: T + 1,
          type: "next"
        }));
        if (e = e.concat(w), n.calendartType == "simple" && e.length / 7 < 6) {
          const z = w.length ? w[w.length - 1].text : 0, T = S(7).map(($e, X) => ({
            text: z + X + 1,
            type: "next"
          }));
          e = e.concat(T);
        }
      }
      return me(e);
    }), H = () => {
      var s;
      const e = (s = n.parent) == null ? void 0 : s.offsetHeight;
      let t = Number(e) - Y - 1;
      p.value = t / v.value.length - 1 + "px";
    };
    ae(
      () => v.value,
      (e) => {
        if (n.cellHeight) {
          p.value = `${n.cellHeight}px`;
          return;
        }
        e && e.length ? I(() => {
          H();
        }) : p.value = "128px";
      },
      { immediate: !0 }
    );
    const b = () => {
      if (n.cellHeight) {
        p.value = `${n.cellHeight}px`;
        return;
      }
      I(() => {
        H();
      });
    }, y = (e, t) => {
      switch (t) {
        case "prev":
          return n.date.startOf("month").subtract(1, "month").date(e);
        case "next":
          return n.date.startOf("month").add(1, "month").date(e);
        case "current":
          return n.date.date(e);
      }
    }, B = ({ text: e, type: t }) => {
      const s = [t], c = y(e, t);
      return c.isSame(n.selectedDay, "day") && s.push("is-selected"), c.isSame(o, "day") && s.push("is-today"), s;
    }, E = ({ text: e, type: t }) => {
      const s = y(e, t);
      h("pick", s);
    }, r = (e) => {
      const t = v.value[0];
      if (t) {
        const s = t[e];
        if (s) {
          const { text: c, type: k } = s;
          return y(c, k).format("MM/DD");
        }
      }
      return "";
    }, m = (e) => {
      const t = v.value[0];
      if (t) {
        const s = t[e];
        if (s) {
          const { text: c, type: k } = s, w = y(c, k);
          return {
            isSelected: w.isSame(n.selectedDay),
            type: `${k}-month`,
            day: w.format("YYYY-MM-DD"),
            date: w.toDate()
          };
        }
      }
      return {
        isSelected: null,
        type: null,
        day: null,
        date: null
      };
    }, l = ({ text: e, type: t }) => {
      const s = y(e, t);
      return {
        isSelected: s.isSame(n.selectedDay),
        type: `${t}-month`,
        day: s.format("YYYY-MM-DD"),
        date: s.toDate()
      };
    };
    return i({
      getHeight: b
    }), (e, t) => v.value.length ? (u(), f(
      "table",
      {
        key: 0,
        class: x(["das-calendar-table", { "is-range": O.value, "is-week": a.calendartType == "week", simple: a.calendartType == "simple" }]),
        cellspacing: "0",
        cellpadding: "0",
        ref_key: "tableRef",
        ref: M
      },
      [
        a.hideHeader ? C("v-if", !0) : (u(), f("thead", fe, [
          (u(!0), f(
            Z,
            null,
            j(L.value, (s, c) => (u(), f("th", {
              class: "das-calendar-table-header",
              key: s
            }, [
              _("div", pe, [
                _("div", ye, [
                  _(
                    "div",
                    {
                      class: x(["das-calendar-header-week", { highlight: a.highlightWeek }])
                    },
                    P(s),
                    3
                    /* TEXT, CLASS */
                  ),
                  a.calendartType == "week" ? D(e.$slots, "week-type", {
                    key: 0,
                    data: m(c)
                  }) : C("v-if", !0)
                ]),
                a.calendartType == "week" ? (u(), f(
                  "div",
                  {
                    key: 0,
                    class: x(["das-calendar-header-day", { highlight: a.highlightDay }])
                  },
                  P(r(c)),
                  3
                  /* TEXT, CLASS */
                )) : C("v-if", !0)
              ])
            ]))),
            128
            /* KEYED_FRAGMENT */
          ))
        ])),
        _("tbody", null, [
          (u(!0), f(
            Z,
            null,
            j(v.value, (s, c) => (u(), f(
              "tr",
              {
                key: c,
                class: x({
                  "das-calendar-table-row": !0,
                  "hide-border": c === 0 && a.hideHeader
                })
              },
              [
                (u(!0), f(
                  Z,
                  null,
                  j(s, (k, w) => (u(), f("td", {
                    key: w,
                    class: x(B(k)),
                    onClick: (z) => E(k)
                  }, [
                    C(" v-scrollbar "),
                    C(' :style="{ height: trHeight }" '),
                    _(
                      "div",
                      {
                        class: "das-calendar-day",
                        style: te({ height: p.value })
                      },
                      [
                        D(e.$slots, "date-cell", {
                          data: l(k)
                        })
                      ],
                      4
                      /* STYLE */
                    )
                  ], 10, ke))),
                  128
                  /* KEYED_FRAGMENT */
                ))
              ],
              2
              /* CLASS */
            ))),
            128
            /* KEYED_FRAGMENT */
          ))
        ])
      ],
      2
      /* CLASS */
    )) : C("v-if", !0);
  }
}), we = {
  key: 0,
  class: "das-calendar-body 1"
}, Ce = {
  key: 1,
  class: "das-calendar-cell-date"
}, ze = {
  key: 2,
  class: "das-calendar-cell-cont"
}, ve = {
  key: 1,
  class: "das-calendar-body 2"
}, Me = {
  key: 1,
  class: "das-calendar-cell-date"
}, De = {
  key: 2,
  class: "das-calendar-cell-cont"
}, Oe = {
  name: "test"
}, Ne = /* @__PURE__ */ Q({
  ...Oe,
  props: {
    modelValue: {},
    range: {},
    type: { default: "month" },
    isSwitchSelection: { type: Boolean, default: !0 },
    isSwitchMonth: { type: Boolean, default: !1 },
    highlightWeek: { type: Boolean, default: !1 },
    highlightDay: { type: Boolean, default: !0 },
    showZhou: { type: Boolean, default: !0 },
    firstDayOfWeek: {},
    cellHeight: { default: 0 }
  },
  emits: ["update:model-value", "itemClick"],
  setup(a, { expose: i, emit: d }) {
    const h = g(() => K.value == "zh-CN" ? 1 : (K.value == "en-US", 0)), o = a, n = d, p = W(), M = W(), Y = W(), $ = g(() => o.firstDayOfWeek || h.value), O = W(), L = N(), v = g({
      get() {
        return o.modelValue ? (O.value = y.value, y.value) : O.value;
      },
      set(r) {
        if (!r) return;
        O.value = r;
        const m = r.toDate();
        o.isSwitchMonth && n("update:model-value", m);
      }
    }), H = g(() => {
      if (o.type == "week") {
        if (o.range) return o.range;
        const m = (N(o.modelValue).day() - $.value + 7) % 7, l = 7 - (m + 1), e = [];
        return e.push(N(o.modelValue).subtract(m, "day").toDate()), e.push(N(o.modelValue).add(l, "day").toDate()), e;
      } else
        return o.range ? o.range : void 0;
    }), b = g(() => {
      if (!H.value) return [];
      const r = H.value.map((e) => N(e)), [m, l] = r;
      return o.type == "week" ? [[m, l]] : m.isAfter(l) ? [] : m.isSame(l, "month") ? q(m, l) : m.add(1, "month").month() !== l.month() ? [] : q(m, l);
    }), y = g(() => o.modelValue ? N(o.modelValue) : v.value || (b.value.length ? b.value[0][0] : L));
    g(() => y.value.subtract(1, "month").date(1)), g(() => y.value.add(1, "month").date(1)), g(() => y.value.subtract(1, "year").date(1)), g(() => y.value.add(1, "year").date(1)), g(() => {
      const r = `das-calendar-month${y.value.format("M")}`;
      return `${y.value.year()} ${A("das-calendar-year")} ${A(r)}`;
    }), ne(() => {
      Y.value = le(() => {
        var r, m;
        Array.isArray(M.value) ? (r = M.value[0]) == null || r.getHeight() : (m = M.value) == null || m.getHeight();
      }, p.value);
    }), re(() => {
      Y.value.disconnect();
    });
    const B = (r) => {
      o.isSwitchSelection && (v.value = r), n("itemClick", r.toDate());
    };
    return i({
      $calendar: p,
      setActive: (r) => {
        v.value = N(r), n("itemClick", r);
      }
    }), (r, m) => (u(), f(
      "div",
      {
        ref_key: "$calendar",
        ref: p,
        class: "das-ui-calendar"
      },
      [
        C(` <div class="das-calendar-header" >\r
      <slot name="header" :date="i18nDate">\r
        <div class="das-calendar-title" >{{ i18nDate }}</div>\r
        <div class="das-calendar-button-group" v-if="validatedRange.length === 0" >\r
          <das-button @click="selectDate('prev-month')" >  {{ t('das-calendar-prevMonth') }}</das-button>\r
          <das-button @click="selectDate('today')" >  {{ t('das-calendar-today') }}</das-button>\r
          <das-button @click="selectDate('next-month')" >  {{ t('das-calendar-nextMonth') }}</das-button>\r
        </div>\r
      </slot>\r
    </div> `),
        b.value.length === 0 ? (u(), f("div", we, [
          se(G, {
            ref_key: "$calendarTable",
            ref: M,
            date: y.value,
            firstDayOfWeek: $.value,
            parent: p.value,
            calendartType: a.type,
            highlightWeek: a.highlightWeek,
            highlightDay: a.highlightDay,
            showZhou: a.showZhou,
            cellHeight: a.cellHeight,
            "selected-day": O.value,
            onPick: B
          }, U({
            "date-cell": R((l) => [
              r.$slots.custom ? D(r.$slots, "custom", V(F({ key: 0 }, l))) : C("v-if", !0),
              a.type != "week" && !r.$slots.custom ? (u(), f("p", Ce, [
                _(
                  "span",
                  null,
                  P(Number(l.data.day.split("-").slice(2)).toString()),
                  1
                  /* TEXT */
                ),
                D(r.$slots, "date-type", V(J(l)))
              ])) : C("v-if", !0),
              r.$slots.custom ? C("v-if", !0) : (u(), f("div", ze, [
                D(r.$slots, "date-cell", V(J(l)))
              ]))
            ]),
            _: 2
            /* DYNAMIC */
          }, [
            r.$slots["week-type"] && a.type == "week" ? {
              name: "week-type",
              fn: R((l) => [
                D(r.$slots, "week-type", V(J(l)))
              ]),
              key: "0"
            } : void 0
          ]), 1032, ["date", "firstDayOfWeek", "parent", "calendartType", "highlightWeek", "highlightDay", "showZhou", "cellHeight", "selected-day"])
        ])) : (u(), f("div", ve, [
          (u(!0), f(
            Z,
            null,
            j(b.value, (l, e) => (u(), oe(G, {
              ref_for: !0,
              ref_key: "$calendarTable",
              ref: M,
              key: e,
              firstDayOfWeek: $.value,
              date: l[0],
              calendartType: a.type,
              highlightWeek: a.highlightWeek,
              highlightDay: a.highlightDay,
              showZhou: a.showZhou,
              cellHeight: a.cellHeight,
              "selected-day": O.value,
              range: l,
              parent: p.value,
              "hide-header": e !== 0,
              onPick: B
            }, U({
              "date-cell": R((t) => [
                r.$slots.custom ? D(r.$slots, "custom", F({
                  key: 0,
                  ref_for: !0
                }, t)) : C("v-if", !0),
                a.type != "week" && !r.$slots.custom ? (u(), f("p", Me, [
                  _(
                    "span",
                    null,
                    P(Number(t.data.day.split("-").slice(2)).toString()),
                    1
                    /* TEXT */
                  )
                ])) : C("v-if", !0),
                r.$slots.custom ? C("v-if", !0) : (u(), f("div", De, [
                  D(r.$slots, "date-cell", F({ ref_for: !0 }, t))
                ]))
              ]),
              _: 2
              /* DYNAMIC */
            }, [
              r.$slots["week-type"] && a.type == "week" ? {
                name: "week-type",
                fn: R((t) => [
                  D(r.$slots, "week-type", F({ ref_for: !0 }, t))
                ]),
                key: "0"
              } : void 0
            ]), 1032, ["firstDayOfWeek", "date", "calendartType", "highlightWeek", "highlightDay", "showZhou", "cellHeight", "selected-day", "range", "parent", "hide-header"]))),
            128
            /* KEYED_FRAGMENT */
          ))
        ]))
      ],
      512
      /* NEED_PATCH */
    ));
  }
}), Se = {
  周: { "zh-CN": "周", en: "Week", "zh-MO": "周", _appCode: "framework" },
  日历: { "zh-CN": "日历", en: "Calendar", "zh-MO": "日曆", _appCode: "framework" },
  "das-calendar-prevMonth": { "zh-CN": "上个月", en: "Last Month", "zh-MO": "上個月", _appCode: "framework" },
  "das-calendar-nextMonth": { "zh-CN": "下个月", en: "Next Month", "zh-MO": "下個月", _appCode: "framework" },
  "das-calendar-prevYear": { "zh-CN": "前一年", en: "Previous Year", "zh-MO": "前一年", _appCode: "framework" },
  "das-calendar-nextYear": { "zh-CN": "后一年", en: "Next Year", "zh-MO": "後一年", _appCode: "framework" },
  "das-calendar-year": { "zh-CN": "年", en: "Year", "zh-MO": "年", _appCode: "framework" },
  "das-calendar-today": { "zh-CN": "今天", en: "Today", "zh-MO": "今天", _appCode: "framework" },
  "das-calendar-weeks-sun": { "zh-CN": "日", en: "Sun", "zh-MO": "日", _appCode: "framework" },
  "das-calendar-weeks-mon": { "zh-CN": "一", en: "Mon", "zh-MO": "一", _appCode: "framework" },
  "das-calendar-weeks-tue": { "zh-CN": "二", en: "Tues", "zh-MO": "二", _appCode: "framework" },
  "das-calendar-weeks-wed": { "zh-CN": "三", en: "Wed", "zh-MO": "三", _appCode: "framework" },
  "das-calendar-weeks-thu": { "zh-CN": "四", en: "Thur", "zh-MO": "四", _appCode: "framework" },
  "das-calendar-weeks-fri": { "zh-CN": "五", en: "Fri", "zh-MO": "五", _appCode: "framework" },
  "das-calendar-weeks-sat": { "zh-CN": "六", en: "Sat", "zh-MO": "六", _appCode: "framework" },
  "das-calendar-weeks-zhou-sun": { "zh-CN": "周日", en: "Sun", "zh-MO": "周日", _appCode: "framework" },
  "das-calendar-weeks-zhou-mon": { "zh-CN": "周一", en: "Mon", "zh-MO": "週一", _appCode: "framework" },
  "das-calendar-weeks-zhou-tue": { "zh-CN": "周二", en: "Tues", "zh-MO": "週二", _appCode: "framework" },
  "das-calendar-weeks-zhou-wed": { "zh-CN": "周三", en: "Wed", "zh-MO": "週三", _appCode: "framework" },
  "das-calendar-weeks-zhou-thu": { "zh-CN": "周四", en: "Thur", "zh-MO": "週四", _appCode: "framework" },
  "das-calendar-weeks-zhou-fri": { "zh-CN": "周五", en: "Fri", "zh-MO": "週五", _appCode: "framework" },
  "das-calendar-weeks-zhou-sat": { "zh-CN": "周六", en: "Sat", "zh-MO": "週六", _appCode: "framework" },
  "das-calendar-month1": { "zh-CN": "1 月", en: "Jan", "zh-MO": "1 月", _appCode: "framework" },
  "das-calendar-month2": { "zh-CN": "2 月", en: "Feb", "zh-MO": "2 月", _appCode: "framework" },
  "das-calendar-month3": { "zh-CN": "3 月", en: "Mar", "zh-MO": "3 月", _appCode: "framework" },
  "das-calendar-month4": { "zh-CN": "4 月", en: "Apr", "zh-MO": "4 月", _appCode: "framework" },
  "das-calendar-month5": { "zh-CN": "5 月", en: "May", "zh-MO": "5 月", _appCode: "framework" },
  "das-calendar-month6": { "zh-CN": "6 月", en: "Jun", "zh-MO": "6 月", _appCode: "framework" },
  "das-calendar-month7": { "zh-CN": "7 月", en: "Jul", "zh-MO": "7 月", _appCode: "framework" },
  "das-calendar-month8": { "zh-CN": "8 月", en: "Aug", "zh-MO": "8 月", _appCode: "framework" },
  "das-calendar-month9": { "zh-CN": "9 月", en: "Sept", "zh-MO": "9 月", _appCode: "framework" },
  "das-calendar-month10": { "zh-CN": "10 月", en: "Oct", "zh-MO": "10 月", _appCode: "framework" },
  "das-calendar-month11": { "zh-CN": "11 月", en: "Nov", "zh-MO": "11 月", _appCode: "framework" },
  "das-calendar-month12": { "zh-CN": "12 月", en: "Dec", "zh-MO": "12 月", _appCode: "framework" },
  "das-calendar-months-jan": { "zh-CN": "一月", en: "Jan", "zh-MO": "一月", _appCode: "framework" },
  "das-calendar-months-feb": { "zh-CN": "二月", en: "Feb", "zh-MO": "二月", _appCode: "framework" },
  "das-calendar-months-mar": { "zh-CN": "三月", en: "Mar", "zh-MO": "三月", _appCode: "framework" },
  "das-calendar-months-apr": { "zh-CN": "四月", en: "Apr", "zh-MO": "四月", _appCode: "framework" },
  "das-calendar-months-may": { "zh-CN": "五月", en: "May", "zh-MO": "五月", _appCode: "framework" },
  "das-calendar-months-jun": { "zh-CN": "六月", en: "Jun", "zh-MO": "六月", _appCode: "framework" },
  "das-calendar-months-jul": { "zh-CN": "七月", en: "Jul", "zh-MO": "七月", _appCode: "framework" },
  "das-calendar-months-aug": { "zh-CN": "八月", en: "Aug", "zh-MO": "八月", _appCode: "framework" },
  "das-calendar-months-sep": { "zh-CN": "九月", en: "Sept", "zh-MO": "九月", _appCode: "framework" },
  "das-calendar-months-oct": { "zh-CN": "十月", en: "Oct", "zh-MO": "十月", _appCode: "framework" },
  "das-calendar-months-nov": { "zh-CN": "十一月", en: "Nov", "zh-MO": "十一月", _appCode: "framework" },
  "das-calendar-months-dec": { "zh-CN": "十二月", en: "Dec", "zh-MO": "十二月", _appCode: "framework" }
};
de(Se);
const xe = ee(Ne), Ae = { name: `calendar ${A("日历")}` };
export {
  xe as DasCalendar,
  Ae as default
};
