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

import { h as helper } from '../../../_chunks/dep-29915b8e.js';
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 { useLNodeJSX } from '../../../hooks/lnode.js';
import { usePrefixClass } from '../../../hooks/use-config.js';
import useDefaultValue from '../../../hooks/use-default-value.js';
import '../../../utils/map-props.js';
import 'vue';
import '../../../utils/with-install.js';
import 'lodash';
import '../../../config.js';
import '../../input/input.js';
import '@ldesign/icons-vue';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../../_chunks/dep-47d77fc8.js';
import '../../../_chunks/dep-70c789e5.js';
import '../../../_chunks/dep-5210251b.js';
import '../../../utils/helper.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import '../../../_chunks/dep-def4c2ce.js';
import '../../../utils/mixins.js';
import '../../../utils/event.js';
import '../../../utils/render-node.js';
import '../../input/props.js';
import '../../input/group.js';
import '../../loading/loading.js';
import '../../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../../utils/easing.js';
import '../../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../../_chunks/dep-eb467ddf.js';
import '../../../_chunks/dep-cf772670.js';
import '../../loading/plugin.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import '../../../hooks/render-lnode.js';
import '../../config-provider/useConfig.js';

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(value) ? value[iKeys.label] : value;
}
function useSingle(props, context) {
  const instance = getCurrentInstance();
  const {
    value,
    keys,
    inputValue: propsInputValue
  } = toRefs(props);
  const classPrefix = usePrefixClass();
  const [inputValue, setInputValue] = useDefaultValue(propsInputValue, props.defaultInputValue ?? "", props.onInputChange, "inputValue", "input-change");
  const inputRef = ref();
  const renderTNode = useLNodeJSX();
  const commonInputProps = computed(() => pick(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(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(Input, 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
  };
}

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