"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
;/* empty css                                               */
;/* empty css                                                */
;/* empty css                                               */
;/* empty css                                                  */
;/* empty css                                                 */
;/* empty css                                                  */
;/* empty css                                                */
;/* empty css                                                */
;/* empty css                                                 */
;/* empty css                                                */
;/* empty css                                                     */
;/* empty css                                               */
;/* empty css                                              */
;/* empty css                                                 */
;/* empty css                                                       */
;/* empty css                                                   */
const Vue = require("vue");
const index = require("../field/index.js");
const moment = require("../../../../node_modules/moment/dist/moment.js");
const index$1 = require("../../../../node_modules/vant/es/calendar/index.js");
const index$2 = require("../../../../node_modules/vant/es/icon/index.js");
const _sfc_main = {
  __name: "FieldCalendarPicker",
  props: {
    title: {
      type: String,
      default: "日期选择"
    },
    calendarType: {
      type: String,
      default: "single"
    },
    textFormat: {
      type: String,
      default: "YYYY-MM-DD"
    },
    valueFormat: {
      type: String,
      default: "YYYYMMDD"
    },
    modelValue: {
      type: String,
      default: ""
    },
    disabled: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    },
    "is-link": false
  },
  emits: ["update:modelValue"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const fieldTextValue = Vue.ref("");
    const showCalendar = Vue.ref(false);
    const emit = __emit;
    const defaultDate = Vue.computed({
      get: () => {
        if (!props.modelValue) {
          return null;
        }
        if (props.calendarType == "single") {
          try {
            return moment.default(props.modelValue, props.valueFormat).toDate();
          } catch (error) {
            return /* @__PURE__ */ new Date();
          }
        } else if (props.calendarType == "multiple") {
          try {
            return props.modelValue.map(
              (value) => moment.default(value, props.valueFormat).toDate()
            );
          } catch (error) {
            return [/* @__PURE__ */ new Date()];
          }
        } else if (props.calendarType == "range") {
          try {
            return props.modelValue.map(
              (value) => moment.default(value, props.valueFormat).toDate()
            );
          } catch (error) {
            return [];
          }
        }
      },
      set: (val) => {
        emit("update:modelValue", val);
      }
    });
    Vue.watch(
      () => props.modelValue,
      (value) => {
        if (!value) {
          fieldTextValue.value = "";
          return;
        }
        if (props.calendarType == "single") {
          fieldTextValue.value = moment.default(value, props.valueFormat).format(
            props.textFormat
          );
        } else if (props.calendarType == "multiple") {
          if (Array.isArray(value)) {
            fieldTextValue.value = `选择了 ${value.length} 个日期`;
          }
        } else if (props.calendarType == "range") {
          if (Array.isArray(value)) {
            const [start, end] = value;
            fieldTextValue.value = `${moment.default(start, props.valueFormat).format(
              props.textFormat
            )} 至 ${moment.default(end, props.valueFormat).format(props.textFormat)}`;
          }
        }
      },
      { immediate: true }
    );
    const onConfirm = (values) => {
      if (props.calendarType == "single") {
        defaultDate.value = moment.default(values).format(props.valueFormat);
      } else if (props.calendarType == "multiple") {
        defaultDate.value = values.map(
          (value) => moment.default(value).format(props.valueFormat)
        );
      } else if (props.calendarType == "range") {
        defaultDate.value = values.map(
          (value) => moment.default(value).format(props.valueFormat)
        );
      }
      showCalendar.value = false;
    };
    return (_ctx, _cache) => {
      const _component_van_icon = index$2.Icon;
      const _component_van_calendar = index$1.Calendar;
      return Vue.openBlock(), Vue.createBlock(Vue.unref(index.MobileField), Vue.mergeProps(_ctx.$attrs, {
        modelValue: Vue.unref(fieldTextValue),
        "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => Vue.isRef(fieldTextValue) ? fieldTextValue.value = $event : null),
        isLink: __props.readonly || __props.disabled ? false : true,
        readonly: "",
        disabled: __props.disabled,
        onOpenPicker: _cache[3] || (_cache[3] = () => {
          if (!__props.readonly) {
            showCalendar.value = true;
          }
        })
      }), Vue.createSlots({
        default: Vue.withCtx(() => [
          Vue.createVNode(_component_van_calendar, Vue.mergeProps(_ctx.$attrs, {
            show: Vue.unref(showCalendar),
            "onUpdate:show": _cache[1] || (_cache[1] = ($event) => Vue.isRef(showCalendar) ? showCalendar.value = $event : null),
            "default-date": Vue.unref(defaultDate),
            title: __props.title,
            type: __props.calendarType,
            readonly: false,
            "switch-mode": "year-month",
            onConfirm
          }), null, 16, ["show", "default-date", "title", "type"])
        ]),
        _: 2
      }, [
        !__props.readonly && !__props.disabled && Vue.unref(fieldTextValue) ? {
          name: "right-icon",
          fn: Vue.withCtx(() => [
            Vue.createVNode(_component_van_icon, {
              name: "clear",
              class: "custom-close-icon",
              onClick: _cache[0] || (_cache[0] = Vue.withModifiers(($event) => defaultDate.value = null, ["stop"]))
            })
          ]),
          key: "0"
        } : void 0
      ]), 1040, ["modelValue", "isLink", "disabled"]);
    };
  }
};
exports.default = _sfc_main;
