/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var VueCompositionAPI = require('@vue/composition-api');
var dayjs = require('dayjs');
var format = require('../../../chunks/format.js');
var utils = require('../../../chunks/utils2.js');
var hooks_useVmodel = require('../../../hooks/use-vmodel.js');
require('../../../chunks/dayjs.min-12eb376c.js');
require('@babel/runtime/helpers/typeof');
require('../../../chunks/_commonjsHelpers-ea2bee21.js');
require('../../../chunks/log.js');
require('../../../chunks/isObjectLike-2443c6ff.js');
require('../../../chunks/defineProperty-28395935.js');
require('../../../chunks/_isIndex-1bf92538.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);

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

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

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

  if (props.enableTimePicker) {
    if (!utils.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"));
  }

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

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

exports["default"] = useSingleValue;
//# sourceMappingURL=use-single-value.js.map
