import { defineComponent as E, ref as h, watch as L, computed as O, createVNode as r, resolveComponent as C, createTextVNode as x } from "vue";
import t from "dayjs";
const N = {
  modelValue: {
    type: Date,
    default: /* @__PURE__ */ new Date()
  },
  readOnly: {
    type: Boolean,
    default: !1
  }
};
const V = "g";
function v(a, s, n) {
  let o = a;
  return s && (o += `__${s}`), n && (o += `--${n}`), o;
}
function B(a, s = !1) {
  const n = s ? `.${V}-${a}` : `${V}-${a}`;
  return {
    b: () => v(n),
    e: (u) => u ? v(n, u) : "",
    m: (u) => u ? v(n, "", u) : "",
    em: (u, m) => u && m ? v(n, u, m) : ""
  };
}
const I = "G", f = /* @__PURE__ */ E({
  name: `${I}Calendar`,
  props: N,
  emits: ["change", "update:modelValue"],
  setup(a, {
    emit: s,
    slots: n
  }) {
    const o = B("calendar"), d = h(a.modelValue ? t(a.modelValue).format("YYYY-MM-DD") : t().format("YYYY-MM-DD")), l = h(t().format("YYYY-MM")), p = h([]), u = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"], m = (e) => {
      const Y = t(e).startOf("month").day(), M = t(t(e).subtract(7 - Y, "days")).format("YYYY-MM-DD");
      p.value = [...Array(42)].map((i, c) => c).reduce((i, c) => {
        const D = t(M).add(c + 1, "days").format("YYYY-MM-DD"), w = D.split("-"), A = t(D).day();
        return i.push({
          index: c + 1,
          date: D,
          day: w[w.length - 1],
          week: u[A]
        }), i;
      }, []);
    };
    m(l.value);
    const y = (e) => {
      d.value = t(e).format("YYYY-MM-DD"), e.indexOf(l.value) === -1 && (l.value = t(e).format("YYYY-MM"), m(l.value)), s("update:modelValue", new Date(d.value)), s("change", d.value);
    }, g = (e) => {
      let Y = "";
      e === "nextMonth" && (Y = t(l.value).add(1, "month").format("YYYY-MM")), e === "lastMonth" && (Y = t(l.value).subtract(1, "month").format("YYYY-MM")), y(Y);
    };
    L(() => a.modelValue, () => {
      d.value = t(a.modelValue).format("YYYY-MM-DD"), d.value.indexOf(l.value) === -1 && (l.value = t(a.modelValue).format("YYYY-MM"), m(l.value));
    });
    const b = O(() => p.value.map((e) => {
      const Y = e.date.includes(l.value), M = d.value === e.date, i = {
        "current-month": Y,
        "current-date": M,
        [o.em("day-box", "day")]: !0
      }, c = {
        isSelected: M,
        date: e.date,
        day: parseInt(e.day)
      };
      return r("div", {
        onClick: () => {
          y(c.date);
        },
        class: i
      }, [n.dateCell ? n.dateCell(c) : c.day]);
    })), k = u.map((e) => r("div", {
      class: o.em("week", "item")
    }, [e])), $ = () => r("div", {
      class: o.e("header")
    }, [r("div", null, [l.value]), r("div", null, [r(C("g-button"), {
      type: "primary",
      plain: !0,
      onClick: () => {
        g("lastMonth");
      }
    }, {
      default: () => [x("上个月")]
    }), r(C("g-button"), {
      type: "primary",
      plain: !0,
      onClick: () => {
        y(t().format("YYYY-MM-DD"));
      }
    }, {
      default: () => [x("今天")]
    }), r(C("g-button"), {
      type: "primary",
      plain: !0,
      onClick: () => {
        g("nextMonth");
      }
    }, {
      default: () => [x("下个月")]
    })])]);
    return () => r("div", {
      class: o.b()
    }, [n.header ? n.header(d.value) : $(), r("div", {
      class: o.e("week")
    }, [k]), r("div", {
      class: o.e("day-box")
    }, [b.value])]);
  }
});
f.install = function(a, s) {
  a.component(s || f.name, f);
};
const j = {
  title: "Calendar 日历",
  category: "高级组件",
  status: "100%",
  install(a) {
    a.component(f.name, f);
  }
};
export {
  f as Calendar,
  j as default
};
