/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { ref, computed, watchEffect } from '@vue/composition-api';
import dayjs from 'dayjs';
import { g as getDefaultFormat, a as formatDate, i as isValidDate, f as formatTime, p as parseToDayjs } from '../../../_chunks/dep-7af607c9.js';
import { usePrefixClass } from '../../../hooks/use-config.js';
import useSingleValue from './use-single-value.js';
import { useConfig } from '../../config-provider/useConfig.js';
import '../../../_chunks/dep-d3027203.js';
import '@babel/runtime/helpers/typeof';
import '../../../_chunks/dep-53d4956d.js';
import '../../../_chunks/dep-0f952684.js';
import '@babel/runtime/helpers/slicedToArray';
import '../../../_chunks/dep-33b15d9d.js';
import '../../../_chunks/dep-64cbbe92.js';
import '../../../_chunks/dep-5c5da9dd.js';
import '../../../_chunks/dep-a65fe6e9.js';
import '../../config-provider/context.js';
import 'lodash';
import '../../../_chunks/dep-a0f2090c.js';
import '../../../hooks/use-vmodel.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; }
function useSingle(props, _ref) {
  var emit = _ref.emit;
  var COMPONENT_NAME = usePrefixClass("date-picker");

  var _useConfig = useConfig("datePicker"),
      global = _useConfig.global;

  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,
      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), {}, {
      ref: inputRef,
      prefixIcon: props.prefixIcon,
      placeholder: props.placeholder || global.value.placeholder[props.mode],
      "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"
        });
        emit("clear", "", {
          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
        });
        emit("blur", {
          value: val,
          e: context.e
        });
      },
      onFocus: function onFocus(_, _ref3) {
        var _props$onFocus;

        var e = _ref3.e;
        (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 ? void 0 : _props$onFocus.call(props, {
          value: value.value,
          e: e
        });
        emit("focus", {
          value: value.value,
          e: e
        });
      },
      onChange: function onChange(val) {
        inputValue.value = val;
        if (!isValidDate(val, formatRef.value.format)) return;
        var newMonth = dayjs(val).month();
        var newYear = dayjs(val).year();
        var 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: function onEnter(val) {
        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
          }), {
            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), {}, {
      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) {
        if (context.trigger === "trigger-element-click") {
          popupVisible.value = true;
          return;
        }

        if (!visible) {
          isHoverCell.value = false;
          inputValue.value = formatDate(value.value, {
            format: formatRef.value.format
          });
        }

        popupVisible.value = visible;
      }
    });
  });
  watchEffect(function () {
    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: 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=use-single.js.map
