/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, computed, watch, createVNode } from 'vue';
import dayjs from 'dayjs';
import isFunction from 'lodash/isFunction';
import { usePrefixClass } from '../hooks/useConfig.js';
import { useFormDisabled } from '../form/hooks.js';
import { getDefaultFormat, formatDate, parseToDayjs, formatTime } from '../_common/js/date-picker/format.js';
import { subtractMonth, addMonth, extractTimeObj } from '../_common/js/date-picker/utils.js';
import { SelectInput } from '../select-input/index.js';
import useSingle from './hooks/useSingle.js';
import datePickerProps from './props.js';
import TSinglePanel from './panel/SinglePanel.js';
import '../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import 'lodash/isString';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-512eb181.js';
import '../_chunks/dep-ac28b67c.js';
import '../config-provider/type.js';
import '../_common/js/log/index.js';
import '../_common/js/log/log.js';
import '@babel/runtime/helpers/slicedToArray';
import '@babel/runtime/helpers/objectDestructuringEmpty';
import '@babel/runtime/helpers/extends';
import '../_chunks/dep-ccfdbba1.js';
import 'lodash/chunk';
import '../utils/withInstall.js';
import '../select-input/select-input.js';
import '../popup/index.js';
import '../popup/popup.js';
import '@popperjs/core';
import 'lodash/isObject';
import '../utils/dom.js';
import '../utils/easing.js';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../_common/js/utils/set-style.js';
import '../hooks/useVModel.js';
import '../popup/props.js';
import '../popup/container.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/isUndefined';
import '../hooks/useResizeObserver.js';
import '../popup/style';
import '../popup/type.js';
import '../hooks/index.js';
import '../hooks/icon.js';
import '../hooks/slot.js';
import '../hooks/tnode.js';
import '../hooks/useCommonClassName.js';
import '../hooks/useDefaultValue.js';
import '../hooks/useDestroyOnClose.js';
import '../hooks/useKeepAnimation.js';
import '../hooks/useRipple.js';
import '../utils/set-style.js';
import '../hooks/useVirtualScroll.js';
import '../select-input/props.js';
import '../select-input/useSingle.js';
import 'lodash/pick';
import '../input/index.js';
import '../input/input.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@ldesign/icons-vue-next';
import '../hooks/useGlobalIcon.js';
import '../input/props.js';
import '../input/useInput.js';
import '../form/const.js';
import '../input/useLengthLimit.js';
import 'lodash/isNumber';
import '../_common/js/utils/helper.js';
import 'lodash/isNull';
import '../input/useInputEventHandler.js';
import '../input/useInputWidth.js';
import '../input/input-group.js';
import '../input/input-group-props.js';
import '../input/style';
import '../input/type.js';
import '../loading/index.js';
import '../loading/directive.js';
import '../loading/plugin.js';
import '../loading/loading.js';
import '../hooks/useTeleport.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../loading/style';
import '../loading/type.js';
import '../select-input/useMultiple.js';
import '../tag-input/index.js';
import '../tag-input/tag-input.js';
import '../tag-input/props.js';
import '../tag-input/hooks/useTagScroll.js';
import '../tag-input/useTagList.js';
import '../tag/index.js';
import '../tag/tag.js';
import '../tag/props.js';
import '../tag/check-tag.js';
import '../tag/check-tag-props.js';
import '../tag/style';
import '../tag/type.js';
import '../tag-input/hooks/useHover.js';
import '../tag-input/hooks/useDragSorter.js';
import '../tag-input/style';
import '../tag-input/type.js';
import '../select-input/useOverlayInnerStyle.js';
import '../select-input/style';
import '../select-input/type.js';
import './hooks/useSingleValue.js';
import './hooks/useTableData.js';
import './hooks/useDisableDate.js';
import './panel/ExtraContent.js';
import './base/Footer.js';
import '../button/index.js';
import '../button/button.js';
import '../button/props.js';
import '../button/style';
import '../button/type.js';
import './panel/PanelContent.js';
import './base/Header.js';
import '../pagination/index.js';
import '../pagination/pagination.js';
import 'lodash/isNaN';
import '../input-number/index.js';
import '../input-number/input-number.js';
import '../input-number/props.js';
import '../input-number/useInputNumber.js';
import '../_common/js/input-number/number.js';
import 'lodash/isNil';
import '../_common/js/input-number/large-number.js';
import '../input-number/style';
import '../input-number/type.js';
import '../select/index.js';
import '../select/select.js';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/intersection';
import '../common-components/fake-arrow.js';
import '../select/props.js';
import '../select/select-panel.js';
import 'lodash/omit';
import '../select/option.js';
import '../checkbox/index.js';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../checkbox/constants.js';
import '../checkbox/group.js';
import '../checkbox/checkbox-group-props.js';
import '../checkbox/style';
import '../checkbox/type.js';
import '../select/option-props.js';
import '../select/helper.js';
import '../select/optionGroup.js';
import '../select/option-group-props.js';
import '../select/hooks/usePanelVirtualScroll.js';
import '../select/hooks/useSelectOptions.js';
import '../select/style';
import '../select/type.js';
import '../input-adornment/index.js';
import '../input-adornment/input-adornment.js';
import '../input-adornment/props.js';
import '../input-adornment/style';
import '../input-adornment/type.js';
import '../pagination/props.js';
import '../pagination/usePaginationClasses.js';
import '../pagination/useMoreAction.js';
import '../pagination/pagination-mini.js';
import '../pagination/pagination-mini-props.js';
import '../pagination/style';
import '../pagination/type.js';
import './base/Table.js';
import './base/Cell.js';
import '../time-picker/panel/time-picker-panel.js';
import '../_common/js/time-picker/const.js';
import '../time-picker/panel/props.js';
import 'lodash/isBoolean';
import '../time-picker/props.js';
import '../time-picker/panel/single-panel.js';
import 'lodash/range';
import 'lodash/padStart';
import '../_common/js/time-picker/utils.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var _DatePicker = defineComponent({
  name: "TDatePicker",
  props: _objectSpread({}, datePickerProps),
  setup: function setup(props2) {
    var COMPONENT_NAME = usePrefixClass("date-picker");
    var _useSingle = useSingle(props2),
      inputValue = _useSingle.inputValue,
      popupVisible = _useSingle.popupVisible,
      inputProps = _useSingle.inputProps,
      popupProps = _useSingle.popupProps,
      isHoverCell = _useSingle.isHoverCell,
      cacheValue = _useSingle.cacheValue,
      value = _useSingle.value,
      year = _useSingle.year,
      month = _useSingle.month,
      time = _useSingle.time,
      inputRef = _useSingle.inputRef,
      onChange = _useSingle.onChange;
    var disabled = useFormDisabled();
    var formatRef = computed(function () {
      return getDefaultFormat({
        mode: props2.mode,
        format: props2.format,
        valueType: props2.valueType,
        enableTimePicker: props2.enableTimePicker
      });
    });
    watch(popupVisible, function (visible) {
      cacheValue.value = formatDate(value.value, {
        format: formatRef.value.format
      });
      inputValue.value = formatDate(value.value, {
        format: formatRef.value.format
      });
      if (visible) {
        year.value = parseToDayjs(value.value, formatRef.value.format).year();
        month.value = parseToDayjs(value.value, formatRef.value.format).month();
        time.value = formatTime(value.value, formatRef.value.timeFormat, props2.defaultTime);
      } else {
        isHoverCell.value = false;
      }
    });
    function onCellMouseEnter(date) {
      isHoverCell.value = true;
      inputValue.value = formatDate(date, {
        format: formatRef.value.format
      });
    }
    function onCellMouseLeave() {
      isHoverCell.value = false;
      inputValue.value = formatDate(cacheValue.value, {
        format: formatRef.value.format
      });
    }
    function onCellClick(date) {
      var _props2$onPick;
      isHoverCell.value = false;
      if (props2.mode === "date") {
        year.value = date.getFullYear();
        month.value = date.getMonth();
      }
      if (props2.enableTimePicker) {
        cacheValue.value = formatDate(date, {
          format: formatRef.value.format
        });
      } else {
        onChange === null || onChange === void 0 ? void 0 : onChange(formatDate(date, {
          format: formatRef.value.format,
          targetFormat: formatRef.value.valueType
        }), {
          dayjsValue: parseToDayjs(date, formatRef.value.format),
          trigger: "pick"
        });
        popupVisible.value = false;
      }
      (_props2$onPick = props2.onPick) === null || _props2$onPick === void 0 ? void 0 : _props2$onPick.call(props2, date);
    }
    function onJumperClick(_ref) {
      var trigger = _ref.trigger;
      var monthCountMap = {
        date: 1,
        week: 1,
        month: 12,
        quarter: 12,
        year: 120
      };
      var monthCount = monthCountMap[props2.mode] || 0;
      var current = new Date(year.value, month.value);
      var next = null;
      if (trigger === "prev") next = subtractMonth(current, monthCount);else if (trigger === "current") next = new Date();else if (trigger === "next") next = addMonth(current, monthCount);
      var nextYear = next.getFullYear();
      var nextMonth = next.getMonth();
      year.value = nextYear;
      month.value = nextMonth;
    }
    function onTimePickerChange(val) {
      var _props2$onPick2;
      time.value = val;
      var _extractTimeObj = extractTimeObj(val),
        hours = _extractTimeObj.hours,
        minutes = _extractTimeObj.minutes,
        seconds = _extractTimeObj.seconds,
        milliseconds = _extractTimeObj.milliseconds,
        meridiem = _extractTimeObj.meridiem;
      var nextHours = hours;
      if (/am/i.test(meridiem) && nextHours === 12) nextHours -= 12;
      if (/pm/i.test(meridiem) && nextHours < 12) nextHours += 12;
      var currentDate = !dayjs(inputValue.value, formatRef.value.format).isValid() ? dayjs() : dayjs(inputValue.value, formatRef.value.format);
      var nextDate = currentDate.hour(nextHours).minute(minutes).second(seconds).millisecond(milliseconds).toDate();
      inputValue.value = formatDate(nextDate, {
        format: formatRef.value.format
      });
      cacheValue.value = formatDate(nextDate, {
        format: formatRef.value.format
      });
      (_props2$onPick2 = props2.onPick) === null || _props2$onPick2 === void 0 ? void 0 : _props2$onPick2.call(props2, nextDate);
    }
    function onConfirmClick() {
      var nextValue = formatDate(inputValue.value, {
        format: formatRef.value.format
      });
      if (nextValue) {
        onChange === null || onChange === void 0 ? void 0 : onChange(formatDate(inputValue.value, {
          format: formatRef.value.format,
          targetFormat: formatRef.value.valueType
        }), {
          dayjsValue: parseToDayjs(inputValue.value, formatRef.value.format),
          trigger: "confirm"
        });
      } else {
        inputValue.value = formatDate(value.value, {
          format: formatRef.value.format
        });
      }
      popupVisible.value = false;
    }
    function onPresetClick(presetValue) {
      var presetVal = isFunction(presetValue) ? presetValue() : presetValue;
      onChange === null || onChange === void 0 ? void 0 : onChange(formatDate(presetVal, {
        format: formatRef.value.format,
        targetFormat: formatRef.value.valueType
      }), {
        dayjsValue: parseToDayjs(presetVal, formatRef.value.format),
        trigger: "preset"
      });
      popupVisible.value = false;
    }
    function onYearChange(nextYear) {
      year.value = nextYear;
    }
    function onMonthChange(nextMonth) {
      month.value = nextMonth;
    }
    var panelProps = computed(function () {
      return {
        value: cacheValue.value,
        year: year.value,
        month: month.value,
        format: formatRef.value.format,
        mode: props2.mode,
        presets: props2.presets,
        time: time.value,
        disableDate: props2.disableDate,
        firstDayOfWeek: props2.firstDayOfWeek,
        timePickerProps: props2.timePickerProps,
        enableTimePicker: props2.enableTimePicker,
        presetsPlacement: props2.presetsPlacement,
        popupVisible: popupVisible.value,
        onCellClick: onCellClick,
        onCellMouseEnter: onCellMouseEnter,
        onCellMouseLeave: onCellMouseLeave,
        onJumperClick: onJumperClick,
        onConfirmClick: onConfirmClick,
        onPresetClick: onPresetClick,
        onYearChange: onYearChange,
        onMonthChange: onMonthChange,
        onTimePickerChange: onTimePickerChange,
        onPanelClick: function onPanelClick() {
          var _inputRef$value, _inputRef$value$focus;
          return (_inputRef$value = inputRef.value) === null || _inputRef$value === void 0 ? void 0 : (_inputRef$value$focus = _inputRef$value.focus) === null || _inputRef$value$focus === void 0 ? void 0 : _inputRef$value$focus.call(_inputRef$value);
        }
      };
    });
    return function () {
      return createVNode("div", {
        "class": COMPONENT_NAME.value
      }, [createVNode(SelectInput, {
        "disabled": disabled.value,
        "value": inputValue.value,
        "status": props2.status,
        "tips": props2.tips,
        "clearable": props2.clearable,
        "popupProps": popupProps.value,
        "inputProps": inputProps.value,
        "popupVisible": popupVisible.value,
        "panel": function panel() {
          return createVNode(TSinglePanel, panelProps.value, null);
        }
      }, null)]);
    };
  }
});

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