'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var dayjs = require('dayjs');
var isoWeek = require('dayjs/plugin/isoWeek');
var index = require('../../button/index.js');
var index$1 = require('../../svg-icon/index.js');

"use strict";
const _hoisted_1 = { class: "week-day-list" };
const _hoisted_2 = { class: "list" };
const _hoisted_3 = ["onClick"];
const _hoisted_4 = { class: "week-name" };
const _hoisted_5 = { class: "day" };
var script = /* @__PURE__ */ vue.defineComponent({
  __name: "week-day-list",
  props: {
    modelValue: { type: String, required: true, default: "" },
    prevWeekLabel: { type: String, required: false, default: "\u4E0A\u4E00\u5468" },
    nextWeekLabel: { type: String, required: false, default: "\u4E0B\u4E00\u5468" },
    weekLabels: { type: Array, required: false, default: () => ["\u5468\u4E00", "\u5468\u4E8C", "\u5468\u4E09", "\u5468\u56DB", "\u5468\u4E94", "\u5468\u516D", "\u5468\u65E5"] }
  },
  emits: ["update:modelValue", "change"],
  setup(__props, { emit: __emit }) {
    dayjs.extend(isoWeek);
    const emit = __emit;
    const props = __props;
    const activeIndexDay = vue.ref(3);
    const weekIndex = vue.ref(0);
    const list = vue.ref([]);
    const day = vue.computed(() => list.value[activeIndexDay.value]?.day);
    function generateList() {
      const labels = props.weekLabels;
      const startOf = dayjs().startOf("isoWeek").add(weekIndex.value, "week");
      const res = labels?.map((item, index) => {
        return {
          label: item,
          day: startOf.add(index, "day").format("YYYY-MM-DD")
        };
      });
      console.log(startOf.format("YYYY-MM-DD"), res);
      if (!list.value.length) {
        const today = dayjs().format("YYYY-MM-DD");
        const findIndex = res.findIndex((item) => item.day === today);
        activeIndexDay.value = findIndex || 0;
      }
      list.value = res;
    }
    function generateWeekDates(dateStr, isMondayFirst = true) {
      const targetDate = dayjs(dateStr);
      if (!targetDate.isValid()) {
        throw new Error("\u4F20\u5165\u7684\u65E5\u671F\u683C\u5F0F\u65E0\u6548\uFF0C\u8BF7\u4F7F\u7528 YYYY-MM-DD \u683C\u5F0F");
      }
      let firstDayOfWeek;
      if (isMondayFirst) {
        firstDayOfWeek = targetDate.day() === 0 ? targetDate.subtract(6, "day") : targetDate.subtract(targetDate.day() - 1, "day");
      } else {
        firstDayOfWeek = targetDate.subtract(targetDate.day(), "day");
      }
      const weekDates = [];
      for (let i = 0; i < 7; i++) {
        const currentDate = firstDayOfWeek.add(i, "day");
        weekDates.push(currentDate.format("YYYY-MM-DD"));
      }
      return weekDates;
    }
    const prev = () => {
      weekIndex.value--;
      generateList();
    };
    const next = () => {
      weekIndex.value++;
      generateList();
    };
    vue.watch(
      () => props.modelValue,
      (newValue) => {
        const weekDates = generateWeekDates(newValue);
        list.value = weekDates?.map((item, index) => {
          return {
            label: props.weekLabels[index],
            day: item
          };
        });
        const findIndex = list.value.findIndex((item) => item.day == newValue);
        activeIndexDay.value = findIndex || 0;
      }
    );
    vue.watch(day, (res) => {
      emit("update:modelValue", res);
      emit("change", res);
    });
    vue.onBeforeMount(() => {
      generateList();
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock("div", _hoisted_1, [
        vue.createVNode(vue.unref(index.MeButton), { onClick: prev }, {
          default: vue.withCtx(() => [
            vue.createVNode(vue.unref(index$1.MeSvgIcon), { name: "ri-arrow-left-s-fill" }),
            vue.createTextVNode(
              " " + vue.toDisplayString(__props.prevWeekLabel),
              1
              /* TEXT */
            )
          ]),
          _: 1
          /* STABLE */
        }),
        vue.createElementVNode("div", _hoisted_2, [
          (vue.openBlock(true), vue.createElementBlock(
            vue.Fragment,
            null,
            vue.renderList(list.value, (item, index) => {
              return vue.openBlock(), vue.createElementBlock("div", {
                key: index,
                class: vue.normalizeClass(["item", { active: index === activeIndexDay.value }]),
                onClick: ($event) => activeIndexDay.value = index
              }, [
                vue.createElementVNode(
                  "div",
                  _hoisted_4,
                  vue.toDisplayString(item.label),
                  1
                  /* TEXT */
                ),
                vue.createElementVNode(
                  "div",
                  _hoisted_5,
                  vue.toDisplayString(vue.unref(dayjs)(item.day).format("MM-DD")),
                  1
                  /* TEXT */
                )
              ], 10, _hoisted_3);
            }),
            128
            /* KEYED_FRAGMENT */
          ))
        ]),
        vue.createVNode(vue.unref(index.MeButton), { onClick: next }, {
          default: vue.withCtx(() => [
            vue.createTextVNode(
              vue.toDisplayString(__props.nextWeekLabel) + " ",
              1
              /* TEXT */
            ),
            vue.createVNode(vue.unref(index$1.MeSvgIcon), { name: "ri-arrow-right-s-fill" })
          ]),
          _: 1
          /* STABLE */
        })
      ]);
    };
  }
});

exports.default = script;
//# sourceMappingURL=week-day-list.vue2.js.map
