"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 _classnames2 = _interopRequireDefault(require("classnames"));

require("./index.css");

var _excluded = ["disabled", "onFocus", "onBlur", "inputRef", "onChange", "className", "value", "placeholder", "maxLine", "line", "changeEditStatus", "maxLength", "initValue", "onErrorChange", "validator", "onKeyDown", "disabledEnter"];

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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var Textarea = function Textarea(p) {
  var _classnames;

  var disabled = p.disabled,
      _onFocus = p.onFocus,
      _onBlur = p.onBlur,
      inputRef = p.inputRef,
      _onChange = p.onChange,
      className = p.className,
      value = p.value,
      placeholder = p.placeholder,
      _p$maxLine = p.maxLine,
      maxLine = _p$maxLine === void 0 ? 4 : _p$maxLine,
      line = p.line,
      changeEditStatus = p.changeEditStatus,
      maxLength = p.maxLength,
      initValue = p.initValue,
      onErrorChange = p.onErrorChange,
      validator = p.validator,
      _onKeyDown = p.onKeyDown,
      _p$disabledEnter = p.disabledEnter,
      disabledEnter = _p$disabledEnter === void 0 ? true : _p$disabledEnter,
      others = _objectWithoutProperties(p, _excluded);

  var prefixCls = "titaui-pc-textArea";

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

  var _useState3 = (0, _react.useState)(initValue || ""),
      _useState4 = _slicedToArray(_useState3, 2),
      selfValue = _useState4[0],
      setSelfValue = _useState4[1];

  var selfInputRef = (0, _react.useRef)();
  var realInputRef = inputRef || selfInputRef;
  var isControlled = ("value" in p);
  var realValue = isControlled ? value || '' : selfValue || '';

  var _useState5 = (0, _react.useState)(false),
      _useState6 = _slicedToArray(_useState5, 2),
      error = _useState6[0],
      setError = _useState6[1];

  var _useState7 = (0, _react.useState)(""),
      _useState8 = _slicedToArray(_useState7, 2),
      errorText = _useState8[0],
      setErrorText = _useState8[1];

  var wrapperRef = (0, _react.useRef)();

  var _useState9 = (0, _react.useState)(false),
      _useState10 = _slicedToArray(_useState9, 2),
      validateEnable = _useState10[0],
      setvalidateEnable = _useState10[1];

  var _useState11 = (0, _react.useState)(false),
      _useState12 = _slicedToArray(_useState11, 2),
      isTrue = _useState12[0],
      setIsTrue = _useState12[1];

  var _useState13 = (0, _react.useState)(line || 1),
      _useState14 = _slicedToArray(_useState13, 2),
      textAreaRows = _useState14[0],
      setTextAreaRows = _useState14[1];

  var _useState15 = (0, _react.useState)(false),
      _useState16 = _slicedToArray(_useState15, 2),
      showTopShadow = _useState16[0],
      setShowTopShadow = _useState16[1];

  var _useState17 = (0, _react.useState)(false),
      _useState18 = _slicedToArray(_useState17, 2),
      showBottomShadow = _useState18[0],
      setShowBottomShadow = _useState18[1];

  var handleShadowVisible = function handleShadowVisible() {
    setTimeout(function () {
      if (realInputRef.current) {
        if (Math.abs(realInputRef.current.scrollHeight - realInputRef.current.offsetHeight) <= 3) {
          setShowTopShadow(false);
          setShowBottomShadow(false);
        } else if (realInputRef.current.scrollTop > 0 && Math.abs(realInputRef.current.scrollHeight - realInputRef.current.scrollTop - realInputRef.current.offsetHeight) > 3) {
          setShowTopShadow(true);
          setShowBottomShadow(true);
        } else if (realInputRef.current.scrollTop > 0) {
          setShowTopShadow(true);
          setShowBottomShadow(false);
        } else {
          setShowTopShadow(false);
          setShowBottomShadow(true);
        }
      }
    }, 0);
  };

  var resetTextareaHeight = function resetTextareaHeight() {
    setTimeout(function () {
      var rows = realInputRef.current && Math.ceil(realInputRef.current.scrollHeight / 22);

      if (rows && rows <= maxLine) {
        setTextAreaRows(rows);
      } else {
        setTextAreaRows(maxLine);
      }
    }, 0);
  }; // 鼠标移入


  var changeMoveOver = function changeMoveOver() {
    setIsTrue(true);
  }; // 鼠标移出


  var changeMoveOut = function changeMoveOut() {
    setIsTrue(false);
  };

  var areaNormal = isTrue ? "".concat(prefixCls, "-area-focus") : "".concat(prefixCls, "-area-normal");
  var areaFocus = active ? "".concat(prefixCls, "-area-focus") : areaNormal;
  var areaError = error ? "".concat(prefixCls, "-area-error") : areaFocus;
  (0, _react.useEffect)(function () {
    if (!validateEnable) return;
    var hasError = false;
    var eText = "";

    if (maxLength !== undefined) {
      if (realValue.length > maxLength) {
        hasError = true;
        eText = "\u5DF2\u8F93\u5165 ".concat(realValue.length, " \u4E2A\u5B57\uFF0C\u6700\u591A\u53EF\u8F93\u5165 ").concat(maxLength, " \u4E2A\u5B57");
      }
    }

    if (!hasError && validator) {
      var _validator = validator(realValue),
          isValid = _validator.isValid,
          _errorText = _validator.errorText;

      if (!isValid) {
        hasError = true;
        eText = _errorText || "";
      }
    }

    setErrorText(eText);

    if (hasError !== error) {
      if (onErrorChange) {
        onErrorChange(hasError);
      }

      setError(hasError);
    }
  }, [realValue]);
  (0, _react.useEffect)(function () {
    resetTextareaHeight();
  }, [changeEditStatus]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefixCls)
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefixCls, "-content ").concat(areaError, " ").concat(disabled ? " disabled" : ""),
    onFocus: function onFocus() {},
    onBlur: function onBlur() {},
    onMouseOver: changeMoveOver,
    onMouseOut: changeMoveOut,
    ref: wrapperRef,
    onClick: function onClick() {
      var _realInputRef$current;

      (_realInputRef$current = realInputRef.current) === null || _realInputRef$current === void 0 ? void 0 : _realInputRef$current.focus();
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames2["default"])("".concat(prefixCls, "-content-inner scrollbar-size-sm"), (_classnames = {}, _defineProperty(_classnames, "".concat(prefixCls, "-content-top-shadow"), showTopShadow), _defineProperty(_classnames, "".concat(prefixCls, "-content-bottom-shadow"), showBottomShadow), _classnames))
  }, /*#__PURE__*/_react["default"].createElement("textarea", _extends({
    className: "".concat(className, " scrollbar-size-sm"),
    disabled: disabled,
    ref: realInputRef,
    value: realValue,
    rows: textAreaRows // rows={4}
    ,
    placeholder: placeholder,
    onScroll: handleShadowVisible,
    onChange: function onChange(e) {
      if (!isControlled) {
        setSelfValue(e.target.value);
      }

      if (_onChange) {
        _onChange(e);
      }

      resetTextareaHeight();
      setvalidateEnable(true);
    },
    onFocus: function onFocus(e) {
      setActive(true);

      if (_onFocus) {
        _onFocus(e);
      }
    },
    onKeyDown: function onKeyDown(e) {
      if (_onKeyDown) {
        _onKeyDown(e);
      }

      if (e.keyCode === 13 && disabledEnter) {
        e.preventDefault();
      }
    },
    onBlur: function onBlur(e) {
      setActive(false);

      if (_onBlur) {
        _onBlur(e);
      }
    }
  }, others)))), error && /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefixCls, "-err-color")
  }, errorText));
};

var _default = Textarea;
exports["default"] = _default;