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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var VueCompositionAPI = require('@vue/composition-api');
var dayjs = require('dayjs');
var hooks_useConfig = require('../../hooks/useConfig.js');
var _common_js_datePicker_format = require('../../_common/js/date-picker/format.js');
var datePicker_hooks_useSingleValue = require('./useSingleValue.js');
var configProvider_useConfig = require('../../config-provider/useConfig.js');
require('../../config-provider/context.js');
require('lodash/merge');
require('lodash/mergeWith');
require('../../_common/js/global-config/default-config.js');
require('../../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../../_chunks/dep-7c884501.js');
require('lodash/isString');
require('../../_common/js/log/log.js');
require('../../_common/js/date-picker/utils.js');
require('@babel/runtime/helpers/slicedToArray');
require('@babel/runtime/helpers/objectDestructuringEmpty');
require('@babel/runtime/helpers/extends');
require('lodash/isFunction');
require('../../_chunks/dep-6e3fbdd7.js');
require('lodash/chunk');
require('../../hooks/useVModel.js');
require('lodash/kebabCase');

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

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

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__default["default"](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 = hooks_useConfig.usePrefixClass("date-picker");
  var _useConfig = configProvider_useConfig.useConfig("datePicker"),
    global = _useConfig.global;
  var inputRef = VueCompositionAPI.ref();
  var _useSingleValue = datePicker_hooks_useSingleValue["default"](props),
    value = _useSingleValue.value,
    onChange = _useSingleValue.onChange,
    time = _useSingleValue.time,
    month = _useSingleValue.month,
    year = _useSingleValue.year,
    cacheValue = _useSingleValue.cacheValue;
  var formatRef = VueCompositionAPI.computed(function () {
    return _common_js_datePicker_format.getDefaultFormat({
      mode: props.mode,
      format: props.format,
      valueType: props.valueType,
      enableTimePicker: props.enableTimePicker
    });
  });
  var popupVisible = VueCompositionAPI.ref(false);
  var isHoverCell = VueCompositionAPI.ref(false);
  var inputValue = VueCompositionAPI.ref(_common_js_datePicker_format.formatDate(value.value, {
    format: formatRef.value.format
  }));
  var inputProps = VueCompositionAPI.computed(function () {
    return _objectSpread(_objectSpread({}, props.inputProps), {}, {
      ref: inputRef,
      size: props.size,
      prefixIcon: props.prefixIcon,
      placeholder: props.placeholder || global.value.placeholder[props.mode],
      "class": [_defineProperty__default["default"]({}, "".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__default["default"](),
          trigger: "clear"
        });
        emit("clear", "", {
          dayjsValue: dayjs__default["default"](),
          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 (!_common_js_datePicker_format.isValidDate(val, formatRef.value.format)) return;
        cacheValue.value = val;
        var newMonth = _common_js_datePicker_format.parseToDayjs(val, formatRef.value.format).month();
        var newYear = _common_js_datePicker_format.parseToDayjs(val, formatRef.value.format).year();
        var newTime = _common_js_datePicker_format.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 === null || onChange === void 0 ? void 0 : onChange("", {
            dayjsValue: dayjs__default["default"](),
            trigger: "enter"
          });
          popupVisible.value = false;
          return;
        }
        if (!_common_js_datePicker_format.isValidDate(val, formatRef.value.format) && !_common_js_datePicker_format.isValidDate(value.value, formatRef.value.format)) return;
        popupVisible.value = false;
        if (_common_js_datePicker_format.isValidDate(val, formatRef.value.format)) {
          onChange === null || onChange === void 0 ? void 0 : onChange(_common_js_datePicker_format.formatDate(val, {
            format: formatRef.value.format,
            targetFormat: formatRef.value.valueType
          }), {
            dayjsValue: _common_js_datePicker_format.parseToDayjs(val, formatRef.value.format),
            trigger: "enter"
          });
        } else if (_common_js_datePicker_format.isValidDate(value.value, formatRef.value.format)) {
          inputValue.value = _common_js_datePicker_format.formatDate(value.value, {
            format: formatRef.value.format
          });
        } else {
          inputValue.value = "";
        }
      }
    });
  });
  var popupProps = VueCompositionAPI.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) {
        var _props$popupProps3, _props$popupProps3$on;
        (_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;
      }
    });
  });
  VueCompositionAPI.watch(value, function (value2) {
    if (!value2) {
      inputValue.value = "";
      return;
    }
    if (!_common_js_datePicker_format.isValidDate(value2, formatRef.value.format)) return;
    inputValue.value = _common_js_datePicker_format.formatDate(value2, {
      format: formatRef.value.format
    });
  }, {
    immediate: true
  });
  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
  };
}

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