/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

import { ref, computed, watchEffect } from '@vue/composition-api';
import dayjs from 'dayjs';
import { g as getDefaultFormat, f as formatDate, i as isValidDate, a as formatTime, p as parseToDayjs } from '../../../chunks/format.js';
import { usePrefixClass } from '../../../hooks/useConfig.js';
import useSingleValue from './use-single-value.js';
import { useConfig } from '../../config-provider/useConfig.js';
import '../../../chunks/dayjs.min-0f6442fd.js';
import '../../../chunks/_commonjsHelpers-fe796db6.js';
import '../../../chunks/log.js';
import '../../../chunks/utils.js';
import '../../../chunks/defineProperty-19da3d4c.js';
import '../../../chunks/_isIndex-8b06cfbb.js';
import '../../../chunks/isFunction-cb08cfdf.js';
import '../../../chunks/isObjectLike-51896b77.js';
import '../../../chunks/isSymbol-0ea4cedf.js';
import '../../config-provider/context.js';
import 'lodash/merge';
import 'lodash/mergeWith';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../../hooks/useVModel.js';
import 'lodash/kebabCase';

function useSingle(props, _ref) {
  let {
    emit
  } = _ref;
  const COMPONENT_NAME = usePrefixClass("date-picker");
  const {
    global
  } = useConfig("datePicker");
  const inputRef = ref();
  const {
    value,
    onChange,
    time,
    month,
    year,
    cacheValue
  } = useSingleValue(props);
  const formatRef = computed(() => getDefaultFormat({
    mode: props.mode,
    format: props.format,
    valueType: props.valueType,
    enableTimePicker: props.enableTimePicker
  }));
  const popupVisible = ref(false);
  const isHoverCell = ref(false);
  const inputValue = ref(formatDate(value.value, {
    format: formatRef.value.format
  }));
  const inputProps = 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(),
        trigger: "clear"
      });
      emit("clear", "", {
        dayjsValue: dayjs(),
        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 (!isValidDate(val, formatRef.value.format)) return;
      const newMonth = dayjs(val).month();
      const newYear = dayjs(val).year();
      const newTime = 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(),
          trigger: "enter"
        });
        popupVisible.value = false;
        return;
      }
      if (!isValidDate(val, formatRef.value.format) && !isValidDate(value.value, formatRef.value.format)) return;
      popupVisible.value = false;
      if (isValidDate(val, formatRef.value.format)) {
        onChange?.(formatDate(val, {
          format: formatRef.value.format,
          targetFormat: formatRef.value.valueType
        }), {
          dayjsValue: parseToDayjs(val, formatRef.value.format),
          trigger: "enter"
        });
      } else if (isValidDate(value.value, formatRef.value.format)) {
        inputValue.value = formatDate(value.value, {
          format: formatRef.value.format
        });
      } else {
        inputValue.value = "";
      }
    }
  }));
  const popupProps = 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;
    }
  }));
  watchEffect(() => {
    if (!value.value) {
      inputValue.value = "";
      return;
    }
    if (!isValidDate(value.value, formatRef.value.format)) return;
    inputValue.value = formatDate(value.value, {
      format: formatRef.value.format
    });
  });
  return {
    year,
    month,
    value,
    time,
    inputValue,
    popupVisible,
    inputProps,
    popupProps,
    inputRef,
    cacheValue,
    isHoverCell,
    onChange
  };
}

export { useSingle as default };
//# sourceMappingURL=use-single.js.map
