"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var _react = _interopRequireWildcard(require("react"));
var React = _react;
require("./index.scss");
var _excluded = ["value", "onClick", "children", "onChange", "onClear", "onPressEnter", "onFocus", "onBlur"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(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 (var _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); }
var Input = function Input(_ref) {
  var _otherProps$hasClear, _otherProps$readOnly, _otherProps$hasBorder;
  var value = _ref.value,
    onClick = _ref.onClick,
    children = _ref.children,
    onChange = _ref.onChange,
    onClear = _ref.onClear,
    onPressEnter = _ref.onPressEnter,
    onFocus = _ref.onFocus,
    onBlur = _ref.onBlur,
    otherProps = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
  var _otherProps = otherProps || {};

  // legacy schema 兼容：旧属性不再在面板暴露，但历史页面可能仍存在
  var legacyClearable = _otherProps.clearable;
  var legacyReadonly = _otherProps.readonly;
  var legacyBorder = _otherProps.border;
  var legacyPrefix = _otherProps.prefix;
  var legacySuffix = _otherProps.suffix;
  var legacyPrefixIcon = _otherProps.prefixIcon;
  var legacySuffixIcon = _otherProps.suffixIcon;
  var legacyIconSize = _otherProps.iconSize;

  // 兼容旧属性命名（优先使用 Next 原生属性名）
  var hasClear = (_otherProps$hasClear = _otherProps.hasClear) !== null && _otherProps$hasClear !== void 0 ? _otherProps$hasClear : legacyClearable;
  var readOnly = (_otherProps$readOnly = _otherProps.readOnly) !== null && _otherProps$readOnly !== void 0 ? _otherProps$readOnly : legacyReadonly;
  var hasBorder = (_otherProps$hasBorder = _otherProps.hasBorder) !== null && _otherProps$hasBorder !== void 0 ? _otherProps$hasBorder : legacyBorder;

  // legacy props 不应透传给 Next，否则会被当作 DOM attribute 下发
  delete _otherProps.clearable;
  delete _otherProps.readonly;
  delete _otherProps.border;
  delete _otherProps.prefix;
  delete _otherProps.suffix;
  delete _otherProps.prefixIcon;
  delete _otherProps.suffixIcon;
  delete _otherProps.iconSize;
  // 构建addonBefore内容
  var innerBefore = legacyPrefixIcon || legacyPrefix ? /*#__PURE__*/React.createElement("div", {
    style: {
      display: 'flex',
      alignItems: 'center'
    }
  }, legacyPrefixIcon && /*#__PURE__*/(0, _react.createElement)(_icon["default"], {
    type: legacyPrefixIcon,
    size: legacyIconSize
  }), legacyPrefix && /*#__PURE__*/React.createElement("span", null, legacyPrefix)) : null;

  // 构建addonAfter内容
  var innerAfter = legacySuffix || legacySuffixIcon ? /*#__PURE__*/React.createElement("div", {
    style: {
      display: 'flex',
      alignItems: 'center'
    }
  }, legacySuffix && /*#__PURE__*/React.createElement("span", null, legacySuffix), legacySuffixIcon && /*#__PURE__*/(0, _react.createElement)(_icon["default"], {
    type: legacySuffixIcon,
    size: legacyIconSize
  })) : null;
  var nextProps = (0, _extends2["default"])({}, _otherProps);
  if (hasClear !== undefined) nextProps.hasClear = hasClear;
  if (readOnly !== undefined) nextProps.readOnly = readOnly;
  if (hasBorder !== undefined) nextProps.hasBorder = hasBorder;

  // 仅在用户未显式传 innerBefore/innerAfter 时，才使用旧属性构造的内容
  if (innerBefore && nextProps.innerBefore == null) nextProps.innerBefore = innerBefore;
  if (innerAfter && nextProps.innerAfter == null) nextProps.innerAfter = innerAfter;
  if (onClick) nextProps.onClick = onClick;
  if (children != null) nextProps.children = children;
  if (onChange) nextProps.onChange = onChange;
  if (onClear) nextProps.onClear = onClear;
  if (onPressEnter) nextProps.onPressEnter = onPressEnter;
  if (onFocus) nextProps.onFocus = onFocus;
  if (onBlur) nextProps.onBlur = onBlur;

  // Next Input 通过 `'value' in props` 判断是否受控。
  // 低代码场景下 value 可能为 undefined（表达式未初始化），此时传入 value 会导致“受控但不更新”，表现为无法输入/无法联动。
  // 因此仅在 value 非 null/undefined 时才下传 value。
  if (value !== undefined && value !== null) {
    nextProps.value = value;
    // 受控模式下避免同时存在 defaultValue
    if ('defaultValue' in nextProps) {
      delete nextProps.defaultValue;
    }
  }
  return /*#__PURE__*/(0, _react.createElement)(NextInput, nextProps);
};
Input.displayName = 'Input';
var _default = exports["default"] = Input;