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

import { _ as _defineProperty } from '../../_chunks/dep-eb20e23f.mjs';
import { ref, computed, createVNode, watch } from 'vue';
import { CalendarIcon } from '@ldesign/icons-vue-next';
import { d as dayjs } from '../../_chunks/dep-6b4aa943.mjs';
import { useTNodeJSX } from '../../hooks/tnode.mjs';
import { useFormDisabled } from '../../form/hooks.mjs';
import { usePrefixClass } from '../../hooks/useConfig.mjs';
import { useGlobalIcon } from '../../hooks/useGlobalIcon.mjs';
import { getDefaultFormat, formatDate, isValidDate, parseToDayjs, formatTime } from '../../_common/js/date-picker/format.mjs';
import useSingleValue from './useSingleValue.mjs';
import { useConfig } from '../../config-provider/useConfig.mjs';
import '../../_chunks/dep-b4041200.mjs';
import '../../_chunks/dep-a0b4a044.mjs';
import '../../_chunks/dep-6b22dd27.mjs';
import '../../_chunks/dep-c959a6fb.mjs';
import '../../_chunks/dep-0f0c89ff.mjs';
import '../../_chunks/dep-f6649a46.mjs';
import '../../_chunks/dep-37ba476d.mjs';
import '../../_chunks/dep-e3b14d70.mjs';
import '../../_chunks/dep-66d6733b.mjs';
import '../../_chunks/dep-be27d184.mjs';
import '../../_chunks/dep-c31e1d9e.mjs';
import '../../_chunks/dep-1fe0ad42.mjs';
import '../../_chunks/dep-d54f2690.mjs';
import '../../_chunks/dep-59c8420d.mjs';
import '../../_chunks/dep-74848b92.mjs';
import '../../_chunks/dep-c4e1a112.mjs';
import '../../utils/render-tnode.mjs';
import '../../_chunks/dep-bd03960e.mjs';
import '../../_chunks/dep-44f056cd.mjs';
import '../../_chunks/dep-a9c8a5d5.mjs';
import '../../_chunks/dep-10085c61.mjs';
import '../../_chunks/dep-1b058de3.mjs';
import '../../_chunks/dep-39cc276d.mjs';
import '../../_chunks/dep-8f777748.mjs';
import '../../_chunks/dep-05fa17c9.mjs';
import '../../_chunks/dep-92e7085e.mjs';
import '../../_chunks/dep-1cf2b129.mjs';
import '../../_chunks/dep-44417261.mjs';
import '../../_chunks/dep-bf74f1c2.mjs';
import '../../_chunks/dep-79821ff2.mjs';
import '../../_chunks/dep-0bfbf721.mjs';
import '../../_chunks/dep-ae357f26.mjs';
import '../../_chunks/dep-352691c2.mjs';
import '../../_chunks/dep-2cfd5b3a.mjs';
import '../../_chunks/dep-c852a61a.mjs';
import '../../_chunks/dep-23a0f453.mjs';
import '../../_chunks/dep-99920870.mjs';
import '../../_chunks/dep-c7e9ce16.mjs';
import '../../_chunks/dep-3afa94cf.mjs';
import '../../_chunks/dep-ba3c0b2e.mjs';
import '../../_chunks/dep-ee57d235.mjs';
import '../../_common/js/global-config/default-config.mjs';
import '../../_common/js/global-config/locale/zh_CN.mjs';
import '../../_chunks/dep-d52291a9.mjs';
import '../../config-provider/type.mjs';
import '../../_common/js/log/index.mjs';
import '../../_common/js/log/log.mjs';
import '../../_common/js/date-picker/utils.mjs';
import '../../_chunks/dep-55846619.mjs';
import '../../_chunks/dep-cfcedf47.mjs';
import '../../_chunks/dep-47763a99.mjs';
import '../../_chunks/dep-6205fc47.mjs';
import '../../hooks/useVModel.mjs';

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; }
function useSingle(props) {
  var COMPONENT_NAME = usePrefixClass("date-picker");
  var _useConfig = useConfig("datePicker"),
    globalConfig = _useConfig.globalConfig;
  var _useGlobalIcon = useGlobalIcon({
      CalendarIcon: CalendarIcon
    }),
    CalendarIcon$1 = _useGlobalIcon.CalendarIcon;
  var disabled = useFormDisabled();
  var renderTNodeJSX = useTNodeJSX();
  var inputRef = ref();
  var _useSingleValue = useSingleValue(props),
    value = _useSingleValue.value,
    onChange = _useSingleValue.onChange,
    time = _useSingleValue.time,
    month = _useSingleValue.month,
    year = _useSingleValue.year,
    cacheValue = _useSingleValue.cacheValue;
  var formatRef = computed(function () {
    return getDefaultFormat({
      mode: props.mode,
      format: props.format,
      valueType: props.valueType,
      enableTimePicker: props.enableTimePicker
    });
  });
  var popupVisible = ref(false);
  var isHoverCell = ref(false);
  var inputValue = ref(formatDate(value.value, {
    format: formatRef.value.format
  }));
  var inputProps = computed(function () {
    return _objectSpread(_objectSpread({}, props.inputProps), {}, {
      size: props.size,
      ref: inputRef,
      prefixIcon: props.prefixIcon && function () {
        return renderTNodeJSX("prefixIcon");
      },
      readonly: !props.allowInput,
      placeholder: props.placeholder || globalConfig.value.placeholder[props.mode],
      suffixIcon: function suffixIcon() {
        return renderTNodeJSX("suffixIcon") || createVNode(CalendarIcon$1, null, null);
      },
      "class": [_defineProperty({}, "".concat(COMPONENT_NAME.value, "__input--placeholder"), isHoverCell.value)],
      onClear: function onClear(context) {
        var _context$e;
        context === null || context === void 0 ? void 0 : (_context$e = context.e) === null || _context$e === void 0 ? void 0 : _context$e.stopPropagation();
        popupVisible.value = false;
        onChange === null || onChange === void 0 ? void 0 : onChange("", {
          dayjsValue: dayjs(),
          trigger: "clear"
        });
      },
      onBlur: function onBlur(val, context) {
        var _props$onBlur;
        (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 ? void 0 : _props$onBlur.call(props, {
          value: val,
          e: context.e
        });
      },
      onFocus: function onFocus(_, _ref2) {
        var _props$onFocus;
        var e = _ref2.e;
        (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 ? void 0 : _props$onFocus.call(props, {
          value: value.value,
          e: e
        });
      },
      onChange: function onChange(val) {
        inputValue.value = val;
        if (!isValidDate(val, formatRef.value.format)) return;
        cacheValue.value = val;
        var newMonth = parseToDayjs(val, formatRef.value.format).month();
        var newYear = parseToDayjs(val, formatRef.value.format).year();
        var newTime = formatTime(val, formatRef.value.format, formatRef.value.timeFormat, props.defaultTime);
        !Number.isNaN(newYear) && (year.value = newYear);
        !Number.isNaN(newMonth) && (month.value = newMonth);
        !Number.isNaN(newTime) && (time.value = newTime);
      },
      onEnter: function 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 === null || onChange === void 0 ? void 0 : 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 = "";
        }
      }
    });
  });
  var popupProps = computed(function () {
    var _props$popupProps$ove, _props$popupProps, _props$popupProps2;
    return _objectSpread(_objectSpread({
      expandAnimation: true
    }, props.popupProps), {}, {
      disabled: disabled.value,
      overlayInnerStyle: (_props$popupProps$ove = (_props$popupProps = props.popupProps) === null || _props$popupProps === void 0 ? void 0 : _props$popupProps.overlayInnerStyle) !== null && _props$popupProps$ove !== void 0 ? _props$popupProps$ove : {
        width: "auto"
      },
      overlayClassName: [(_props$popupProps2 = props.popupProps) === null || _props$popupProps2 === void 0 ? void 0 : _props$popupProps2.overlayClassName, "".concat(COMPONENT_NAME.value, "__panel-container")],
      onVisibleChange: function onVisibleChange(visible, context) {
        var _props$popupProps3, _props$popupProps3$on;
        if (disabled.value) return;
        (_props$popupProps3 = props.popupProps) === null || _props$popupProps3 === void 0 ? void 0 : (_props$popupProps3$on = _props$popupProps3.onVisibleChange) === null || _props$popupProps3$on === void 0 ? void 0 : _props$popupProps3$on.call(_props$popupProps3, visible, context);
        if (context.trigger === "trigger-element-click") {
          popupVisible.value = true;
          return;
        }
        popupVisible.value = visible;
      }
    });
  });
  watch(value, function (value2) {
    if (!value2) {
      inputValue.value = "";
      return;
    }
    if (!isValidDate(value2, formatRef.value.format)) return;
    inputValue.value = formatDate(value2, {
      format: formatRef.value.format
    });
  });
  return {
    year: year,
    month: month,
    value: value,
    time: time,
    inputValue: inputValue,
    popupVisible: popupVisible,
    inputProps: inputProps,
    popupProps: popupProps,
    inputRef: inputRef,
    cacheValue: cacheValue,
    isHoverCell: isHoverCell,
    onChange: onChange
  };
}

export { useSingle as default };
//# sourceMappingURL=useSingle.mjs.map
