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

import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { toRefs, computed, ref, watchEffect } from '@vue/composition-api';
import dayjs from 'dayjs';
import { g as getDefaultFormat, i as isValidDate, a as formatDate, f as formatTime, p as parseToDayjs } from '../../../_chunks/dep-7af607c9.js';
import { b as extractTimeFormat } from '../../../_chunks/dep-0f952684.js';
import useVModel from '../../../hooks/use-vmodel.js';
import '../../../_chunks/dep-d3027203.js';
import '@babel/runtime/helpers/typeof';
import '../../../_chunks/dep-53d4956d.js';
import '../../../_chunks/dep-a65fe6e9.js';
import '../../../_chunks/dep-5c5da9dd.js';
import '../../../_chunks/dep-33b15d9d.js';
import '../../../_chunks/dep-64cbbe92.js';

var PARTIAL_MAP = {
  first: "start",
  second: "end"
};
function initYearMonthTime(_ref) {
  var value = _ref.value,
      _ref$mode = _ref.mode,
      mode = _ref$mode === void 0 ? "date" : _ref$mode,
      format = _ref.format,
      _ref$timeFormat = _ref.timeFormat,
      timeFormat = _ref$timeFormat === void 0 ? "HH:mm:ss" : _ref$timeFormat,
      enableTimePicker = _ref.enableTimePicker;
  var defaultYearMonthTime = {
    year: [dayjs().year(), dayjs().year()],
    month: [dayjs().month(), dayjs().month()],
    time: [dayjs().format(timeFormat), dayjs().format(timeFormat)]
  };

  if (mode === "year") {
    defaultYearMonthTime.year[1] += 10;
  } else if (mode === "month" || mode === "quarter") {
    defaultYearMonthTime.year[1] += 1;
  } else if ((mode === "date" || mode === "week") && !enableTimePicker) {
    defaultYearMonthTime.month[1] += 1;
  }

  if (!value || !Array.isArray(value) || !value.length) {
    return defaultYearMonthTime;
  }

  return {
    year: value.map(function (v) {
      return parseToDayjs(v, format).year();
    }),
    month: value.map(function (v) {
      return parseToDayjs(v, format).month();
    }),
    time: value.map(function (v) {
      return parseToDayjs(v, format).format(timeFormat);
    })
  };
}
function useRangeValue(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,
      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 (!Array.isArray(value.value)) {
    console.error("typeof value: ".concat(value.value, " must be Array!"));
  } else if (!isValidDate(value.value, formatRef.value.format)) {
    console.error("value: ".concat(value.value, " is invalid dateTime! Check whether the value is consistent with format: ").concat(formatRef.value.format));
  }

  var isFirstValueSelected = ref(false);
  var time = ref(initYearMonthTime({
    value: value.value,
    mode: props.mode,
    format: formatRef.value.format,
    timeFormat: formatRef.value.timeFormat
  }).time);
  var month = ref(initYearMonthTime({
    value: value.value,
    mode: props.mode,
    format: formatRef.value.format,
    enableTimePicker: props.enableTimePicker
  }).month);
  var year = ref(initYearMonthTime({
    value: value.value,
    mode: props.mode,
    format: formatRef.value.format
  }).year);
  var cacheValue = ref(formatDate(value.value, {
    format: formatRef.value.format
  }));
  watchEffect(function () {
    if (!value.value) {
      cacheValue.value = [];
      return;
    }

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

export { PARTIAL_MAP, useRangeValue as default, initYearMonthTime };
//# sourceMappingURL=use-range-value.js.map
