/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { toRefs, computed, ref, watchEffect } from '@vue/composition-api';
import dayjs from 'dayjs';
import { getDefaultFormat, formatTime, formatDate, isValidDate } from '../../_common/js/date-picker/format.js';
import useVModel from '../../hooks/useVModel.js';
import { extractTimeFormat } from '../../_common/js/date-picker/utils.js';
import '../../_common/js/log/index.js';
import '../../_common/js/log/log.js';
import '@babel/runtime/helpers/extends';
import '@babel/runtime/helpers/defineProperty';
import '../../_chunks/dep-a0d91f92.js';
import '../../_chunks/dep-b98966c0.js';
import 'lodash/chunk';

function useSingleValue(props) {
  var _toRefs = toRefs(props),
      valueFromProps = _toRefs.value;

  var _useVModel = useVModel(valueFromProps, props.defaultValue, props.onChange, "change"),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      value = _useVModel2[0],
      onChange = _useVModel2[1];

  var formatRef = computed(function () {
    return getDefaultFormat({
      mode: props.mode,
      format: props.format,
      valueType: props.valueType,
      enableTimePicker: props.enableTimePicker
    });
  });

  if (props.enableTimePicker) {
    if (!extractTimeFormat(formatRef.value.format)) console.error("format: ".concat(formatRef.value.format, " \u4E0D\u89C4\u8303\uFF0C\u5305\u542B\u65F6\u95F4\u9009\u62E9\u5FC5\u987B\u8981\u6709\u65F6\u95F4\u683C\u5F0F\u5316 HH:mm:ss"));
    if (!extractTimeFormat(formatRef.value.valueType) && formatRef.value.valueType !== "time-stamp") console.error("valueType: ".concat(formatRef.value.valueType, " \u4E0D\u89C4\u8303\uFF0C\u5305\u542B\u65F6\u95F4\u9009\u62E9\u5FC5\u987B\u8981\u6709\u65F6\u95F4\u683C\u5F0F\u5316 HH:mm:ss"));
  }

  var time = ref(formatTime(value.value, formatRef.value.timeFormat));
  var month = ref(dayjs(value.value).month() || new Date().getMonth());
  var year = ref(dayjs(value.value).year() || new Date().getFullYear());
  var cacheValue = ref(formatDate(value.value, {
    format: formatRef.value.format,
    targetFormat: formatRef.value.format
  }));
  watchEffect(function () {
    if (!value.value) {
      cacheValue.value = "";
      return;
    }

    if (!isValidDate(value.value, formatRef.value.valueType)) return;
    cacheValue.value = formatDate(value.value, {
      format: formatRef.value.format,
      targetFormat: formatRef.value.format
    });
    time.value = formatTime(value.value, formatRef.value.timeFormat);
  });
  return {
    year: year,
    month: month,
    value: value,
    time: time,
    cacheValue: cacheValue,
    onChange: onChange
  };
}

export { useSingleValue as default };
//# sourceMappingURL=useSingleValue.js.map
