"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

var _classnames = _interopRequireDefault(require("classnames"));

var _errorTip = _interopRequireDefault(require("../../../tooltip/error-tip"));

require("./index.css");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var preCls = "form-field-textarea";

function OverlayInner(_ref) {
  var hasError = _ref.hasError,
      hasActive = _ref.hasActive,
      tip = _ref.tip;
  if (hasError) return /*#__PURE__*/_react["default"].createElement("span", {
    style: {
      color: "#F05E5E"
    }
  }, tip);
  if (hasActive) return /*#__PURE__*/_react["default"].createElement("span", {
    style: {
      color: "#3F4755"
    }
  }, tip);
  return /*#__PURE__*/_react["default"].createElement("span", null);
}

var _default = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _classNames2;

  var label = props.label,
      verifies = props.verifies,
      placeholder = props.placeholder,
      onChange = props.onChange,
      require = props.require,
      name = props.name,
      getContainer = props.getContainer,
      _props$resizeType = props.resizeType,
      resizeType = _props$resizeType === void 0 ? "none" : _props$resizeType,
      areaHeight = props.areaHeight,
      layout = props.layout;

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      hasError = _useState2[0],
      setHasError = _useState2[1];

  var _useState3 = (0, _react.useState)(false),
      _useState4 = _slicedToArray(_useState3, 2),
      hasActive = _useState4[0],
      setHasActive = _useState4[1];

  var _useState5 = (0, _react.useState)(""),
      _useState6 = _slicedToArray(_useState5, 2),
      tip = _useState6[0],
      setTip = _useState6[1];

  var _useState7 = (0, _react.useState)(props.value || ""),
      _useState8 = _slicedToArray(_useState7, 2),
      value = _useState8[0],
      setValue = _useState8[1];

  var inputRef = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    setValue(props.value);
  }, [props.value]);

  var _verifing = function verifing() {
    var value = inputRef.current && inputRef.current.value;
    if (!verifies || !verifies.length) return [];
    if (!require && !value) return [];
    var errors = [];

    for (var i = 0; i < verifies.length; i++) {
      var _verifies$i = verifies[i],
          test = _verifies$i.test,
          _tip = _verifies$i.tip;

      if (!value.match(test)) {
        errors.push({
          name: name,
          tip: _tip
        });
      }
    }

    return errors;
  };

  var onInputFocusHandler = function onInputFocusHandler() {
    setHasActive(true);
    inputRef.current.setSelectionRange(9999999, 9999999);
  };

  var onBlurHandler = function onBlurHandler() {
    setHasActive(false);
  };

  var onChangeHandler = function onChangeHandler() {
    var value = inputRef.current && inputRef.current.value;
    onChange && onChange(value);
    setValue(value);

    var errors = _verifing();

    if (errors.length) {
      setHasError(true);
      setTip(errors[0].tip);
    } else {
      setHasError(false);
      setTip("");
    }
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      verifing: function verifing() {
        var errors = _verifing();

        if (errors.length) {
          setHasError(true);
          setTip(errors[0].tip);
          inputRef.current.focus();
        }

        return errors;
      }
    };
  });
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])(preCls, _defineProperty({}, "".concat(preCls, "-row"), layout === "row"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__label"))
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(preCls, "__label-text"))
  }, label), require && /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(preCls, "__label-require"))
  }, "*")), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__border"), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(preCls, "__border--error"), hasError), _defineProperty(_classNames2, "".concat(preCls, "__border--active"), hasActive), _classNames2))
  }, /*#__PURE__*/_react["default"].createElement(_errorTip["default"], {
    placement: "topLeft",
    destroyTooltipOnHide: true,
    visible: (hasActive || hasError) && tip,
    overlay: /*#__PURE__*/_react["default"].createElement(OverlayInner, {
      hasActive: hasActive,
      hasError: hasError,
      tip: tip
    }),
    getTooltipContainer: getContainer
  }, /*#__PURE__*/_react["default"].createElement("textarea", {
    ref: inputRef,
    className: (0, _classnames["default"])("".concat(preCls, "__textarea")),
    value: value,
    placeholder: placeholder,
    onFocus: onInputFocusHandler,
    onBlur: onBlurHandler,
    onChange: onChangeHandler,
    style: {
      resize: resizeType,
      height: "".concat(areaHeight, "px")
    }
  }))));
});

exports["default"] = _default;