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

'use strict';

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

var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var isObject = require('lodash/isObject');
var pick = require('lodash/pick');
var input_index = require('../input/index.js');
var loading_index = require('../loading/index.js');
var hooks_tnode = require('../hooks/tnode.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_useDefaultValue = require('../hooks/useDefaultValue.js');
var form_hooks = require('../form/hooks.js');
require('../utils/withInstall.js');
require('../input/input.js');
require('@ldesign/icons-vue-next');
require('lodash/isUndefined');
require('../hooks/useGlobalIcon.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('lodash/isFunction');
require('lodash/isString');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
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-7f0559c3.js');
require('dayjs');
require('../input/props.js');
require('../input/useInput.js');
require('../form/const.js');
require('../hooks/useVModel.js');
require('lodash/kebabCase');
require('../input/useLengthLimit.js');
require('lodash/isNumber');
require('../_common/js/log/log.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/isNull');
require('../input/useInputEventHandler.js');
require('../input/useInputWidth.js');
require('lodash/camelCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('../input/input-group.js');
require('../input/input-group-props.js');
require('../loading/directive.js');
require('../loading/plugin.js');
require('../utils/dom.js');
require('../utils/easing.js');
require('../loading/loading.js');
require('../hooks/useTeleport.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/set-style.js');

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

var _objectWithoutProperties__default = /*#__PURE__*/_interopDefaultLegacy(_objectWithoutProperties);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
var pick__default = /*#__PURE__*/_interopDefaultLegacy(pick);

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__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; }
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__default["default"](value) ? value[iKeys.label] : value;
}
function useSingle(props, context) {
  var _props$defaultInputVa;
  var _toRefs = vue.toRefs(props),
    value = _toRefs.value,
    keys = _toRefs.keys,
    propsInputValue = _toRefs.inputValue;
  var classPrefix = hooks_useConfig.usePrefixClass();
  var inputRef = vue.ref();
  var _useDefaultValue = hooks_useDefaultValue["default"](propsInputValue, (_props$defaultInputVa = props.defaultInputValue) !== null && _props$defaultInputVa !== void 0 ? _props$defaultInputVa : "", props.onInputChange, "inputValue"),
    _useDefaultValue2 = _slicedToArray__default["default"](_useDefaultValue, 2),
    inputValue = _useDefaultValue2[0],
    setInputValue = _useDefaultValue2[1];
  var renderTNode = hooks_tnode.useTNodeJSX();
  var disable = form_hooks.useFormDisabled();
  var commonInputProps = vue.computed(function () {
    return _objectSpread(_objectSpread({}, pick__default["default"](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 vue.createVNode(loading_index.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;
      _context$slots.tips;
      var slots = _objectWithoutProperties__default["default"](_context$slots, _excluded);
    return vue.createVNode(input_index.Input, vue.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__default["default"](_ref, "".concat(classPrefix.value, "-input--focused"), popupVisible), _defineProperty__default["default"](_ref, "".concat(classPrefix.value, "-is-focused"), popupVisible), _ref)
    }), slots);
  };
  return {
    inputRef: inputRef,
    commonInputProps: commonInputProps,
    onInnerClear: onInnerClear,
    renderSelectSingle: renderSelectSingle
  };
}

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