/**
 * 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 iconsVue = require('@ldesign/icons-vue');
var format = require('../../chunks/format.js');
var utils = require('../../chunks/utils.js');
var hooks_useConfig = require('../../hooks/use-config.js');
var hooks_useGlobalIcon = require('../../hooks/use-global-icon.js');
var components_selectInput_index = require('../select-input/index.js');
var hooks_useFormDisabled = require('../../hooks/use-form-disabled.js');
var components_datePicker_hooks_useSingle = require('./hooks/use-single.js');
var components_datePicker_props = require('./props.js');
var components_datePicker_panel_singlePanel = require('./panel/single-panel.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('../config-provider/useConfig.js');
require('../config-provider/context.js');
require('lodash');
require('../../chunks/zh_CN.js');
require('../../utils/with-install.js');
require('vue');
require('../../config.js');
require('../select-input/select-input.js');
require('../../chunks/helper.js');
require('../popup/index.js');
require('../../utils/map-props.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../../chunks/set-style.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../../utils/render-node.js');
require('../../utils/helper.js');
require('lodash/camelCase');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../utils/mixins.js');
require('../popup/props.js');
require('../popup/container.js');
require('../select-input/props.js');
require('../select-input/hooks/use-single.js');
require('lodash/isObject');
require('lodash/pick');
require('../input/index.js');
require('../input/input.js');
require('lodash/kebabCase');
require('../../chunks/helper2.js');
require('../../utils/event.js');
require('../input/props.js');
require('../input/group.js');
require('../loading/index.js');
require('../loading/loading.js');
require('../../utils/transfer-dom.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../loading/plugin.js');
require('../../hooks/lnode.js');
require('lodash/isEmpty');
require('lodash/isFunction');
require('../../hooks/render-lnode.js');
require('../../hooks/use-default-value.js');
require('../select-input/hooks/use-multiple.js');
require('../tag-input/index.js');
require('../tag-input/tag-input.js');
require('../tag-input/props.js');
require('../tag-input/hooks/use-tag-scroll.js');
require('../tag-input/hooks/use-tag-list.js');
require('../tag/index.js');
require('../tag/tag.js');
require('../tag/props.js');
require('../tag/check-tag.js');
require('../../hooks/use-vmodel.js');
require('../tag-input/hooks/use-hover.js');
require('../tag-input/hooks/use-drag-sorter.js');
require('../select-input/hooks/use-overlay-innerstyle.js');
require('./hooks/use-single-value.js');
require('./hooks/use-table-data.js');
require('./hooks/use-disabled-date.js');
require('../../chunks/_commonjsHelpers.js');
require('./panel/extra-content.js');
require('./base/Footer.js');
require('../button/index.js');
require('../button/button.js');
require('../../utils/ripple.js');
require('../button/props.js');
require('./panel/panel-content.js');
require('./base/Header.js');
require('../jumper/jumper.js');
require('../jumper/props.js');
require('../select/select.js');
require('lodash/debounce');
require('lodash/get');
require('lodash/cloneDeep');
require('lodash/isArray');
require('../common/fake-arrow.js');
require('../select/option.js');
require('../checkbox/index.js');
require('../checkbox/group.js');
require('lodash/intersection');
require('../checkbox/checkbox.js');
require('../checkbox/props.js');
require('../../hooks/use-lazy-load.js');
require('../../chunks/observe.js');
require('../../hooks/use-common-classname.js');
require('../select/utils.js');
require('../select/props.js');
require('../select/select-panel.js');
require('../../hooks/use-virtual-scroll.js');
require('../select/hooks/use-select-options.js');
require('./base/Table.js');
require('./base/Cell.js');
require('../time-picker/panel/time-picker-panel.js');
require('../../chunks/single-panel.js');
require('lodash/range');
require('lodash/padStart');
require('../time-picker/panel/props.js');
require('../time-picker/props.js');

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

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

var _DatePicker = VueCompositionAPI.defineComponent({
  name: "LDatePicker",
  props: {
    ...components_datePicker_props.DatePickerProps
  },
  setup(props2, _ref) {
    let {
      emit
    } = _ref;
    const COMPONENT_NAME = hooks_useConfig.usePrefixClass("date-picker");
    const {
      CalendarIcon
    } = hooks_useGlobalIcon.useGlobalIcon({
      CalendarIcon: iconsVue.CalendarIcon
    });
    const {
      inputValue,
      popupVisible,
      inputProps: datePickerInputProps,
      popupProps: datePickerPopupProps,
      isHoverCell,
      cacheValue,
      value,
      year,
      month,
      time,
      inputRef,
      onChange
    } = components_datePicker_hooks_useSingle["default"](props2, {
      emit
    });
    const formatRef = VueCompositionAPI.computed(() => format.getDefaultFormat({
      mode: props2.mode,
      format: props2.format,
      valueType: props2.valueType,
      enableTimePicker: props2.enableTimePicker
    }));
    const {
      formDisabled
    } = hooks_useFormDisabled["default"]();
    const isDisabled = VueCompositionAPI.computed(() => formDisabled.value || props2.disabled);
    VueCompositionAPI.watch(popupVisible, visible => {
      cacheValue.value = format.formatDate(value.value, {
        format: formatRef.value.format
      });
      inputValue.value = format.formatDate(value.value, {
        format: formatRef.value.format
      });
      if (visible) {
        year.value = format.parseToDayjs(value.value, formatRef.value.format).year();
        month.value = format.parseToDayjs(value.value, formatRef.value.format).month();
        time.value = format.formatTime(value.value, formatRef.value.timeFormat);
      } else {
        isHoverCell.value = false;
      }
    });
    function onCellMouseEnter(date) {
      isHoverCell.value = true;
      inputValue.value = format.formatDate(date, {
        format: formatRef.value.format
      });
    }
    function onCellMouseLeave() {
      isHoverCell.value = false;
      inputValue.value = format.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 = format.formatDate(date, {
          format: formatRef.value.format
        });
      } else {
        onChange === null || onChange === void 0 ? void 0 : onChange(format.formatDate(date, {
          format: formatRef.value.format,
          targetFormat: formatRef.value.valueType
        }), {
          dayjsValue: format.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);
      emit("pick", date);
    }
    function onJumperClick(_ref2) {
      let {
        trigger
      } = _ref2;
      const monthCountMap = {
        date: 1,
        week: 1,
        month: 12,
        quarter: 12,
        year: 120
      };
      const monthCount = monthCountMap[props2.mode] || 0;
      const current = new Date(year.value, month.value);
      let next = null;
      if (trigger === "prev") next = utils.subtractMonth(current, monthCount);else if (trigger === "current") next = new Date();else if (trigger === "next") next = utils.addMonth(current, monthCount);
      const nextYear = next.getFullYear();
      const nextMonth = next.getMonth();
      year.value = nextYear;
      month.value = nextMonth;
    }
    function onTimePickerChange(val) {
      var _props2$onPick2;
      time.value = val;
      const {
        hours,
        minutes,
        seconds,
        milliseconds,
        meridiem
      } = utils.extractTimeObj(val);
      let nextHours = hours;
      if (/am/i.test(meridiem) && nextHours === 12) nextHours -= 12;
      if (/pm/i.test(meridiem) && nextHours < 12) nextHours += 12;
      const currentDate = !dayjs__default["default"](inputValue.value, formatRef.value.format).isValid() ? dayjs__default["default"]() : dayjs__default["default"](inputValue.value, formatRef.value.format);
      const nextDate = currentDate.hour(nextHours).minute(minutes).second(seconds).millisecond(milliseconds).toDate();
      inputValue.value = format.formatDate(nextDate, {
        format: formatRef.value.format
      });
      cacheValue.value = format.formatDate(nextDate, {
        format: formatRef.value.format
      });
      (_props2$onPick2 = props2.onPick) === null || _props2$onPick2 === void 0 ? void 0 : _props2$onPick2.call(props2, nextDate);
    }
    function onConfirmClick() {
      const nextValue = format.formatDate(inputValue.value, {
        format: formatRef.value.format
      });
      if (nextValue) {
        onChange === null || onChange === void 0 ? void 0 : onChange(format.formatDate(inputValue.value, {
          format: formatRef.value.format,
          targetFormat: formatRef.value.valueType
        }), {
          dayjsValue: format.parseToDayjs(inputValue.value, formatRef.value.format),
          trigger: "confirm"
        });
      } else {
        inputValue.value = format.formatDate(value.value, {
          format: formatRef.value.format
        });
      }
      popupVisible.value = false;
    }
    function onPresetClick(presetValue) {
      const presetVal = typeof presetValue === "function" ? presetValue() : presetValue;
      onChange === null || onChange === void 0 ? void 0 : onChange(format.formatDate(presetVal, {
        format: formatRef.value.format,
        targetFormat: formatRef.value.valueType
      }), {
        dayjsValue: format.parseToDayjs(presetVal, formatRef.value.format),
        trigger: "preset"
      });
      popupVisible.value = false;
    }
    function onYearChange(nextYear) {
      year.value = nextYear;
    }
    function onMonthChange(nextMonth) {
      month.value = nextMonth;
    }
    const panelProps = VueCompositionAPI.computed(() => ({
      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,
      onCellMouseEnter,
      onCellMouseLeave,
      onJumperClick,
      onConfirmClick,
      onPresetClick,
      onYearChange,
      onMonthChange,
      onTimePickerChange,
      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 {
      COMPONENT_NAME,
      inputValue,
      datePickerPopupProps,
      datePickerInputProps,
      popupVisible,
      panelProps,
      isDisabled,
      CalendarIcon
    };
  },
  render() {
    const h = arguments[0];
    const {
      COMPONENT_NAME,
      inputValue,
      datePickerPopupProps,
      datePickerInputProps,
      popupVisible,
      panelProps,
      isDisabled,
      CalendarIcon
    } = this;
    const renderSuffixIcon = () => {
      if (this.suffixIcon) return this.suffixIcon;
      if (this.$scopedSlots.suffixIcon) return this.$scopedSlots.suffixIcon;
      if (this.$scopedSlots["suffix-icon"]) return this.$scopedSlots["suffix-icon"];
      return () => h(CalendarIcon);
    };
    return h("div", {
      "class": COMPONENT_NAME
    }, [h(components_selectInput_index.SelectInput, {
      "attrs": {
        "disabled": isDisabled,
        "value": inputValue,
        "status": this.status,
        "tips": this.tips,
        "popupProps": datePickerPopupProps,
        "inputProps": {
          suffixIcon: renderSuffixIcon(),
          ...datePickerInputProps
        },
        "popupVisible": popupVisible,
        "clearable": this.clearable,
        "allowInput": this.allowInput,
        "panel": () => h(components_datePicker_panel_singlePanel["default"], {
          "props": {
            ...panelProps
          }
        })
      }
    })]);
  }
});

exports["default"] = _DatePicker;
//# sourceMappingURL=date-picker.js.map
