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

import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { toRefs, ref, computed, createVNode, mergeProps } from 'vue';
import isObject from 'lodash/isObject';
import pick from 'lodash/pick';
import { Input } from '../input/index.js';
import { Loading } from '../loading/index.js';
import { useTNodeJSX } from '../hooks/tnode.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import useDefaultValue from '../hooks/useDefaultValue.js';
import { useFormDisabled } from '../form/hooks.js';
import '../utils/withInstall.js';
import '../input/input.js';
import '@ldesign/icons-vue-next';
import 'lodash/isUndefined';
import '../hooks/useGlobalIcon.js';
import '../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import 'lodash/isFunction';
import 'lodash/isString';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-2e5d8948.js';
import 'dayjs';
import '../_chunks/dep-4580aabe.js';
import '../config-provider/type.js';
import '../input/props.js';
import '../input/useInput.js';
import '../form/const.js';
import '../hooks/index.js';
import '../hooks/icon.js';
import '../hooks/slot.js';
import '../hooks/useCommonClassName.js';
import '../hooks/useDestroyOnClose.js';
import '../hooks/useKeepAnimation.js';
import '../hooks/useRipple.js';
import '../utils/set-style.js';
import '../hooks/useVirtualScroll.js';
import '../hooks/useVModel.js';
import 'lodash/kebabCase';
import 'lodash/camelCase';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import '../input/useLengthLimit.js';
import 'lodash/isNumber';
import '../_common/js/log/index.js';
import '../_common/js/log/log.js';
import '../_common/js/utils/helper.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/isNull';
import '../input/useInputEventHandler.js';
import '../input/useInputWidth.js';
import '../input/input-group.js';
import '../input/input-group-props.js';
import '../input/style';
import '../input/type.js';
import '../loading/directive.js';
import '../loading/plugin.js';
import '../utils/dom.js';
import '../utils/easing.js';
import '../loading/loading.js';
import '../hooks/useTeleport.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/set-style.js';
import '../loading/style';
import '../loading/type.js';

var _excluded = ["tips"];
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; }
var COMMON_PROPERTIES = ["status", "clearable", "disabled", "label", "placeholder", "readonly", "suffix", "suffixIcon", "onPaste", "onMouseenter", "onMouseleave"];
var DEFAULT_KEYS = {
  label: "label",
  value: "value",
  children: "children"
};
function getInputValue(value, keys) {
  var iKeys = _objectSpread(_objectSpread({}, DEFAULT_KEYS), keys);
  return isObject(value) ? value[iKeys.label] : value;
}
function useSingle(props, context) {
  var _props$defaultInputVa;
  var _toRefs = toRefs(props),
    value = _toRefs.value,
    keys = _toRefs.keys,
    propsInputValue = _toRefs.inputValue;
  var classPrefix = usePrefixClass();
  var inputRef = ref();
  var _useDefaultValue = useDefaultValue(propsInputValue, (_props$defaultInputVa = props.defaultInputValue) !== null && _props$defaultInputVa !== void 0 ? _props$defaultInputVa : "", props.onInputChange, "inputValue"),
    _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
    inputValue = _useDefaultValue2[0],
    setInputValue = _useDefaultValue2[1];
  var renderTNode = useTNodeJSX();
  var disable = useFormDisabled();
  var commonInputProps = computed(function () {
    return _objectSpread(_objectSpread({}, pick(props, COMMON_PROPERTIES)), {}, {
      disabled: disable.value
    });
  });
  var onInnerClear = function onInnerClear(context2) {
    var _context2$e, _props$onClear;
    context2 === null || context2 === void 0 ? void 0 : (_context2$e = context2.e) === null || _context2$e === void 0 ? void 0 : _context2$e.stopPropagation();
    (_props$onClear = props.onClear) === null || _props$onClear === void 0 ? void 0 : _props$onClear.call(props, context2);
    setInputValue("", {
      trigger: "clear"
    });
  };
  var onInnerInputChange = function onInnerInputChange(value2, context2) {
    if (props.allowInput) setInputValue(value2, _objectSpread(_objectSpread({}, context2), {}, {
      trigger: context2.trigger || "input"
    }));
  };
  var renderSelectSingle = function renderSelectSingle(popupVisible) {
    var _context$attrs, _ref;
    var singleValueDisplay = renderTNode("valueDisplay");
    var displayedValue = popupVisible && props.allowInput ? inputValue.value : getInputValue(value.value, keys.value);
    var prefixContent = [renderTNode("label"), singleValueDisplay];
    var inputProps = _objectSpread(_objectSpread({}, commonInputProps.value), {}, {
      value: singleValueDisplay ? void 0 : displayedValue,
      label: prefixContent.length ? function () {
        return prefixContent;
      } : void 0,
      autoWidth: props.autoWidth,
      readonly: !props.allowInput || props.readonly,
      placeholder: singleValueDisplay ? "" : props.placeholder,
      suffixIcon: !disable.value && props.loading ? function () {
        return createVNode(Loading, {
          "loading": true,
          "size": "small"
        }, null);
      } : props.suffixIcon,
      showClearIconOnEmpty: Boolean(props.clearable && (inputValue.value || displayedValue) && !disable.value && !props.readonly),
      allowTriggerBlur: props.allowInput && !props.readonly
    }, props.inputProps);
    var _context$slots = context.slots,
      tips = _context$slots.tips,
      slots = _objectWithoutProperties(_context$slots, _excluded);
    return createVNode(Input, mergeProps({
      "ref": "inputRef",
      "style": (_context$attrs = context.attrs) === null || _context$attrs === void 0 ? void 0 : _context$attrs.style
    }, _objectSpread({
      onChange: onInnerInputChange,
      onClear: onInnerClear,
      onBlur: function onBlur(val, context2) {
        var _props$onBlur;
        (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 ? void 0 : _props$onBlur.call(props, value.value, _objectSpread(_objectSpread({}, context2), {}, {
          inputValue: val
        }));
      },
      onEnter: function onEnter(val, context2) {
        var _props$onEnter;
        (_props$onEnter = props.onEnter) === null || _props$onEnter === void 0 ? void 0 : _props$onEnter.call(props, value.value, _objectSpread(_objectSpread({}, context2), {}, {
          inputValue: val
        }));
      },
      onFocus: function onFocus(val, context2) {
        var _props$onFocus;
        (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 ? void 0 : _props$onFocus.call(props, value.value, _objectSpread(_objectSpread({}, context2), {}, {
          inputValue: val
        }));
      }
    }, inputProps), {
      "inputClass": (_ref = {}, _defineProperty(_ref, "".concat(classPrefix.value, "-input--focused"), popupVisible), _defineProperty(_ref, "".concat(classPrefix.value, "-is-focused"), popupVisible), _ref)
    }), slots);
  };
  return {
    inputRef: inputRef,
    commonInputProps: commonInputProps,
    onInnerClear: onInnerClear,
    renderSelectSingle: renderSelectSingle
  };
}

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