"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Input = void 0;
var _antdMobileIcons = require("antd-mobile-icons");
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _bound = require("../../utils/bound");
var _nativeProps = require("../../utils/native-props");
var _usePropsValue = require("../../utils/use-props-value");
var _validate = require("../../utils/validate");
var _withDefaultProps = require("../../utils/with-default-props");
var _configProvider = require("../config-provider");
var _useInputHandleKeyDown = _interopRequireDefault(require("./useInputHandleKeyDown"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const classPrefix = `adm-input`;
const defaultProps = {
  defaultValue: '',
  clearIcon: _react.default.createElement(_antdMobileIcons.CloseCircleFill, null),
  onlyShowClearWhenFocus: true
};
const Input = exports.Input = (0, _react.forwardRef)((props, ref) => {
  const {
    locale,
    input: componentConfig = {}
  } = (0, _configProvider.useConfig)();
  const mergedProps = (0, _withDefaultProps.mergeProps)(defaultProps, componentConfig, props);
  const [value, setValue] = (0, _usePropsValue.usePropsValue)(mergedProps);
  const [hasFocus, setHasFocus] = (0, _react.useState)(false);
  const compositionStartRef = (0, _react.useRef)(false);
  const nativeInputRef = (0, _react.useRef)(null);
  const handleKeydown = (0, _useInputHandleKeyDown.default)({
    onEnterPress: mergedProps.onEnterPress,
    onKeyDown: mergedProps.onKeyDown
  });
  (0, _react.useImperativeHandle)(ref, () => ({
    clear: () => {
      setValue('');
    },
    focus: () => {
      var _a;
      (_a = nativeInputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
    },
    blur: () => {
      var _a;
      (_a = nativeInputRef.current) === null || _a === void 0 ? void 0 : _a.blur();
    },
    get nativeElement() {
      return nativeInputRef.current;
    }
  }));
  function checkValue() {
    let nextValue = value;
    if (mergedProps.type === 'number') {
      const boundValue = nextValue && (0, _bound.bound)(parseFloat(nextValue), mergedProps.min, mergedProps.max).toString();
      // fix the display issue of numbers starting with 0
      if (Number(nextValue) !== Number(boundValue)) {
        nextValue = boundValue;
      }
    }
    if (nextValue !== value) {
      setValue(nextValue);
    }
  }
  const shouldShowClear = (() => {
    if (!mergedProps.clearable || !value || mergedProps.readOnly) return false;
    if (mergedProps.onlyShowClearWhenFocus) {
      return hasFocus;
    } else {
      return true;
    }
  })();
  return (0, _nativeProps.withNativeProps)(mergedProps, _react.default.createElement("div", {
    className: (0, _classnames.default)(`${classPrefix}`, mergedProps.disabled && `${classPrefix}-disabled`)
  }, _react.default.createElement("input", {
    ref: nativeInputRef,
    className: `${classPrefix}-element`,
    value: value,
    onChange: e => {
      setValue(e.target.value);
    },
    onFocus: e => {
      var _a;
      setHasFocus(true);
      (_a = mergedProps.onFocus) === null || _a === void 0 ? void 0 : _a.call(mergedProps, e);
    },
    onBlur: e => {
      var _a;
      setHasFocus(false);
      checkValue();
      (_a = mergedProps.onBlur) === null || _a === void 0 ? void 0 : _a.call(mergedProps, e);
    },
    onPaste: mergedProps.onPaste,
    id: mergedProps.id,
    placeholder: mergedProps.placeholder,
    disabled: mergedProps.disabled,
    readOnly: mergedProps.readOnly,
    maxLength: mergedProps.maxLength,
    minLength: mergedProps.minLength,
    max: mergedProps.max,
    min: mergedProps.min,
    autoComplete: mergedProps.autoComplete,
    enterKeyHint: mergedProps.enterKeyHint,
    autoFocus: mergedProps.autoFocus,
    pattern: mergedProps.pattern,
    inputMode: mergedProps.inputMode,
    type: mergedProps.type,
    name: mergedProps.name,
    autoCapitalize: mergedProps.autoCapitalize,
    autoCorrect: mergedProps.autoCorrect,
    onKeyDown: handleKeydown,
    onKeyUp: mergedProps.onKeyUp,
    onCompositionStart: e => {
      var _a;
      compositionStartRef.current = true;
      (_a = mergedProps.onCompositionStart) === null || _a === void 0 ? void 0 : _a.call(mergedProps, e);
    },
    onCompositionEnd: e => {
      var _a;
      compositionStartRef.current = false;
      (_a = mergedProps.onCompositionEnd) === null || _a === void 0 ? void 0 : _a.call(mergedProps, e);
    },
    onClick: mergedProps.onClick,
    step: mergedProps.step,
    role: mergedProps.role,
    "aria-valuenow": mergedProps['aria-valuenow'],
    "aria-valuemax": mergedProps['aria-valuemax'],
    "aria-valuemin": mergedProps['aria-valuemin'],
    "aria-label": mergedProps['aria-label']
  }), shouldShowClear && _react.default.createElement("div", {
    className: `${classPrefix}-clear`,
    onMouseDown: e => {
      e.preventDefault();
    },
    onClick: () => {
      var _a, _b;
      setValue('');
      (_a = mergedProps.onClear) === null || _a === void 0 ? void 0 : _a.call(mergedProps);
      // https://github.com/ant-design/ant-design-mobile/issues/5212
      if ((0, _validate.isIOS)() && compositionStartRef.current) {
        compositionStartRef.current = false;
        (_b = nativeInputRef.current) === null || _b === void 0 ? void 0 : _b.blur();
      }
    },
    "aria-label": locale.Input.clear
  }, mergedProps.clearIcon)));
});