import { computed, inject, ref, watch, createVNode, Fragment } from "vue";
import "./styles/date-picker-panel.scss";
import { useProxiedModel } from "../../composables/proxied-model/index.mjs";
import { DateRangeKey } from "./date-range-picker-panel.mjs";
import { createDate } from "../../composables/date/date.mjs";
import { wrapInArray, createRange } from "../../utils/dist/index.mjs";
import { Card } from "../card/card.mjs";
import { Button } from "../button/button.mjs";
import { defineComponent } from "../../utils/define-component.mjs";
const ROWS_COUNT = 6;
const COLS_COUNT = 7;
const CELLS_COUNT = ROWS_COUNT * COLS_COUNT;
const WEEKDAYS = ["\u65E5", "\u4E00", "\u4E8C", "\u4E09", "\u56DB", "\u4E94", "\u516D"];
const DatePickerPanel = defineComponent({
  name: "VeDatePickerPanel",
  props: {
    activeColor: {
      type: String,
      default: "primary"
    },
    firstDayOfWeek: {
      type: [String, Number],
      default: 0,
      validator: (val) => Number(val) in WEEKDAYS
    },
    format: {
      type: [String, Function],
      default: "YYYY-MM-DD"
    },
    max: String,
    min: String,
    modelValue: String,
    prevDoubleIcon: {
      type: String,
      default: "$prevDouble"
    },
    prevIcon: {
      type: String,
      default: "$prev"
    },
    nextIcon: {
      type: String,
      default: "$next"
    },
    nextDoubleIcon: {
      type: String,
      default: "$nextDouble"
    }
  },
  emits: {
    "update:modelValue": (_modelValue) => true
  },
  setup(props, {
    slots
  }) {
    const firstDayOfWeek = computed(() => Number(props.firstDayOfWeek));
    const max = computed(() => props.max ? createDate(props.max) : void 0);
    const min = computed(() => props.min ? createDate(props.min) : void 0);
    const model = useProxiedModel(props, "modelValue", props.modelValue, (v) => createDate(v), (v) => formatter(v));
    const range = inject(DateRangeKey, null);
    const selected = computed(() => {
      if (range) {
        return range.selected.value.map((v) => valueFormatter(v.startOf("day")));
      }
      return wrapInArray(valueFormatter(model.value.startOf("day")));
    });
    const weekdays = computed(() => createRange(7).map((i) => WEEKDAYS[(i + firstDayOfWeek.value) % 7]));
    const internalModel = ref(model.value.startOf("month"));
    const month = computed(() => internalModel.value.format("M"));
    watch(model, (val) => {
      internalModel.value = val.startOf("month");
    });
    function formatter(date) {
      return typeof props.format === "string" ? date.format(props.format) : props.format(date);
    }
    function valueFormatter(date) {
      return createDate(formatter(date)).valueOf();
    }
    const rows = computed(() => {
      const now = valueFormatter(createDate().startOf("day"));
      const sharedProps = {
        icon: true,
        variant: "text",
        shape: "circle",
        size: "small"
      };
      const cells = [];
      const weekDay = Number(internalModel.value.format("d"));
      const offsetDay = firstDayOfWeek.value - weekDay;
      let date = internalModel.value.add(offsetDay, "day");
      createRange(CELLS_COUNT).forEach((_) => {
        const current = date;
        date = date.add(1, "day");
        const text = formatter(current);
        const value = valueFormatter(current);
        const selectedIndex = selected.value.findIndex((v) => v === value);
        const isActive = selectedIndex > -1;
        const isToday = now == value;
        const variant = isActive ? "contained" : isToday ? "outlined" : "text";
        const color = isActive || isToday ? props.activeColor : void 0;
        function onClick() {
          if (range) {
            range.select(text);
            range.isPreview.value && range.preview(false);
          } else {
            model.value = current;
          }
        }
        cells.push({
          isActive,
          selectedIndex,
          isOtherMonth: current.format("M") !== month.value,
          text,
          value,
          props: {
            ...sharedProps,
            variant,
            color,
            text: current.format("D"),
            onClick
          }
        });
      });
      const rows2 = createRange(ROWS_COUNT).map((_, index) => {
        return cells.slice(index * COLS_COUNT, (index + 1) * COLS_COUNT);
      });
      rows2.unshift(weekdays.value.map((text) => ({
        props: {
          ...sharedProps,
          text,
          disabled: true
        }
      })));
      return rows2;
    });
    const controls = computed(() => {
      const sharedProps = {
        variant: "text",
        shape: "circle",
        size: "small"
      };
      const hasPrev = min.value ? internalModel.value.valueOf() > min.value.valueOf() : true;
      const hasNext = max.value ? internalModel.value.valueOf() < max.value.valueOf() : true;
      return {
        prevDouble: {
          ...sharedProps,
          icon: props.prevDoubleIcon,
          onClick: prevYear,
          disabled: !hasPrev
        },
        prev: {
          ...sharedProps,
          icon: props.prevIcon,
          onClick: prevMonth,
          disabled: !hasPrev
        },
        date: {
          ...sharedProps,
          shape: "rounded-sm",
          style: {
            fontSize: "14px"
          },
          text: internalModel.value.format("YYYY-MM")
        },
        next: {
          ...sharedProps,
          icon: props.nextIcon,
          onClick: nextMonth,
          disabled: !hasNext
        },
        nextDouble: {
          ...sharedProps,
          icon: props.nextDoubleIcon,
          onClick: nextYear,
          disabled: !hasNext
        }
      };
    });
    function prevYear() {
      internalModel.value = internalModel.value.subtract(1, "year");
    }
    function nextYear() {
      internalModel.value = internalModel.value.add(1, "year");
    }
    function prevMonth() {
      internalModel.value = internalModel.value.subtract(1, "month");
    }
    function nextMonth() {
      internalModel.value = internalModel.value.add(1, "month");
    }
    function onMousemove(text) {
      if (!range)
        return;
      if (range.selected.value.length === 1) {
        !range.isPreview.value && range.preview(true);
      }
      if (range.isPreview.value) {
        range.select(text);
      }
    }
    return () => {
      return createVNode(Card, {
        "class": "ve-date-picker-panel",
        "divided": true,
        "onMousedown": (e) => e.preventDefault()
      }, {
        ...slots,
        header: () => createVNode(Fragment, null, [createVNode(Button, controls.value.prevDouble, null), createVNode(Button, controls.value.prev, null), createVNode("div", {
          "class": "ve-date-picker-panel__date"
        }, [createVNode(Button, controls.value.date, null)]), createVNode(Button, controls.value.next, null), createVNode(Button, controls.value.nextDouble, null)]),
        text: () => createVNode(Fragment, null, [rows.value.map((cols) => createVNode("div", {
          "class": "ve-date-picker-panel__row"
        }, [cols.map((col) => createVNode("div", {
          "key": col.value,
          "class": ["ve-date-picker-panel__cell", {
            "ve-date-picker-panel__cell--active": col.isActive,
            "ve-date-picker-panel__cell--other-month": col.isOtherMonth,
            "ve-date-picker-panel__cell--range-in": col.value && (range == null ? void 0 : range.inRange(col.value)),
            "ve-date-picker-panel__cell--range-start": col.selectedIndex === 0,
            "ve-date-picker-panel__cell--range-end": col.selectedIndex === 1
          }],
          "onMousemove": () => onMousemove(col.text)
        }, [createVNode(Button, col.props, null)]))]))])
      });
    };
  }
});
export {
  DatePickerPanel
};
//# sourceMappingURL=date-picker-panel.mjs.map
