"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; 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 = exports.StyledTextarea = void 0;

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

var _uuid = require("uuid");

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

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 _textArea = require("./text-area.css-rules");

var _spacing = require("../../../mixins/spacing");

var _stackee = require("../../../mixins/stackee");

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 _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 _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) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) 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; }

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 StyledTextarea = _styledComponents["default"].textarea.attrs(function () {
  return {
    fontSize: 'f1'
  };
}).withConfig({
  displayName: "StyledTextarea",
  componentId: "sc-3stucl-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 ", ";}&::placeholder{color:", ";font-style:italic;opacity:1;}&&::-ms-input-placeholder{font-style:italic;color:", ";}&&:-ms-input-placeholder{font-style:italic;color:", ";}"], _textArea.getTextareaHeight, _textArea.getTextColor, _typography.getFontFamily, _typography.getFontSize, _typography.getFontWeight, (0, _branding.getPaletteColor)('greyscale-light'), _textArea.getResizable, (0, _focus.getBaseFocusState)({
  hoverTransition: false,
  activeTransition: false,
  pointerCursor: false
}), _focus.FOCUS_VISIBLE_SELECTOR, (0, _branding.getPaletteColor)('greyscale-dark'), (0, _branding.getPaletteColor)('greyscale-medium'), (0, _branding.getPaletteColor)('greyscale-dark'), (0, _branding.getPaletteColor)('greyscale-dark'), (0, _branding.getPaletteColor)('greyscale-dark'));

exports.StyledTextarea = StyledTextarea;
var TextAreaContainer = (0, _styledComponents["default"])(_primitives.Box).withConfig({
  displayName: "TextAreaContainer",
  componentId: "sc-3stucl-1"
})(["&&&{", ";}", ";"], _spacing.getSpacingRules, _stackee.getStackeeRules);

var TextArea = function TextArea(_ref) {
  var _ref$id = _ref.id,
      id = _ref$id === void 0 ? '' : _ref$id,
      name = _ref.name,
      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,
      className = _ref.className,
      spacing = _ref.spacing,
      grow = _ref.grow,
      shrink = _ref.shrink,
      basis = _ref.basis,
      dataQa = _ref['data-qa'],
      dataCy = _ref['data-cy'],
      props = _objectWithoutProperties(_ref, ["id", "name", "placeholder", "label", "value", "disabled", "focusOnMount", "resize", "hideLabel", "height", "onChange", "onBlur", "onFocus", "className", "spacing", "grow", "shrink", "basis", "data-qa", "data-cy"]);

  var textareaId = (0, _react.useMemo)(function () {
    return "textarea-".concat(id, "-").concat((0, _uuid.v4)());
  }, [id]);

  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 /*#__PURE__*/_react["default"].createElement(TextAreaContainer, _extends({
    stacked: "column",
    className: className,
    spacing: spacing,
    grow: grow,
    shrink: shrink,
    basis: basis,
    "data-cy": dataCy,
    "data-qa": dataQa,
    id: id
  }, props), hideLabel ? /*#__PURE__*/_react["default"].createElement(_hideAccessibly.HideAccessibly, null, /*#__PURE__*/_react["default"].createElement(_fieldLabel.FieldLabel, {
    htmlFor: textareaId
  }, label)) : /*#__PURE__*/_react["default"].createElement(_fieldLabel.FieldLabel, {
    htmlFor: textareaId
  }, label), /*#__PURE__*/_react["default"].createElement(StyledTextarea, {
    ref: textareaRef,
    id: textareaId,
    name: name,
    resize: resize,
    placeholder: placeholder,
    value: managedValue,
    height: height,
    focusOnMount: focusOnMount,
    onChange: handleChange,
    onBlur: onBlur,
    onFocus: onFocus,
    disabled: disabled
  }));
};

exports.TextArea = TextArea;
TextArea.displayName = 'TextArea';