import { defineComponent, shallowRef, watch, nextTick, createVNode, withDirectives, Fragment, onMounted } from 'vue';
import '../../../utils/index.mjs';
import '../../../directives/index.mjs';
import { inputProps } from './inputProps.mjs';
import { triggerFocus, resolveOnChange, hasAddon, hasPrefixSuffix, fixControlledValue } from './utils/commonUtils.mjs';
import BaseInput from './BaseInput.mjs';
import { omit } from '../../../utils/lang/omit.mjs';
import { classNames } from '../../../utils/dom/class.mjs';
import { antInputDirective } from '../../../directives/ant-input/index.mjs';

var VcInput = /* @__PURE__ */ defineComponent({
  name: "VCInput",
  inheritAttrs: false,
  props: inputProps(),
  setup(props, {
    slots,
    attrs,
    expose,
    emit
  }) {
    const stateValue = shallowRef(props.value === void 0 ? props.defaultValue : props.value);
    const focused = shallowRef(false);
    const inputRef = shallowRef();
    const rootRef = shallowRef();
    watch(() => props.value, () => {
      stateValue.value = props.value;
    });
    watch(() => props.disabled, () => {
      if (props.disabled)
        focused.value = false;
    });
    const focus = (option) => {
      if (inputRef.value)
        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 {
        nextTick(() => {
          var _a;
          if (inputRef.value.value !== stateValue.value)
            (_a = rootRef.value) == null ? void 0 : _a.$forceUpdate();
        });
      }
      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;
      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) => {
      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(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: classNames(prefixCls, {
          [`${prefixCls}-disabled`]: disabled
        }, inputClassName, !hasAddon({
          addonAfter,
          addonBefore
        }) && !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 = createVNode("input", omit(inputProps2, ["size"]), null);
      return withDirectives(inputNode, [[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 = [...fixControlledValue(stateValue.value)].length;
        const dataCount = typeof showCount === "object" ? showCount.formatter({
          count: valueLength,
          maxlength
        }) : `${valueLength}${hasMaxLength ? ` / ${maxlength}` : ""}`;
        return createVNode(Fragment, null, [!!showCount && createVNode("span", {
          "class": classNames(`${prefixCls}-show-count-suffix`, {
            [`${prefixCls}-show-count-has-suffix`]: !!suffix
          })
        }, [dataCount]), suffix]);
      }
      return null;
    };
    onMounted(() => {
      if (process.env.NODE_ENV === "test") {
        if (props.autofocus)
          focus();
      }
    });
    return () => {
      const {
        prefixCls,
        disabled,
        ...rest
      } = props;
      return createVNode(BaseInput, {
        ...rest,
        ...attrs,
        "ref": rootRef,
        "prefixCls": prefixCls,
        "inputElement": getInputElement(),
        "handleReset": handleReset,
        "value": fixControlledValue(stateValue.value),
        "focused": focused.value,
        "triggerFocus": focus,
        "suffix": getSuffix(),
        "disabled": disabled
      }, slots);
    };
  }
});

export { VcInput as default };
//# sourceMappingURL=Input.mjs.map
