import { defineComponent, ref, watch, watchEffect, openBlock, createElementBlock, createVNode, createBlock, unref, mergeProps, withCtx } from "vue";
import dayjs from "dayjs";
import { _ as _sfc_main$2 } from "./index8.js";
import { i as isLeapYear } from "./index5.js";
import { Field } from "vant";
import { _ as _export_sfc } from "./plugin-vue_export-helper.js";
import "./index9.js";
import "./index6.js";
import "@wltech/svg";
const dateKey = {
  year: "year",
  month: "month",
  day: "day"
};
const timeKey = {
  hour: "hour",
  minute: "minute",
  second: "second"
};
const dataKeyArr = Reflect.ownKeys(dateKey);
const timeKeyArr = Reflect.ownKeys(timeKey);
const keyToMethods = {
  year: "getFullYear",
  month: "getMonth",
  day: "getDate",
  hour: "getHours",
  minute: "getMinutes",
  second: "getSeconds"
};
const onGetDayNumber = (month, year) => {
  const month31 = [1, 3, 5, 7, 8, 10, 12];
  if (month === 2) {
    if (year && isLeapYear(year)) {
      return 29;
    } else {
      return 28;
    }
  } else {
    if (month31.includes(month)) {
      return 31;
    } else {
      return 30;
    }
  }
};
const onGetTime = (data) => {
  let time = data.reduce((pre, cur, currentIndex) => {
    let prefix = "", suffix = "";
    if ([dateKey.day, dateKey.month].includes(cur.type) && pre) {
      prefix = "/";
    }
    if ([timeKey.minute, timeKey.second].includes(cur.type) && pre) {
      prefix = ":";
    }
    if (cur.type === timeKey.hour && pre) {
      prefix = " ";
    }
    if (cur.type === timeKey.hour && currentIndex === data.length - 1) {
      suffix = ":0:0";
    }
    pre = pre + prefix + cur.value + suffix;
    return pre;
  }, "");
  if (time.indexOf("/") === time.lastIndexOf("/") && time.indexOf("/") !== -1) {
    time += "/1";
  }
  if (time.indexOf("/") === -1) {
    const currentTime = new Date();
    time += `/${currentTime.getMonth() + 1}/${currentTime.getDate()}`;
  }
  return new Date(time);
};
const _hoisted_1 = {
  class: "wl-date-time-picker",
  "data-weiling-mobile-ui-wl-date-time-picker": ""
};
const __default__ = {
  name: "WlDateTimePicker"
};
const _sfc_main$1 = /* @__PURE__ */ defineComponent({
  ...__default__,
  props: {
    minDate: { default: () => new Date("2000/1/1") },
    maxDate: { default: () => new Date("2050/12/31") },
    type: { default: "date-time" },
    show: { type: Boolean, default: false },
    title: { default: "" },
    descShow: { type: Boolean, default: true },
    defalutTime: { default: () => new Date() }
  },
  emits: ["onConfirm", "onChange", "update:show", "onClose"],
  setup(__props, { emit }) {
    const props = __props;
    const pickerColumns = ref([]);
    const visible = ref(true);
    const pickerDefaultValue = ref([]);
    const getNeedColumns = (type = "date") => {
      let needColumns = [];
      if (type === "date") {
        if (props.type.includes("date")) {
          return dataKeyArr;
        } else {
          dataKeyArr.forEach((ele) => {
            if (props.type.includes(ele)) {
              needColumns.push(ele);
            }
          });
        }
      }
      if (type === "time") {
        if (props.type.includes("time")) {
          return timeKeyArr;
        } else {
          timeKeyArr.forEach((ele) => {
            if (props.type.includes(ele)) {
              needColumns.push(ele);
            }
          });
        }
      }
      return needColumns;
    };
    const getNeedValue = (funcName, type, desc = "", addStep = 0, min = 0, max = 0) => {
      const minData = funcName ? props.minDate[funcName]() + addStep : min;
      const maxData = funcName ? props.maxDate[funcName]() + addStep : max;
      let data = [];
      for (let i = minData; i <= maxData; i++) {
        data.push({
          text: `${i < 10 ? "0" + i : i}${!props.descShow ? "" : desc}`,
          value: i,
          type
        });
      }
      return data;
    };
    const onGetDayCols = (currentYear, currentMonth) => {
      const minYear = props.minDate["getFullYear"]();
      const maxYear = props.maxDate["getFullYear"]();
      const minMon = props.minDate["getMonth"]() + 1;
      const maxMon = props.maxDate["getMonth"]() + 1;
      const minDay = props.minDate["getDate"]();
      const maxDay = props.maxDate["getDate"]();
      let minD = 1, maxD = 31;
      if (currentYear === minYear && currentMonth === minMon && currentMonth) {
        minD = minDay;
      } else if (currentYear === maxYear && currentMonth === maxMon && currentMonth) {
        maxD = maxDay;
      } else if (currentMonth) {
        if (currentMonth === 2) {
          maxD = currentYear ? onGetDayNumber(currentYear, currentMonth) : 29;
        } else {
          maxD = onGetDayNumber(
            currentMonth,
            currentYear || new Date().getFullYear()
          );
        }
      }
      return getNeedValue("", dateKey.day, "\u65E5", 0, minD, maxD);
    };
    const onGetMonthCol = (currentYear) => {
      const minYear = props.minDate["getFullYear"]();
      const maxYear = props.maxDate["getFullYear"]();
      const minMon = props.minDate["getMonth"]() + 1;
      const maxMon = props.maxDate["getMonth"]() + 1;
      const minMonth = currentYear === minYear ? minMon : 1;
      const maxMonth = currentYear === maxYear ? maxMon : 12;
      const data = getNeedValue("", dateKey.month, "\u6708", 1, minMonth, maxMonth);
      return data;
    };
    const getDateColumns = () => {
      var _a, _b, _c;
      const needColumns = getNeedColumns("date");
      let columns = [];
      if (needColumns.includes(dateKey.year)) {
        const data = getNeedValue("getFullYear", dateKey.year, "\u5E74");
        columns.push(data);
      }
      if (needColumns.includes(dateKey.month)) {
        const currentYear = ((_a = props.defalutTime) == null ? void 0 : _a.getFullYear()) || new Date().getFullYear();
        columns.push(onGetMonthCol(currentYear));
      }
      if (needColumns.includes(dateKey.day)) {
        columns.push(
          onGetDayCols(
            ((_b = props.defalutTime) == null ? void 0 : _b.getFullYear()) || new Date().getFullYear(),
            ((_c = props.defalutTime) == null ? void 0 : _c.getMonth()) + 1 || new Date().getMonth() + 1
          )
        );
      }
      return columns;
    };
    const getTimeColumns = () => {
      const needColumns = getNeedColumns("time");
      let columns = [];
      if (needColumns.includes(timeKey.hour)) {
        const data = getNeedValue("", timeKey.hour, "\u65F6", 0, 0, 23);
        columns.push(data);
      }
      if (needColumns.includes(timeKey.minute)) {
        const data = getNeedValue("", timeKey.minute, "\u5206", 0, 0, 59);
        columns.push(data);
      }
      if (needColumns.includes(timeKey.second)) {
        const data = getNeedValue("", timeKey.second, "\u79D2", 0, 0, 59);
        columns.push(data);
      }
      return columns;
    };
    const onConfirm = (val, indexs, { selectedOptions: data }) => {
      console.log(1111111, val, indexs, data);
      emit("onConfirm", {
        data,
        time: onGetTime(data)
      });
    };
    let preChangeData = [];
    const onChange = (val, index2, { selectedOptions: data }) => {
      const dayIndex = data.findIndex((ele) => ele.type === dateKey.day);
      const monthIndex = data.findIndex((ele) => ele.type === dateKey.month);
      if (dayIndex !== -1) {
        const columns = pickerColumns.value;
        let preYear = { value: 0 }, preMonth = { value: 0 };
        let curYear = { value: 0 }, curMonth = { value: 0 };
        if (dayIndex !== 1) {
          preYear = preChangeData[0] || { value: 0 };
          preMonth = preChangeData[1] || { value: 0 };
          curYear = data[0];
          curMonth = data[1];
        } else {
          preMonth = preChangeData[0] || { value: 0 };
          curMonth = data[0];
        }
        if (curYear.value !== preYear.value) {
          columns[dayIndex] = onGetDayCols(curYear.value, curMonth.value);
          columns[monthIndex] = onGetMonthCol(curYear.value);
        } else if (preMonth.value !== curMonth.value) {
          columns[dayIndex] = onGetDayCols(curYear.value, curMonth.value);
          preChangeData = data;
          pickerColumns.value = columns;
        }
      }
      emit("onChange", {
        data,
        time: onGetTime(data)
      });
    };
    const onClose = () => {
      emit("update:show", false);
      emit("onClose");
    };
    watch(
      () => [props.type, props.maxDate, props.minDate, props.defalutTime],
      () => {
        pickerColumns.value = [...getDateColumns(), ...getTimeColumns()];
      },
      { immediate: true }
    );
    watchEffect(() => {
      visible.value = props.show;
    });
    watch(
      () => [props.defalutTime, pickerColumns.value],
      () => {
        if (props.defalutTime && pickerColumns.value.length !== 0) {
          const needColumns = [
            ...getNeedColumns("date"),
            ...getNeedColumns("time")
          ];
          const indexs = [];
          needColumns.forEach((key, i) => {
            let data = props.defalutTime[keyToMethods[key]]();
            if (key === dateKey.month) {
              data += 1;
            }
            indexs.push(data);
          });
          pickerDefaultValue.value = indexs;
        }
      },
      {
        immediate: true
      }
    );
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createVNode(_sfc_main$2, {
          modelValue: visible.value,
          "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => visible.value = $event),
          title: __props.title,
          columns: pickerColumns.value,
          teleport: "",
          contentHeight: "300px",
          pickerDefaultValue: pickerDefaultValue.value,
          onOnConfirm: onConfirm,
          onOnChange: onChange,
          onOnClose: onClose
        }, null, 8, ["modelValue", "title", "columns", "pickerDefaultValue"])
      ]);
    };
  }
});
var index_vue_vue_type_style_index_0_scoped_true_lang = "";
var index_vue_vue_type_style_index_1_lang = "";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    value: { default: "" },
    item: { default: () => {
    } }
  },
  emits: ["update:value"],
  setup(__props, { emit }) {
    const props = __props;
    const formatDate = (stamp, type) => {
      if (!stamp)
        return "";
      return dayjs(+stamp).format(type);
    };
    const DATE_TIME_TYPE = {
      YYYY: "year",
      "YYYY/MM": "year month",
      "YYYY/MM/DD": "year month day",
      "YYYY/MM/DD HH": "year month day hour",
      "YYYY/MM/DD HH:mm": "year month day hour minute",
      "YYYY/MM/DD HH:mm:ss": "year month day hour minute"
    };
    const fieldValue = ref("");
    const currentDate = ref(new Date());
    const showCalendar = ref(false);
    const onConfirmSelect = (item) => {
      currentDate.value = item.time;
      fieldValue.value = formatDate(
        new Date(item.time).getTime(),
        props.item.property_format || "YYYY-MM-DD"
      );
      emit("update:value", new Date(item.time).getTime() + "");
      showCalendar.value = false;
    };
    watch(
      () => props.value,
      (val) => {
        if (val) {
          currentDate.value = new Date(val);
          fieldValue.value = formatDate(
            val,
            props.item.property_format || "YYYY/MM/DD"
          );
        }
      },
      {
        immediate: true
      }
    );
    const onReset = () => {
      currentDate.value = "";
      fieldValue.value = "";
      emit("update:value", "");
    };
    return (_ctx, _cache) => {
      return openBlock(), createBlock(unref(Field), mergeProps({
        modelValue: fieldValue.value,
        "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => fieldValue.value = $event),
        label: __props.item.label
      }, __props.item, {
        readonly: "",
        "right-icon": fieldValue.value && __props.item.clearable ? "clear" : "",
        onClickInput: _cache[2] || (_cache[2] = ($event) => showCalendar.value = true),
        onClickRightIcon: onReset
      }), {
        extra: withCtx(() => [
          createVNode(_sfc_main$1, mergeProps(__props.item.dateConfig, {
            defalutTime: currentDate.value,
            show: showCalendar.value,
            "onUpdate:show": _cache[0] || (_cache[0] = ($event) => showCalendar.value = $event),
            onOnConfirm: onConfirmSelect,
            type: DATE_TIME_TYPE[__props.item.property_format] || "date"
          }), null, 16, ["defalutTime", "show", "type"])
        ]),
        _: 1
      }, 16, ["modelValue", "label", "right-icon"]);
    };
  }
});
var index = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-40ac8b98"]]);
export { index as default };
