/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

'use strict';

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

var helper = require('../../../chunks/helper.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var VueCompositionAPI = require('@vue/composition-api');
var isObject = require('lodash/isObject');
var pick = require('lodash/pick');
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('lodash/isEmpty');
require('lodash/isFunction');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../../hooks/render-lnode.js');
require('lodash/isString');
require('../../config-provider/useConfig.js');
require('../../config-provider/context.js');
require('lodash');
require('../../../chunks/zh_CN.js');

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

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

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"];
var DEFAULT_KEYS = {
  label: "label",
  value: "value"
};

function getInputValue(value, keys) {
  var iKeys = keys || DEFAULT_KEYS;
  return isObject__default["default"](value) ? value[iKeys.label] : value;
}

function useSingle(props, context) {
  var _props$defaultInputVa;

  var instance = VueCompositionAPI.getCurrentInstance();

  var _toRefs = VueCompositionAPI.toRefs(props),
      value = _toRefs.value,
      keys = _toRefs.keys,
      propsInputValue = _toRefs.inputValue;

  var classPrefix = hooks_useConfig.usePrefixClass();

  var _useDefaultValue = hooks_useDefaultValue["default"](propsInputValue, (_props$defaultInputVa = props.defaultInputValue) !== null && _props$defaultInputVa !== void 0 ? _props$defaultInputVa : "", props.onInputChange, "inputValue", "input-change"),
      _useDefaultValue2 = _slicedToArray__default["default"](_useDefaultValue, 2),
      inputValue = _useDefaultValue2[0],
      setInputValue = _useDefaultValue2[1];

  var inputRef = VueCompositionAPI.ref();
  var renderTNode = hooks_lnode.useLNodeJSX();
  var commonInputProps = VueCompositionAPI.computed(function () {
    return pick__default["default"](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 displayedValue = popupVisible && props.allowInput ? inputValue.value : getInputValue(value.value, keys.value);
    var prefixContent = [singleValueDisplay, renderTNode("label")];

    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,
      placeholder: singleValueDisplay ? "" : props.placeholder,
      suffixIcon: !props.disabled && props.loading ? function () {
        return h("l-loading", {
          "attrs": {
            "loading": true,
            "size": "small"
          }
        });
      } : props.suffixIcon,
      showClearIconOnEmpty: Boolean(!props.disabled && props.clearable && (inputValue.value || displayedValue)),
      inputClass: (_inputClass = {}, _defineProperty__default["default"](_inputClass, "".concat(classPrefix.value, "-input--focused"), popupVisible), _defineProperty__default["default"](_inputClass, "".concat(classPrefix.value, "-is-focused"), popupVisible), _inputClass)
    }, props.inputProps);

    return h("l-input", helper.helper([{
      "ref": "inputRef"
    }, {
      "props": inputProps
    }, {
      "scopedSlots": context.slots,
      "on": {
        "change": onInnerInputChange,
        "clear": onInnerClear,
        "blur": function blur(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
          }));
          instance.emit("blur", value.value, _objectSpread(_objectSpread({}, context2), {}, {
            inputValue: val
          }));
        },
        "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
          }));
          !popupVisible && setInputValue(getInputValue(value.value, keys.value), _objectSpread(_objectSpread({}, context2), {}, {
            trigger: "input"
          }));
        },
        "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,
    onInnerClear: onInnerClear,
    renderSelectSingle: renderSelectSingle
  };
}

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