"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const utils = require("../date-picker/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 props = require("../utils/props.js");
const [name] = create.createNamespace("time-picker");
const validateTime = (val) => /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/.test(val);
const fullColumns = ["hour", "minute", "second"];
const timePickerProps = basic.extend({}, utils.sharedProps, {
  minHour: props.makeNumericProp(0),
  maxHour: props.makeNumericProp(23),
  minMinute: props.makeNumericProp(0),
  maxMinute: props.makeNumericProp(59),
  minSecond: props.makeNumericProp(0),
  maxSecond: props.makeNumericProp(59),
  minTime: {
    type: String,
    validator: validateTime
  },
  maxTime: {
    type: String,
    validator: validateTime
  },
  columnsType: {
    type: Array,
    default: () => ["hour", "minute"]
  }
});
var stdin_default = Vue.defineComponent({
  name,
  props: timePickerProps,
  emits: ["confirm", "cancel", "change", "update:modelValue"],
  setup(props2, {
    emit,
    slots
  }) {
    const currentValues = Vue.ref(props2.modelValue);
    const pickerRef = Vue.ref();
    const getValidTime = (time) => {
      const timeLimitArr = time.split(":");
      return fullColumns.map((col, i) => props2.columnsType.includes(col) ? timeLimitArr[i] : "00");
    };
    const confirm = () => {
      var _a;
      return (_a = pickerRef.value) == null ? void 0 : _a.confirm();
    };
    const getSelectedTime = () => currentValues.value;
    const columns = Vue.computed(() => {
      let {
        minHour,
        maxHour,
        minMinute,
        maxMinute,
        minSecond,
        maxSecond
      } = props2;
      if (props2.minTime || props2.maxTime) {
        const fullTime = {
          hour: 0,
          minute: 0,
          second: 0
        };
        props2.columnsType.forEach((col, i) => {
          var _a;
          fullTime[col] = (_a = currentValues.value[i]) != null ? _a : 0;
        });
        const {
          hour,
          minute
        } = fullTime;
        if (props2.minTime) {
          const [minH, minM, minS] = getValidTime(props2.minTime);
          minHour = minH;
          minMinute = +hour <= +minHour ? minM : "00";
          minSecond = +hour <= +minHour && +minute <= +minMinute ? minS : "00";
        }
        if (props2.maxTime) {
          const [maxH, maxM, maxS] = getValidTime(props2.maxTime);
          maxHour = maxH;
          maxMinute = +hour >= +maxHour ? maxM : "59";
          maxSecond = +hour >= +maxHour && +minute >= +maxMinute ? maxS : "59";
        }
      }
      return props2.columnsType.map((type) => {
        const {
          filter,
          formatter
        } = props2;
        switch (type) {
          case "hour":
            return utils.genOptions(+minHour, +maxHour, type, formatter, filter, currentValues.value);
          case "minute":
            return utils.genOptions(+minMinute, +maxMinute, type, formatter, filter, currentValues.value);
          case "second":
            return utils.genOptions(+minSecond, +maxSecond, type, formatter, filter, currentValues.value);
          default:
            return [];
        }
      });
    });
    Vue.watch(currentValues, (newValues) => {
      if (!basic.isSameValue(newValues, props2.modelValue)) {
        emit("update:modelValue", newValues);
      }
    });
    Vue.watch(() => props2.modelValue, (newValues) => {
      newValues = utils.formatValueRange(newValues, columns.value);
      if (!basic.isSameValue(newValues, currentValues.value)) {
        currentValues.value = newValues;
      }
    }, {
      immediate: true
    });
    const onChange = (...args) => emit("change", ...args);
    const onCancel = (...args) => emit("cancel", ...args);
    const onConfirm = (...args) => emit("confirm", ...args);
    useExpose.useExpose({
      confirm,
      getSelectedTime
    });
    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(props2, utils.pickerInheritKeys)), slots);
  }
});
exports.default = stdin_default;
exports.timePickerProps = timePickerProps;
