/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { toRefs, computed, ref, watchEffect } from '@vue/composition-api';
import dayjs from 'dayjs';
import { g as getDefaultFormat, i as isValidDate, f as formatDate, a as formatTime, p as parseToDayjs } from '../../../_chunks/dep-c3901797.js';
import { b as extractTimeFormat } from '../../../_chunks/dep-68f38014.js';
import useVModel from '../../../hooks/use-vmodel.js';
import '../../../_chunks/dep-f38f4c93.js';
import '../../../_chunks/dep-5210251b.js';
import '../../../_chunks/dep-70c789e5.js';
import '../../../_chunks/dep-24cf4e98.js';
import '../../../_chunks/dep-eb467ddf.js';
import '../../../_chunks/dep-b31eb7d9.js';
import '../../../_chunks/dep-fe18d4b0.js';
import '../../../_chunks/dep-22504add.js';
import 'lodash/kebabCase';

const PARTIAL_MAP = {
  first: "start",
  second: "end"
};
function initYearMonthTime(_ref) {
  let {
    value,
    mode = "date",
    format,
    timeFormat = "HH:mm:ss",
    enableTimePicker
  } = _ref;
  const 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(v => parseToDayjs(v, format).year()),
    month: value.map(v => parseToDayjs(v, format).month()),
    time: value.map(v => parseToDayjs(v, format).format(timeFormat))
  };
}
function useRangeValue(props) {
  const {
    value: valueFromProps
  } = toRefs(props);
  const [value, onChange] = useVModel(valueFromProps, props.defaultValue, props.onChange, "change");
  const formatRef = computed(() => getDefaultFormat({
    mode: props.mode,
    format: props.format,
    enableTimePicker: props.enableTimePicker
  }));
  if (props.enableTimePicker) {
    if (!extractTimeFormat(formatRef.value.format)) console.error(`format: ${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: ${value.value} must be Array!`);
  } else if (!isValidDate(value.value, formatRef.value.format)) {
    console.error(`value: ${value.value} is invalid dateTime! Check whether the value is consistent with format: ${formatRef.value.format}`);
  }
  const isFirstValueSelected = ref(false);
  const time = ref(initYearMonthTime({
    value: value.value,
    mode: props.mode,
    format: formatRef.value.format,
    timeFormat: formatRef.value.timeFormat
  }).time);
  const month = ref(initYearMonthTime({
    value: value.value,
    mode: props.mode,
    format: formatRef.value.format,
    enableTimePicker: props.enableTimePicker
  }).month);
  const year = ref(initYearMonthTime({
    value: value.value,
    mode: props.mode,
    format: formatRef.value.format
  }).year);
  const cacheValue = ref(formatDate(value.value, {
    format: formatRef.value.format
  }));
  watchEffect(() => {
    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,
    month,
    value,
    time,
    isFirstValueSelected,
    cacheValue,
    onChange
  };
}

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