/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

import { h as helper } from '../../../_chunks/dep-b0f1ed22.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/useConfig.js';
import useDefaultValue from '../../../hooks/useDefaultValue.js';
import '../../../utils/map-props.js';
import 'vue';
import '../../../utils/withInstall.js';
import 'lodash/capitalize';
import '../../../config.js';
import '../../input/input.js';
import '@ldesign/icons-vue';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../../_chunks/dep-1a6bca30.js';
import '../../../_chunks/dep-eaa95999.js';
import '../../../_chunks/dep-102361be.js';
import '../../../utils/helper.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import 'lodash/merge';
import '../../../_chunks/dep-b4fd1955.js';
import '../../../utils/mixins.js';
import '../../../utils/event.js';
import '../../../utils/render-node.js';
import 'lodash';
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-aacc31a3.js';
import '../../../_chunks/dep-c68dc4b6.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 => {
    context2?.e?.stopPropagation();
    props.onClear?.(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) => {
          props.onBlur?.(value.value, {
            ...context2,
            inputValue: val
          });
          instance.emit("blur", value.value, {
            ...context2,
            inputValue: val
          });
        },
        "enter": (val, context2) => {
          props.onEnter?.(value.value, {
            ...context2,
            inputValue: val
          });
          instance.emit("enter", value.value, {
            ...context2,
            inputValue: val
          });
        },
        "focus": (val, context2) => {
          props.onFocus?.(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 => {
          props.onPaste?.(context2);
          instance.emit("paste", context2);
        },
        "mouseenter": context2 => {
          props.onMouseenter?.(context2);
          instance.emit("mouseenter", context2);
        },
        "mouseleave": context2 => {
          props.onMouseleave?.(context2);
          instance.emit("mouseenter", context2);
        }
      }
    }]));
  };
  return {
    inputRef,
    commonInputProps,
    onInnerClear,
    renderSelectSingle
  };
}

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