/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 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 hooks_useConfig = require('../../../hooks/useConfig.js');
var components_datePicker_hooks_useSingleValue = require('./use-single-value.js');
var components_configProvider_useConfig = require('../../config-provider/useConfig.js');
require('../../../chunks/dayjs.min-0f6442fd.js');
require('../../../chunks/_commonjsHelpers-fe796db6.js');
require('../../../chunks/log.js');
require('../../../chunks/utils.js');
require('../../../chunks/defineProperty-19da3d4c.js');
require('../../../chunks/_isIndex-8b06cfbb.js');
require('../../../chunks/isFunction-cb08cfdf.js');
require('../../../chunks/isObjectLike-51896b77.js');
require('../../../chunks/isSymbol-0ea4cedf.js');
require('../../config-provider/context.js');
require('lodash/merge');
require('lodash/mergeWith');
require('../../../chunks/zh_CN.js');
require('../../../hooks/useVModel.js');
require('lodash/kebabCase');

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

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

function useSingle(props, _ref) {
  let {
    emit
  } = _ref;
  const COMPONENT_NAME = hooks_useConfig.usePrefixClass("date-picker");
  const {
    global
  } = components_configProvider_useConfig.useConfig("datePicker");
  const inputRef = VueCompositionAPI.ref();
  const {
    value,
    onChange,
    time,
    month,
    year,
    cacheValue
  } = components_datePicker_hooks_useSingleValue["default"](props);
  const formatRef = VueCompositionAPI.computed(() => format.getDefaultFormat({
    mode: props.mode,
    format: props.format,
    valueType: props.valueType,
    enableTimePicker: props.enableTimePicker
  }));
  const popupVisible = VueCompositionAPI.ref(false);
  const isHoverCell = VueCompositionAPI.ref(false);
  const inputValue = VueCompositionAPI.ref(format.formatDate(value.value, {
    format: formatRef.value.format
  }));
  const inputProps = VueCompositionAPI.computed(() => ({
    ...props.inputProps,
    ref: inputRef,
    prefixIcon: props.prefixIcon,
    placeholder: props.placeholder || global.value.placeholder[props.mode],
    class: [{
      [`${COMPONENT_NAME.value}__input--placeholder`]: isHoverCell.value
    }],
    onClear: context => {
      context?.e?.stopPropagation();
      popupVisible.value = false;
      onChange?.("", {
        dayjsValue: dayjs__default["default"](),
        trigger: "clear"
      });
      emit("clear", "", {
        dayjsValue: dayjs__default["default"](),
        trigger: "clear"
      });
    },
    onBlur: (val, context) => {
      props.onBlur?.({
        value: val,
        e: context.e
      });
      emit("blur", {
        value: val,
        e: context.e
      });
    },
    onFocus: (_, _ref2) => {
      let {
        e
      } = _ref2;
      props.onFocus?.({
        value: value.value,
        e
      });
      emit("focus", {
        value: value.value,
        e
      });
    },
    onChange: val => {
      inputValue.value = val;
      if (!format.isValidDate(val, formatRef.value.format)) return;
      const newMonth = dayjs__default["default"](val).month();
      const newYear = dayjs__default["default"](val).year();
      const newTime = format.formatTime(val, formatRef.value.timeFormat);
      !Number.isNaN(newYear) && (year.value = newYear);
      !Number.isNaN(newMonth) && (month.value = newMonth);
      !Number.isNaN(newTime) && (time.value = newTime);
    },
    onEnter: val => {
      if (!val) {
        onChange?.("", {
          dayjsValue: dayjs__default["default"](),
          trigger: "enter"
        });
        popupVisible.value = false;
        return;
      }
      if (!format.isValidDate(val, formatRef.value.format) && !format.isValidDate(value.value, formatRef.value.format)) return;
      popupVisible.value = false;
      if (format.isValidDate(val, formatRef.value.format)) {
        onChange?.(format.formatDate(val, {
          format: formatRef.value.format,
          targetFormat: formatRef.value.valueType
        }), {
          dayjsValue: format.parseToDayjs(val, formatRef.value.format),
          trigger: "enter"
        });
      } else if (format.isValidDate(value.value, formatRef.value.format)) {
        inputValue.value = format.formatDate(value.value, {
          format: formatRef.value.format
        });
      } else {
        inputValue.value = "";
      }
    }
  }));
  const popupProps = VueCompositionAPI.computed(() => ({
    expandAnimation: true,
    ...props.popupProps,
    overlayInnerStyle: props.popupProps?.overlayInnerStyle ?? {
      width: "auto"
    },
    overlayClassName: [props.popupProps?.overlayClassName, `${COMPONENT_NAME.value}__panel-container`],
    onVisibleChange: (visible, context) => {
      props.popupProps?.onVisibleChange?.(visible, context);
      if (context.trigger === "trigger-element-click") {
        popupVisible.value = true;
        return;
      }
      popupVisible.value = visible;
    }
  }));
  VueCompositionAPI.watchEffect(() => {
    if (!value.value) {
      inputValue.value = "";
      return;
    }
    if (!format.isValidDate(value.value, formatRef.value.format)) return;
    inputValue.value = format.formatDate(value.value, {
      format: formatRef.value.format
    });
  });
  return {
    year,
    month,
    value,
    time,
    inputValue,
    popupVisible,
    inputProps,
    popupProps,
    inputRef,
    cacheValue,
    isHoverCell,
    onChange
  };
}

exports["default"] = useSingle;
//# sourceMappingURL=use-single.js.map
