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