"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _bem = require("@zarm-design/bem");

var _icons = require("@zarm-design/icons");

var React = _interopRequireWildcard(require("react"));

var _configProvider = require("../config-provider");

var _hooks = require("../utils/hooks");

var _resolveOnChange = require("../utils/resolveOnChange");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var regexAstralSymbols = /[\uD800-\uDBFF][\uDC00-\uDFFF]|\n/g;

var countSymbols = function countSymbols() {
  var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  return text.replace(regexAstralSymbols, '_').length;
};

var Input = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _ref = props,
      type = _ref.type,
      disabled = _ref.disabled,
      autoFocus = _ref.autoFocus,
      readOnly = _ref.readOnly,
      clearable = _ref.clearable,
      showLength = _ref.showLength,
      autoHeight = _ref.autoHeight,
      rows = _ref.rows,
      className = _ref.className,
      style = _ref.style,
      maxLength = _ref.maxLength,
      label = _ref.label,
      _ref$defaultValue = _ref.defaultValue,
      defaultValue = _ref$defaultValue === void 0 ? '' : _ref$defaultValue,
      onChange = _ref.onChange,
      onBlur = _ref.onBlur,
      onFocus = _ref.onFocus,
      restProps = (0, _objectWithoutProperties2.default)(_ref, ["type", "disabled", "autoFocus", "readOnly", "clearable", "showLength", "autoHeight", "rows", "className", "style", "maxLength", "label", "defaultValue", "onChange", "onBlur", "onFocus"]);
  var wrapperRef = ref || /*#__PURE__*/React.createRef();
  var inputRef = React.useRef();

  var _useControllableEvent = (0, _hooks.useControllableEventValue)(_objectSpread(_objectSpread({}, props), {}, {
    defaultValue: defaultValue
  })),
      _useControllableEvent2 = (0, _slicedToArray2.default)(_useControllableEvent, 2),
      value = _useControllableEvent2[0],
      setValue = _useControllableEvent2[1];

  var _React$useState = React.useState(autoFocus),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      focused = _React$useState2[0],
      setFocused = _React$useState2[1];

  var isTextarea = type === 'text' && 'rows' in props;
  var blurFromClear = false;
  var blurTimeout;
  var showClearIcon = clearable && !readOnly && !disabled && typeof value !== 'undefined' && (value === null || value === void 0 ? void 0 : value.length) > 0;

  var _React$useContext = React.useContext(_configProvider.ConfigContext),
      prefixCls = _React$useContext.prefixCls;

  var bem = (0, _bem.createBEM)('input', {
    prefixCls: prefixCls
  });
  var cls = bem([{
    textarea: isTextarea,
    disabled: disabled,
    clearable: showClearIcon,
    readonly: readOnly,
    focus: focused
  }, className]);

  var focus = function focus() {
    inputRef.current.focus();
  };

  var blur = function blur() {
    inputRef.current.blur();
  };

  var clear = function clear() {
    onInputClear(new Event('click'));
  };

  var onInputBlur = function onInputBlur(e) {
    blurTimeout = window.setTimeout(function () {
      if (!blurFromClear && document.activeElement !== inputRef.current) {
        setFocused(false);
        onBlur === null || onBlur === void 0 ? void 0 : onBlur(e);
      }

      blurFromClear = false;
    }, 200);
  };

  var onInputFocus = function onInputFocus(e) {
    setFocused(true);
    onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
  };

  var onInputChange = function onInputChange(e) {
    setValue(e);
  };

  var onInputClear = function onInputClear(e) {
    blurFromClear = true;
    (0, _resolveOnChange.resolveOnChange)(inputRef.current, e, setValue);
    focus();
  }; // 渲染文字长度


  var textLengthRender = showLength && maxLength && /*#__PURE__*/React.createElement("div", {
    className: bem('length')
  }, "".concat(countSymbols(value), "/").concat(maxLength));

  var commonProps = _objectSpread(_objectSpread({}, restProps), {}, {
    maxLength: maxLength,
    disabled: disabled,
    autoFocus: autoFocus,
    readOnly: readOnly,
    value: value,
    onFocus: onInputFocus,
    onBlur: onInputBlur,
    onChange: onInputChange
  }); // 渲染输入框


  var inputRender = isTextarea ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("textarea", (0, _extends2.default)({
    ref: inputRef
  }, commonProps, {
    rows: rows
  })), textLengthRender) : /*#__PURE__*/React.createElement("input", (0, _extends2.default)({
    ref: inputRef
  }, commonProps, {
    type: type
  })); // 渲染文本内容

  var textRender = /*#__PURE__*/React.createElement("div", {
    className: bem('content')
  }, value); // 渲染标签栏

  var labelRender = !!label && /*#__PURE__*/React.createElement("div", {
    className: bem('label')
  }, label); // 渲染清除按钮

  var clearIconRender = showClearIcon && /*#__PURE__*/React.createElement(_icons.CloseCircleFill, {
    className: bem('clear'),
    onClick: onInputClear
  });
  React.useImperativeHandle(wrapperRef, function () {
    return {
      focus: focus,
      blur: blur,
      clear: clear,

      get nativeElement() {
        return inputRef.current;
      }

    };
  });
  React.useEffect(function () {
    if (!autoHeight) return;
    inputRef.current.style.height = 'auto';
    inputRef.current.scrollTop = 0;
    inputRef.current.style.height = "".concat(inputRef.current.scrollHeight, "px");

    if (readOnly && rows) {
      inputRef.current.style.height = "".concat(inputRef.current.scrollHeight * rows, "px");
    }
  }, [autoHeight, readOnly, rows, value]);
  React.useEffect(function () {
    return function () {
      !!blurTimeout && clearTimeout(blurTimeout);
    };
  });
  return /*#__PURE__*/React.createElement("div", {
    ref: wrapperRef,
    className: cls,
    style: style
  }, labelRender, !readOnly ? inputRender : textRender, clearIconRender);
});
Input.displayName = 'Input';
Input.defaultProps = {
  type: 'text',
  disabled: false,
  autoFocus: false,
  readOnly: false,
  clearable: false,
  showLength: false,
  autoHeight: false
};
var _default = Input;
exports.default = _default;