"use strict";

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

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

var _randomstring = _interopRequireDefault(require("randomstring"));

var _styledComponents = _interopRequireWildcard(require("styled-components"));

var _textarea = require("./textarea.proptypes");

var _primitives = require("../../../primitives");

var _fieldLabel = require("../../typography/field-label");

var _hideAccessibly = require("../../../primitives/hide-accessibly");

var _branding = require("../../../mixins/branding");

var _typography = require("../../../mixins/typography");

var _focus = require("../../../mixins/focus");

var _formInput = require("../form-input/form-input.css-rules");

var _textarea2 = require("./textarea.css-rules");

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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 StyledTextarea = _styledComponents["default"].textarea.attrs(function () {
  return {
    fontSize: 'f1'
  };
}).withConfig({
  displayName: "StyledTextarea",
  componentId: "sc-1aovv2s-0"
})(["width:100%;height:", ";color:", ";font-family:", ";font-size:", ";font-weight:", ";border-radius:6px;border:1px solid ", ";padding:10px 20px;resize:", ";", " ", "{border:1px solid ", ";}&:not([disabled]):hover{border:1px solid ", ";}"], _textarea2.getTextareaHeight, _formInput.getTextColor, _typography.getFontFamily, _typography.getFontSize, _typography.getFontWeight, (0, _branding.getPaletteColor)('greyscale-light'), _textarea2.getResizable, (0, _focus.getBaseFocusState)({
  hoverTransition: false,
  activeTransition: false,
  pointerCursor: false
}), _focus.FOCUS_VISIBLE_SELECTOR, (0, _branding.getPaletteColor)('greyscale-darker'), (0, _branding.getPaletteColor)('greyscale-medium'));

var _TextArea = function _TextArea(_ref) {
  var _ref$id = _ref.id,
      id = _ref$id === void 0 ? '' : _ref$id,
      placeholder = _ref.placeholder,
      label = _ref.label,
      value = _ref.value,
      disabled = _ref.disabled,
      focusOnMount = _ref.focusOnMount,
      _ref$resize = _ref.resize,
      resize = _ref$resize === void 0 ? false : _ref$resize,
      _ref$hideLabel = _ref.hideLabel,
      hideLabel = _ref$hideLabel === void 0 ? false : _ref$hideLabel,
      height = _ref.height,
      _ref$onChange = _ref.onChange,
      onChange = _ref$onChange === void 0 ? function (_value, _event) {} : _ref$onChange,
      _ref$onBlur = _ref.onBlur,
      onBlur = _ref$onBlur === void 0 ? function () {} : _ref$onBlur,
      _ref$onFocus = _ref.onFocus,
      onFocus = _ref$onFocus === void 0 ? function () {} : _ref$onFocus,
      _ref$className = _ref.className,
      className = _ref$className === void 0 ? '' : _ref$className;
  var textareaId = "textarea-".concat(id, "-").concat(_randomstring["default"].generate(5));

  var _useState = (0, _react.useState)(value),
      _useState2 = _slicedToArray(_useState, 2),
      managedValue = _useState2[0],
      setValue = _useState2[1];

  var handleChange = function handleChange(evt) {
    var newValue = evt.target.value;
    onChange(newValue, evt);
    setValue(newValue);
  };

  var textareaRef = (0, _react.useRef)();
  (0, _react.useLayoutEffect)(function () {
    if (focusOnMount && textareaRef.current) {
      textareaRef.current.focus();
    }
  }, [focusOnMount]);
  return _react["default"].createElement(_primitives.Box, {
    stacked: "column",
    className: className
  }, hideLabel ? _react["default"].createElement(_hideAccessibly.HideAccessibly, null, _react["default"].createElement(_fieldLabel.FieldLabel, {
    htmlFor: textareaId
  }, label)) : _react["default"].createElement(_fieldLabel.FieldLabel, {
    htmlFor: textareaId
  }, label), _react["default"].createElement(StyledTextarea, {
    ref: textareaRef,
    id: textareaId,
    resize: resize,
    placeholder: placeholder,
    value: managedValue,
    height: height,
    focusOnMount: focusOnMount,
    onChange: handleChange,
    onBlur: onBlur,
    onFocus: onFocus,
    disabled: disabled
  }));
};

var TextArea = (0, _styledComponents.withTheme)(_TextArea);
exports.TextArea = TextArea;
TextArea.displayName = 'TextArea';
TextArea.propTypes = _textarea.textAreaPropTypes;