/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

'use strict';

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

var helper = require('../../../chunks/helper.js');
var VueCompositionAPI = require('@vue/composition-api');
var isObject = require('lodash/isObject');
var pick = require('lodash/pick');
var components_input_index = require('../../input/index.js');
var components_loading_index = require('../../loading/index.js');
var hooks_lnode = require('../../../hooks/lnode.js');
var hooks_useConfig = require('../../../hooks/use-config.js');
var hooks_useDefaultValue = require('../../../hooks/use-default-value.js');
require('../../../utils/map-props.js');
require('vue');
require('../../../utils/with-install.js');
require('lodash');
require('../../../config.js');
require('../../input/input.js');
require('@ldesign/icons-vue');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../../chunks/helper2.js');
require('../../../chunks/log.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../../utils/helper.js');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('../../../chunks/zh_CN.js');
require('../../../utils/mixins.js');
require('../../../utils/event.js');
require('../../../utils/render-node.js');
require('../../input/props.js');
require('../../input/group.js');
require('../../loading/loading.js');
require('../../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../../utils/easing.js');
require('../../../utils/transfer-dom.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../../chunks/defineProperty-838e4192.js');
require('../../../chunks/set-style.js');
require('../../loading/plugin.js');
require('lodash/isEmpty');
require('lodash/isFunction');
require('../../../hooks/render-lnode.js');
require('../../config-provider/useConfig.js');

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

var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
var pick__default = /*#__PURE__*/_interopDefaultLegacy(pick);

const COMMON_PROPERTIES = ["status", "clearable", "disabled", "label", "placeholder", "readonly", "suffix", "suffixIcon"];
const DEFAULT_KEYS = {
  label: "label",
  value: "value"
};
function getInputValue(value, keys) {
  const iKeys = keys || DEFAULT_KEYS;
  return isObject__default["default"](value) ? value[iKeys.label] : value;
}
function useSingle(props, context) {
  const instance = VueCompositionAPI.getCurrentInstance();
  const {
    value,
    keys,
    inputValue: propsInputValue
  } = VueCompositionAPI.toRefs(props);
  const classPrefix = hooks_useConfig.usePrefixClass();
  const [inputValue, setInputValue] = hooks_useDefaultValue["default"](propsInputValue, props.defaultInputValue ?? "", props.onInputChange, "inputValue", "input-change");
  const inputRef = VueCompositionAPI.ref();
  const renderTNode = hooks_lnode.useLNodeJSX();
  const commonInputProps = VueCompositionAPI.computed(() => pick__default["default"](props, COMMON_PROPERTIES));
  const 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"
    });
  };
  const onInnerInputChange = (value2, context2) => {
    if (props.allowInput) setInputValue(value2, {
      ...context2,
      trigger: "input"
    });
  };
  const renderSelectSingle = (h, popupVisible) => {
    const singleValueDisplay = renderTNode("valueDisplay");
    const displayedValue = popupVisible && props.allowInput ? inputValue.value : getInputValue(value.value, keys.value);
    const prefixContent = [singleValueDisplay, renderTNode("label")];
    const inputProps = {
      ...commonInputProps.value,
      value: singleValueDisplay ? void 0 : displayedValue,
      label: prefixContent.length ? () => prefixContent : void 0,
      autoWidth: props.autoWidth,
      readonly: !props.allowInput,
      placeholder: singleValueDisplay ? "" : props.placeholder,
      suffixIcon: !props.disabled && props.loading ? () => h(components_loading_index.Loading, {
        "attrs": {
          "loading": true,
          "size": "small"
        }
      }) : props.suffixIcon,
      showClearIconOnEmpty: Boolean(!props.disabled && props.clearable && (inputValue.value || displayedValue)),
      inputClass: {
        [`${classPrefix.value}-input--focused`]: popupVisible,
        [`${classPrefix.value}-is-focused`]: popupVisible
      },
      ...props.inputProps
    };
    return h(components_input_index.Input, helper.helper([{
      "ref": "inputRef"
    }, {
      "props": inputProps
    }, {
      "scopedSlots": context.slots,
      "on": {
        "change": onInnerInputChange,
        "clear": onInnerClear,
        "blur": (val, context2) => {
          var _props$onBlur;
          (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 ? void 0 : _props$onBlur.call(props, value.value, {
            ...context2,
            inputValue: val
          });
          instance.emit("blur", value.value, {
            ...context2,
            inputValue: val
          });
        },
        "enter": (val, context2) => {
          var _props$onEnter;
          (_props$onEnter = props.onEnter) === null || _props$onEnter === void 0 ? void 0 : _props$onEnter.call(props, value.value, {
            ...context2,
            inputValue: val
          });
          instance.emit("enter", value.value, {
            ...context2,
            inputValue: val
          });
        },
        "focus": (val, context2) => {
          var _props$onFocus;
          (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 ? void 0 : _props$onFocus.call(props, value.value, {
            ...context2,
            inputValue: val
          });
          instance.emit("focus", value.value, {
            ...context2,
            tagInputValue: val
          });
          !popupVisible && setInputValue(getInputValue(value.value, keys.value), {
            ...context2,
            trigger: "input"
          });
        },
        "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": 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": 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,
    commonInputProps,
    onInnerClear,
    renderSelectSingle
  };
}

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