'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../../directives/index.cjs');
var inputProps = require('./inputProps.cjs');
var commonUtils = require('./utils/commonUtils.cjs');
var BaseInput = require('./BaseInput.cjs');
var omit = require('../../../utils/lang/omit.cjs');
var _class = require('../../../utils/dom/class.cjs');
var index = require('../../../directives/ant-input/index.cjs');

var VcInput = /* @__PURE__ */ vue.defineComponent({
  name: "VCInput",
  inheritAttrs: false,
  props: inputProps.inputProps(),
  setup(props, {
    slots,
    attrs,
    expose,
    emit
  }) {
    const stateValue = vue.shallowRef(props.value === void 0 ? props.defaultValue : props.value);
    const focused = vue.shallowRef(false);
    const inputRef = vue.shallowRef();
    const rootRef = vue.shallowRef();
    vue.watch(() => props.value, () => {
      stateValue.value = props.value;
    });
    vue.watch(() => props.disabled, () => {
      if (props.disabled)
        focused.value = false;
    });
    const focus = (option) => {
      if (inputRef.value)
        commonUtils.triggerFocus(inputRef.value, option);
    };
    const blur = () => {
      var _a;
      (_a = inputRef.value) == null ? void 0 : _a.blur();
    };
    const setSelectionRange = (start, end, direction) => {
      var _a;
      (_a = inputRef.value) == null ? void 0 : _a.setSelectionRange(start, end, direction);
    };
    const select = () => {
      var _a;
      (_a = inputRef.value) == null ? void 0 : _a.select();
    };
    expose({
      focus,
      blur,
      input: inputRef,
      stateValue,
      setSelectionRange,
      select
    });
    const triggerChange = (e) => {
      emit("change", e);
    };
    const setValue = (value, callback) => {
      if (stateValue.value === value)
        return;
      if (props.value === void 0) {
        stateValue.value = value;
      } else {
        vue.nextTick(() => {
          var _a;
          if (inputRef.value.value !== stateValue.value)
            (_a = rootRef.value) == null ? void 0 : _a.$forceUpdate();
        });
      }
      vue.nextTick(() => {
        callback && callback();
      });
    };
    const handleChange = (e) => {
      const {
        value,
        composing
      } = e.target;
      if ((e.isComposing || composing) && props.lazy || stateValue.value === value)
        return;
      const newVal = e.target.value;
      commonUtils.resolveOnChange(inputRef.value, e, triggerChange);
      setValue(newVal);
    };
    const handleKeyDown = (e) => {
      if (e.keyCode === 13)
        emit("pressEnter", e);
      emit("keydown", e);
    };
    const handleFocus = (e) => {
      focused.value = true;
      emit("focus", e);
    };
    const handleBlur = (e) => {
      focused.value = false;
      emit("blur", e);
    };
    const handleReset = (e) => {
      commonUtils.resolveOnChange(inputRef.value, e, triggerChange);
      setValue("", () => {
        focus();
      });
    };
    const getInputElement = () => {
      var _a, _b;
      const {
        addonBefore = slots.addonBefore,
        addonAfter = slots.addonAfter,
        disabled,
        valueModifiers = {},
        htmlSize,
        autocomplete,
        prefixCls,
        inputClassName,
        prefix = (_a = slots.prefix) == null ? void 0 : _a.call(slots),
        suffix = (_b = slots.suffix) == null ? void 0 : _b.call(slots),
        allowClear,
        type = "text"
      } = props;
      const otherProps = omit.omit(props, [
        "prefixCls",
        "onPressEnter",
        "addonBefore",
        "addonAfter",
        "prefix",
        "suffix",
        "allowClear",
        // Input elements must be either controlled or uncontrolled,
        // specify either the value prop, or the defaultValue prop, but not both.
        "defaultValue",
        "size",
        "bordered",
        "htmlSize",
        "lazy",
        "showCount",
        "valueModifiers",
        "showCount",
        "affixWrapperClassName",
        "groupClassName",
        "inputClassName",
        "wrapperClassName"
      ]);
      const inputProps2 = {
        ...otherProps,
        ...attrs,
        autocomplete,
        onChange: handleChange,
        onInput: handleChange,
        onFocus: handleFocus,
        onBlur: handleBlur,
        onKeydown: handleKeyDown,
        class: _class.classNames(prefixCls, {
          [`${prefixCls}-disabled`]: disabled
        }, inputClassName, !commonUtils.hasAddon({
          addonAfter,
          addonBefore
        }) && !commonUtils.hasPrefixSuffix({
          prefix,
          suffix,
          allowClear
        }) && attrs.class),
        ref: inputRef,
        key: "ant-input",
        size: htmlSize,
        type
      };
      if (valueModifiers.lazy)
        delete inputProps2.onInput;
      if (!inputProps2.autofocus)
        delete inputProps2.autofocus;
      const inputNode = vue.createVNode("input", omit.omit(inputProps2, ["size"]), null);
      return vue.withDirectives(inputNode, [[index.antInputDirective]]);
    };
    const getSuffix = () => {
      var _a;
      const {
        maxlength,
        suffix = (_a = slots.suffix) == null ? void 0 : _a.call(slots),
        showCount,
        prefixCls
      } = props;
      const hasMaxLength = Number(maxlength) > 0;
      if (suffix || showCount) {
        const valueLength = [...commonUtils.fixControlledValue(stateValue.value)].length;
        const dataCount = typeof showCount === "object" ? showCount.formatter({
          count: valueLength,
          maxlength
        }) : `${valueLength}${hasMaxLength ? ` / ${maxlength}` : ""}`;
        return vue.createVNode(vue.Fragment, null, [!!showCount && vue.createVNode("span", {
          "class": _class.classNames(`${prefixCls}-show-count-suffix`, {
            [`${prefixCls}-show-count-has-suffix`]: !!suffix
          })
        }, [dataCount]), suffix]);
      }
      return null;
    };
    vue.onMounted(() => {
      if (process.env.NODE_ENV === "test") {
        if (props.autofocus)
          focus();
      }
    });
    return () => {
      const {
        prefixCls,
        disabled,
        ...rest
      } = props;
      return vue.createVNode(BaseInput.default, {
        ...rest,
        ...attrs,
        "ref": rootRef,
        "prefixCls": prefixCls,
        "inputElement": getInputElement(),
        "handleReset": handleReset,
        "value": commonUtils.fixControlledValue(stateValue.value),
        "focused": focused.value,
        "triggerFocus": focus,
        "suffix": getSuffix(),
        "disabled": disabled
      }, slots);
    };
  }
});

exports.default = VcInput;
//# sourceMappingURL=Input.cjs.map
