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

'use strict';

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

var VueCompositionAPI = require('@vue/composition-api');
var dayjs = require('dayjs');
var format = require('../../../chunks/format.js');
var utils = require('../../../chunks/utils.js');
var hooks_useVmodel = require('../../../hooks/use-vmodel.js');
require('../../../chunks/dayjs.min-c4f4ce86.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../../chunks/log.js');
require('../../../chunks/isObjectLike-c22c7e3b.js');
require('../../../chunks/defineProperty-838e4192.js');
require('../../../chunks/_isIndex-52887d1e.js');
require('../../../chunks/isFunction-91acade7.js');
require('../../../chunks/isSymbol-a38a5184.js');
require('lodash/kebabCase');

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

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

const PARTIAL_MAP = {
  first: "start",
  second: "end"
};
function initYearMonthTime(_ref) {
  let {
    value,
    mode = "date",
    format: format$1,
    timeFormat = "HH:mm:ss",
    enableTimePicker
  } = _ref;
  const defaultYearMonthTime = {
    year: [dayjs__default["default"]().year(), dayjs__default["default"]().year()],
    month: [dayjs__default["default"]().month(), dayjs__default["default"]().month()],
    time: [dayjs__default["default"]().format(timeFormat), dayjs__default["default"]().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 => format.parseToDayjs(v, format$1).year()),
    month: value.map(v => format.parseToDayjs(v, format$1).month()),
    time: value.map(v => format.parseToDayjs(v, format$1).format(timeFormat))
  };
}
function useRangeValue(props) {
  const {
    value: valueFromProps
  } = VueCompositionAPI.toRefs(props);
  const [value, onChange] = hooks_useVmodel["default"](valueFromProps, props.defaultValue, props.onChange, "change");
  const formatRef = VueCompositionAPI.computed(() => format.getDefaultFormat({
    mode: props.mode,
    format: props.format,
    enableTimePicker: props.enableTimePicker
  }));
  if (props.enableTimePicker) {
    if (!utils.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 (!format.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 = VueCompositionAPI.ref(false);
  const time = VueCompositionAPI.ref(initYearMonthTime({
    value: value.value,
    mode: props.mode,
    format: formatRef.value.format,
    timeFormat: formatRef.value.timeFormat
  }).time);
  const month = VueCompositionAPI.ref(initYearMonthTime({
    value: value.value,
    mode: props.mode,
    format: formatRef.value.format,
    enableTimePicker: props.enableTimePicker
  }).month);
  const year = VueCompositionAPI.ref(initYearMonthTime({
    value: value.value,
    mode: props.mode,
    format: formatRef.value.format
  }).year);
  const cacheValue = VueCompositionAPI.ref(format.formatDate(value.value, {
    format: formatRef.value.format
  }));
  VueCompositionAPI.watchEffect(() => {
    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,
    month,
    value,
    time,
    isFirstValueSelected,
    cacheValue,
    onChange
  };
}

exports.PARTIAL_MAP = PARTIAL_MAP;
exports["default"] = useRangeValue;
exports.initYearMonthTime = initYearMonthTime;
//# sourceMappingURL=use-range-value.js.map
