"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const utils = require("./utils.js");
const useExpose = require("../composables/use-expose.js");
const index = require("../picker/index.js");
const basic = require("../utils/basic.js");
const create = require("../utils/create.js");
const currentYear = (/* @__PURE__ */ new Date()).getFullYear();
const [name] = create.createNamespace("date-picker");
const datePickerProps = basic.extend({}, utils.sharedProps, {
  columnsType: {
    type: Array,
    default: () => ["year", "month", "day"]
  },
  minDate: {
    type: Date,
    default: () => new Date(currentYear - 10, 0, 1),
    validator: basic.isDate
  },
  maxDate: {
    type: Date,
    default: () => new Date(currentYear + 10, 11, 31),
    validator: basic.isDate
  }
});
var stdin_default = Vue.defineComponent({
  name,
  props: datePickerProps,
  emits: ["confirm", "cancel", "change", "update:modelValue"],
  setup(props, {
    emit,
    slots
  }) {
    const currentValues = Vue.ref(props.modelValue);
    const updatedByExternalSources = Vue.ref(false);
    const pickerRef = Vue.ref();
    const computedValues = Vue.computed(() => updatedByExternalSources.value ? props.modelValue : currentValues.value);
    const isMinYear = (year) => year === props.minDate.getFullYear();
    const isMaxYear = (year) => year === props.maxDate.getFullYear();
    const isMinMonth = (month) => month === props.minDate.getMonth() + 1;
    const isMaxMonth = (month) => month === props.maxDate.getMonth() + 1;
    const getValue = (type) => {
      const {
        minDate,
        columnsType
      } = props;
      const index2 = columnsType.indexOf(type);
      const value = computedValues.value[index2];
      if (value) {
        return +value;
      }
      switch (type) {
        case "year":
          return minDate.getFullYear();
        case "month":
          return minDate.getMonth() + 1;
        case "day":
          return minDate.getDate();
      }
    };
    const genYearOptions = () => {
      const minYear = props.minDate.getFullYear();
      const maxYear = props.maxDate.getFullYear();
      return utils.genOptions(minYear, maxYear, "year", props.formatter, props.filter, computedValues.value);
    };
    const genMonthOptions = () => {
      const year = getValue("year");
      const minMonth = isMinYear(year) ? props.minDate.getMonth() + 1 : 1;
      const maxMonth = isMaxYear(year) ? props.maxDate.getMonth() + 1 : 12;
      return utils.genOptions(minMonth, maxMonth, "month", props.formatter, props.filter, computedValues.value);
    };
    const genDayOptions = () => {
      const year = getValue("year");
      const month = getValue("month");
      const minDate = isMinYear(year) && isMinMonth(month) ? props.minDate.getDate() : 1;
      const maxDate = isMaxYear(year) && isMaxMonth(month) ? props.maxDate.getDate() : utils.getMonthEndDay(year, month);
      return utils.genOptions(minDate, maxDate, "day", props.formatter, props.filter, computedValues.value);
    };
    const confirm = () => {
      var _a;
      return (_a = pickerRef.value) == null ? void 0 : _a.confirm();
    };
    const getSelectedDate = () => currentValues.value;
    const columns = Vue.computed(() => props.columnsType.map((type) => {
      switch (type) {
        case "year":
          return genYearOptions();
        case "month":
          return genMonthOptions();
        case "day":
          return genDayOptions();
        default:
          return [];
      }
    }));
    Vue.watch(currentValues, (newValues) => {
      if (!basic.isSameValue(newValues, props.modelValue)) {
        emit("update:modelValue", newValues);
      }
    });
    Vue.watch(() => props.modelValue, (newValues, oldValues) => {
      updatedByExternalSources.value = basic.isSameValue(oldValues, currentValues.value);
      newValues = utils.formatValueRange(newValues, columns.value);
      if (!basic.isSameValue(newValues, currentValues.value)) {
        currentValues.value = newValues;
      }
      updatedByExternalSources.value = false;
    }, {
      immediate: true
    });
    const onChange = (...args) => emit("change", ...args);
    const onCancel = (...args) => emit("cancel", ...args);
    const onConfirm = (...args) => emit("confirm", ...args);
    useExpose.useExpose({
      confirm,
      getSelectedDate
    });
    return () => Vue.createVNode(index.Picker, Vue.mergeProps({
      "ref": pickerRef,
      "modelValue": currentValues.value,
      "onUpdate:modelValue": ($event) => currentValues.value = $event,
      "columns": columns.value,
      "onChange": onChange,
      "onCancel": onCancel,
      "onConfirm": onConfirm
    }, basic.pick(props, utils.pickerInheritKeys)), slots);
  }
});
exports.datePickerProps = datePickerProps;
exports.default = stdin_default;
