'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var React = require('react');
var classnames = require('classnames');
var useId = require('./useId-DXHUzdxd.js');
require('react-router-dom');
require('@bigbinary/neeto-hotkeys');
require('./overlayManager.js');
var index = require('./index-CNZKW5X0.js');
require('qs');
var Label = require('./Label.js');
var jsxRuntime = require('react/jsx-runtime');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var _toConsumableArray__default = /*#__PURE__*/_interopDefault(_toConsumableArray);
var _slicedToArray__default = /*#__PURE__*/_interopDefault(_slicedToArray);
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefault(_objectWithoutProperties);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);

var useSyncedRef = function useSyncedRef(ref) {
  var innerRef = React.useRef();
  React.useEffect(function () {
    if (!ref) return;
    if (typeof ref === "function") {
      ref(innerRef.current);
    } else {
      ref.current = innerRef.current;
    }
  });
  return innerRef;
};

var _excluded = ["size", "resize", "suffix", "prefix", "disabled", "required", "nakedTextarea", "helpText", "error", "label", "className", "maxLength", "unlimitedChars", "labelProps", "onBlur", "disableTrimOnBlur"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty__default.default(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var SIZES = {
  small: "small",
  medium: "medium",
  large: "large"
};
var ROWS = {
  small: 1,
  medium: 3,
  large: 4
};
var RESIZE = {
  vertical: "vertical",
  none: "none"
};
var Textarea = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var _ref2, _otherProps$value, _otherProps$onChange;
  var _ref$size = _ref.size,
    size = _ref$size === void 0 ? SIZES.medium : _ref$size,
    _ref$resize = _ref.resize,
    resize = _ref$resize === void 0 ? RESIZE.vertical : _ref$resize,
    _ref$suffix = _ref.suffix,
    suffix = _ref$suffix === void 0 ? null : _ref$suffix,
    _ref$prefix = _ref.prefix,
    prefix = _ref$prefix === void 0 ? null : _ref$prefix,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    _ref$required = _ref.required,
    required = _ref$required === void 0 ? false : _ref$required,
    _ref$nakedTextarea = _ref.nakedTextarea,
    nakedTextarea = _ref$nakedTextarea === void 0 ? false : _ref$nakedTextarea,
    _ref$helpText = _ref.helpText,
    helpText = _ref$helpText === void 0 ? "" : _ref$helpText,
    _ref$error = _ref.error,
    error = _ref$error === void 0 ? "" : _ref$error,
    _ref$label = _ref.label,
    label = _ref$label === void 0 ? "" : _ref$label,
    _ref$className = _ref.className,
    className = _ref$className === void 0 ? "" : _ref$className,
    maxLength = _ref.maxLength,
    _ref$unlimitedChars = _ref.unlimitedChars,
    unlimitedChars = _ref$unlimitedChars === void 0 ? false : _ref$unlimitedChars,
    labelProps = _ref.labelProps,
    onBlur = _ref.onBlur,
    _ref$disableTrimOnBlu = _ref.disableTrimOnBlur,
    disableTrimOnBlur = _ref$disableTrimOnBlu === void 0 ? false : _ref$disableTrimOnBlu,
    otherProps = _objectWithoutProperties__default.default(_ref, _excluded);
  var _useState = React.useState(""),
    _useState2 = _slicedToArray__default.default(_useState, 2),
    valueInternal = _useState2[0],
    setValueInternal = _useState2[1];
  var value = (_ref2 = (_otherProps$value = otherProps.value) !== null && _otherProps$value !== void 0 ? _otherProps$value : valueInternal) !== null && _ref2 !== void 0 ? _ref2 : "";
  var id = useId.useId(otherProps.id);
  var errorId = "error_".concat(id);
  var helpTextId = "helpText_".concat(id);
  var textareaRef = useSyncedRef(ref);
  var describedByIds = [].concat(_toConsumableArray__default.default(error ? [errorId] : []), _toConsumableArray__default.default(helpText ? [helpTextId] : [])).join(" ");
  var valueLength = (value === null || value === void 0 ? void 0 : value.toString().length) || 0;
  var isCharacterLimitVisible = valueLength >= maxLength * 0.85;
  var maxLengthError = unlimitedChars && valueLength > maxLength;
  var onChangeInternal = function onChangeInternal(e) {
    return setValueInternal(e.target.value);
  };
  var onChange = (_otherProps$onChange = otherProps.onChange) !== null && _otherProps$onChange !== void 0 ? _otherProps$onChange : onChangeInternal;
  var isMaxLengthPresent = !!maxLength || maxLength === 0;
  React.useEffect(function () {
    textareaRef.current.style.minHeight = "22px";
    textareaRef.current.style.height = "auto";
    var scrollHeight = textareaRef.current.scrollHeight;
    textareaRef.current.style.height = "".concat(scrollHeight + 1, "px");
  }, [value]);
  var handleTrimmedChangeOnBlur = function handleTrimmedChangeOnBlur(e) {
    if (disableTrimOnBlur || typeof value !== "string") return;
    var trimmedValue = value.trim();
    if (value === trimmedValue) return;
    e.target.value = trimmedValue;
    onChange(e);
  };
  var handleOnBlur = function handleOnBlur(e) {
    handleTrimmedChangeOnBlur(e);
    onBlur === null || onBlur === void 0 ? void 0 : onBlur(e);
  };
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    className: classnames__default.default(["neeto-ui-input__wrapper", className]),
    "data-testid": "nui-textarea-wrapper",
    children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
      className: "neeto-ui-input__label-wrapper",
      children: [label && /*#__PURE__*/jsxRuntime.jsx(Label, _objectSpread(_objectSpread({
        required: required,
        "data-testid": "".concat(index.hyphenize(label), "-label"),
        htmlFor: id
      }, labelProps), {}, {
        children: label
      })), isCharacterLimitVisible && /*#__PURE__*/jsxRuntime.jsxs("p", {
        className: classnames__default.default("neeto-ui-input__max-length", {
          "neeto-ui-input__max-length--error": maxLengthError
        }),
        children: [valueLength, "/", maxLength]
      })]
    }), /*#__PURE__*/jsxRuntime.jsxs("div", {
      "data-testid": "".concat(index.hyphenize(label), "-text-input-label"),
      className: classnames__default.default("neeto-ui-input", "neeto-ui-input--textarea", {
        "neeto-ui-input--error": !!error,
        "neeto-ui-input--disabled": !!disabled,
        "neeto-ui-input--naked": !!nakedTextarea,
        "neeto-ui-input--small": size === SIZES.small,
        "neeto-ui-input--medium": size === SIZES.medium,
        "neeto-ui-input--large": size === SIZES.large,
        "neeto-ui-input--resize--vertical": resize === RESIZE.vertical,
        "neeto-ui-input--resize--none": resize === RESIZE.none
      }),
      children: [prefix && /*#__PURE__*/jsxRuntime.jsx("div", {
        className: "neeto-ui-input__prefix",
        children: prefix
      }), /*#__PURE__*/jsxRuntime.jsx("textarea", _objectSpread(_objectSpread({
        "data-testid": "".concat(index.hyphenize(label), "-text-input"),
        ref: textareaRef,
        rows: ROWS[size]
      }, _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, describedByIds && {
        "aria-describedby": describedByIds
      }), {}, {
        "aria-invalid": !!error,
        "aria-required": required,
        disabled: disabled
      }, isMaxLengthPresent && !unlimitedChars && {
        maxLength: maxLength
      }), otherProps), {}, {
        onChange: onChange,
        value: value
      })), {}, {
        onBlur: handleOnBlur
      })), suffix && /*#__PURE__*/jsxRuntime.jsx("div", {
        className: "neeto-ui-input__suffix",
        children: suffix
      })]
    }), !!error && /*#__PURE__*/jsxRuntime.jsx("p", {
      className: "neeto-ui-input__error",
      "data-testid": "".concat(index.hyphenize(label), "-input-error"),
      id: errorId,
      children: error
    }), helpText && /*#__PURE__*/jsxRuntime.jsx("p", {
      className: "neeto-ui-input__help-text",
      id: helpTextId,
      children: helpText
    })]
  });
});
Textarea.displayName = "Textarea";

exports.Textarea = Textarea;
exports.useSyncedRef = useSyncedRef;
//# sourceMappingURL=Textarea-DxdJutA8.js.map
