"use strict";

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

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

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

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

var _lodash = require("lodash");

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

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

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

var _forms = require("../forms.common");

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

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

var _maskedTextInput = require("../masked-text-input");

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

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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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

var ErrorLabelWithMargin = (0, _styledComponents["default"])(_forms.ErrorLabel).attrs(function () {
  return {
    fontSize: 'f1'
  };
}).withConfig({
  displayName: "ErrorLabelWithMargin",
  componentId: "sc-18vt5gs-0"
})(["font-family:", ";font-size:", ";line-height:1.5;"], _typography.getFontFamily, _typography.getFontSize); // FireFox doesn't respect the align-items: stretch property on
// inputs for some reason. See PAT-202.

var FullWidthFormInput = (0, _styledComponents["default"])(_formInput.FormInput).withConfig({
  displayName: "FullWidthFormInput",
  componentId: "sc-18vt5gs-1"
})(["width:100%;", ""], (0, _focus.getBaseFocusState)({
  hoverTransition: false,
  activeTransition: false,
  pointerCursor: false
}));

var generateErrorString = function generateErrorString(error) {
  if ((0, _lodash.isArray)(error)) return /*#__PURE__*/_react["default"].createElement(ErrorLabelWithMargin, null, error.join(', '));
  if (typeof error === 'string' || _typeof(error) === 'object' && error != null) return /*#__PURE__*/_react["default"].createElement(ErrorLabelWithMargin, null, error);
};

var _TextInput = function _TextInput(_ref2) {
  var _ref2$id = _ref2.id,
      id = _ref2$id === void 0 ? '' : _ref2$id,
      type = _ref2.type,
      placeholder = _ref2.placeholder,
      iconType = _ref2.iconType,
      iconPosition = _ref2.iconPosition,
      label = _ref2.label,
      value = _ref2.value,
      disabled = _ref2.disabled,
      focusOnMount = _ref2.focusOnMount,
      buttonText = _ref2.buttonText,
      error = _ref2.error,
      mask = _ref2.mask,
      maskOptions = _ref2.maskOptions,
      _ref2$large = _ref2.large,
      large = _ref2$large === void 0 ? false : _ref2$large,
      _ref2$hideLabel = _ref2.hideLabel,
      hideLabel = _ref2$hideLabel === void 0 ? false : _ref2$hideLabel,
      _ref2$hideBorder = _ref2.hideBorder,
      hideBorder = _ref2$hideBorder === void 0 ? false : _ref2$hideBorder,
      inputMode = _ref2.inputMode,
      _ref2$onChange = _ref2.onChange,
      onChange = _ref2$onChange === void 0 ? function (_value, _event) {} : _ref2$onChange,
      _ref2$onBlur = _ref2.onBlur,
      onBlur = _ref2$onBlur === void 0 ? function () {} : _ref2$onBlur,
      _ref2$onFocus = _ref2.onFocus,
      onFocus = _ref2$onFocus === void 0 ? function () {} : _ref2$onFocus,
      _ref2$onRef = _ref2.onRef,
      onRef = _ref2$onRef === void 0 ? function (_ref) {} : _ref2$onRef,
      _ref2$buttonPress = _ref2.buttonPress,
      buttonPress = _ref2$buttonPress === void 0 ? function () {} : _ref2$buttonPress,
      _ref2$className = _ref2.className,
      className = _ref2$className === void 0 ? '' : _ref2$className;
  var inputId = "input-".concat(id, "-").concat(_randomstring["default"].generate(5));
  var errorString = generateErrorString(error);
  var hasError = !!errorString || error === true;
  var icon = hasError ? 'error' : iconType;
  var position = hasError ? 'right' : iconPosition;
  return /*#__PURE__*/_react["default"].createElement(_primitives.Box, {
    stacked: "column",
    className: className
  }, hideLabel ? /*#__PURE__*/_react["default"].createElement(_hideAccessibly.HideAccessibly, null, /*#__PURE__*/_react["default"].createElement(_fieldLabel.FieldLabel, {
    htmlFor: inputId
  }, label)) : /*#__PURE__*/_react["default"].createElement(_fieldLabel.FieldLabel, {
    htmlFor: inputId,
    large: large
  }, label), mask ? /*#__PURE__*/_react["default"].createElement(_maskedTextInput.MaskedTextInput, {
    id: inputId,
    type: type,
    placeholder: placeholder,
    value: value,
    error: error,
    focusOnMount: focusOnMount,
    onChange: onChange,
    onBlur: onBlur,
    onFocus: onFocus,
    onRef: onRef,
    large: large,
    disabled: disabled,
    iconType: icon,
    iconPosition: position,
    options: mask ? maskOptions : undefined,
    inputMode: inputMode,
    hideBorder: hideBorder
  }) : /*#__PURE__*/_react["default"].createElement(FullWidthFormInput, {
    id: inputId,
    type: type,
    placeholder: placeholder,
    value: value,
    buttonText: buttonText,
    error: error,
    focusOnMount: focusOnMount,
    onChange: onChange,
    onBlur: onBlur,
    onFocus: onFocus,
    onRef: onRef,
    buttonPress: buttonPress,
    large: large,
    disabled: disabled,
    iconType: icon,
    iconPosition: position,
    options: mask ? maskOptions : undefined,
    inputMode: inputMode,
    hideBorder: hideBorder
  }), errorString);
};

var TextInput = (0, _styledComponents.withTheme)(_TextInput);
exports.TextInput = TextInput;
TextInput.displayName = 'TextInput';