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

import { h as helper } from '../_chunks/dep-c89aafbb.js';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { getCurrentInstance, toRefs, ref, computed } from '@vue/composition-api';
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 '../utils/map-props.js';
import 'vue';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import '../input/input.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/typeof';
import '@ldesign/icons-vue';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../_common/js/utils/helper.js';
import 'lodash/isString';
import 'lodash/isNull';
import 'lodash/isUndefined';
import 'lodash/isNumber';
import 'lodash/isArray';
import '../utils/helper.js';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-be599ec8.js';
import 'dayjs';
import '../_chunks/dep-67ff4010.js';
import '../config-provider/type.js';
import '../utils/mixins.js';
import '../utils/event.js';
import '../utils/render-tnode.js';
import '../_common/js/log/index.js';
import '../_common/js/log/log.js';
import '../input/props.js';
import '../input/input-group.js';
import '../input/style/css.js';
import '../input/type.js';
import '../loading/directive.js';
import 'lodash/mapKeys';
import 'lodash/isEqual';
import '../loading/plugin.js';
import '../config.js';
import '../utils/dom.js';
import 'raf';
import '../utils/easing.js';
import '../loading/loading.js';
import '../utils/transfer-dom.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/css.js';
import '../loading/type.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import '../hooks/render-tnode.js';
import '../config-provider/useConfig.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", "autofocus"];
var DEFAULT_KEYS = {
  label: "label",
  value: "value"
};
function getInputValue(value, keys) {
  var iKeys = keys || DEFAULT_KEYS;
  return isObject(value) ? value[iKeys.label] : value;
}
function useSingle(props, context) {
  var _props$defaultInputVa;
  var instance = getCurrentInstance();
  var _toRefs = toRefs(props),
    value = _toRefs.value,
    keys = _toRefs.keys,
    propsInputValue = _toRefs.inputValue;
  var classPrefix = usePrefixClass();
  var _useDefaultValue = useDefaultValue(propsInputValue, (_props$defaultInputVa = props.defaultInputValue) !== null && _props$defaultInputVa !== void 0 ? _props$defaultInputVa : "", props.onInputChange, "inputValue", "input-change"),
    _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
    inputValue = _useDefaultValue2[0],
    setInputValue = _useDefaultValue2[1];
  var inputRef = ref();
  var renderTNode = useTNodeJSX();
  var commonInputProps = computed(function () {
    return pick(props, COMMON_PROPERTIES);
  });
  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);
    instance.emit("clear", context2);
    setInputValue("", {
      trigger: "clear"
    });
  };
  var onInnerInputChange = function onInnerInputChange(value2, context2) {
    if (props.allowInput) setInputValue(value2, _objectSpread(_objectSpread({}, context2), {}, {
      trigger: "input"
    }));
  };
  var renderSelectSingle = function renderSelectSingle(h, popupVisible) {
    var _inputClass;
    var singleValueDisplay = renderTNode("valueDisplay");
    var pureValue = getInputValue(value.value, keys.value);
    var displayedValue = popupVisible && props.allowInput ? inputValue.value : pureValue;
    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,
      autofocus: props.autofocus,
      readonly: !props.allowInput,
      placeholder: singleValueDisplay ? "" : props.placeholder,
      suffixIcon: !props.disabled && props.loading ? function () {
        return h(Loading, {
          "attrs": {
            "loading": true,
            "size": "small"
          }
        });
      } : props.suffixIcon,
      showClearIconOnEmpty: Boolean(!props.disabled && props.clearable && (inputValue.value || displayedValue)),
      inputClass: (_inputClass = {}, _defineProperty(_inputClass, "".concat(classPrefix.value, "-input--focused"), popupVisible), _defineProperty(_inputClass, "".concat(classPrefix.value, "-is-focused"), popupVisible), _inputClass)
    }, props.inputProps);
    var _context$slots = context.slots,
      tips = _context$slots.tips,
      slots = _objectWithoutProperties(_context$slots, _excluded);
    return h(Input, helper([{
      "ref": "inputRef"
    }, {
      "props": inputProps
    }, {
      "scopedSlots": slots,
      "on": {
        "change": onInnerInputChange,
        "clear": onInnerClear,
        "enter": function enter(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
          }));
          instance.emit("enter", value.value, _objectSpread(_objectSpread({}, context2), {}, {
            inputValue: val
          }));
        },
        "focus": function focus(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
          }));
          instance.emit("focus", value.value, _objectSpread(_objectSpread({}, context2), {}, {
            tagInputValue: val
          }));
        },
        "paste": function paste(context2) {
          var _props$onPaste;
          (_props$onPaste = props.onPaste) === null || _props$onPaste === void 0 ? void 0 : _props$onPaste.call(props, context2);
          instance.emit("paste", context2);
        },
        "mouseenter": function mouseenter(context2) {
          var _props$onMouseenter;
          (_props$onMouseenter = props.onMouseenter) === null || _props$onMouseenter === void 0 ? void 0 : _props$onMouseenter.call(props, context2);
          instance.emit("mouseenter", context2);
        },
        "mouseleave": function mouseleave(context2) {
          var _props$onMouseleave;
          (_props$onMouseleave = props.onMouseleave) === null || _props$onMouseleave === void 0 ? void 0 : _props$onMouseleave.call(props, context2);
          instance.emit("mouseenter", context2);
        }
      }
    }]));
  };
  return {
    inputRef: inputRef,
    commonInputProps: commonInputProps,
    singleInputValue: inputValue,
    onInnerClear: onInnerClear,
    renderSelectSingle: renderSelectSingle
  };
}

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