"use strict";

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

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

var _uuid = require("uuid");

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

var _lodash = require("lodash");

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

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

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

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

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

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

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

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

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

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,
      fontSize = _ref2.fontSize,
      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,
      onBlur = _ref2.onBlur,
      onFocus = _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,
      className = _ref2.className,
      spacing = _ref2.spacing,
      grow = _ref2.grow,
      shrink = _ref2.shrink,
      basis = _ref2.basis,
      hideValueButtonLabel = _ref2.hideValueButtonLabel,
      revealValueButtonLabel = _ref2.revealValueButtonLabel,
      floatLabel = _ref2.floatLabel,
      dataQa = _ref2['data-qa'],
      dataCy = _ref2['data-cy'],
      name = _ref2.name,
      props = _objectWithoutProperties(_ref2, ["id", "type", "placeholder", "iconType", "iconPosition", "label", "value", "disabled", "focusOnMount", "fontSize", "buttonText", "error", "mask", "maskOptions", "large", "hideLabel", "hideBorder", "inputMode", "onChange", "onBlur", "onFocus", "onRef", "buttonPress", "className", "spacing", "grow", "shrink", "basis", "hideValueButtonLabel", "revealValueButtonLabel", "floatLabel", "data-qa", "data-cy", "name"]);

  var inputId = (0, _react.useMemo)(function () {
    return "input-".concat(id, "-").concat((0, _uuid.v4)());
  }, [id]);
  var errorString = generateErrorString(error);
  var hasError = !!errorString || error === true;
  var icon = hasError ? 'error' : iconType;
  var position = hasError ? 'right' : iconPosition;
  var Label = null;

  if (!floatLabel) {
    Label = 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);
  }

  return /*#__PURE__*/_react["default"].createElement(_primitives.Box, _extends({
    stacked: "column",
    className: className
  }, {
    // $FlowFixMe
    spacing: spacing,
    grow: grow,
    shrink: shrink,
    basis: basis
  }, {
    "data-cy": dataCy,
    "data-qa": dataQa,
    id: id
  }, props), Label, mask ? /*#__PURE__*/_react["default"].createElement(_maskedTextInput.MaskedTextInput, {
    id: inputId,
    type: type,
    placeholder: placeholder,
    value: value,
    error: error,
    focusOnMount: focusOnMount,
    fontSize: fontSize,
    onChange: onChange,
    onBlur: onBlur,
    onFocus: onFocus,
    onRef: onRef,
    large: large,
    disabled: disabled,
    iconType: icon,
    iconPosition: position,
    options: mask ? maskOptions : undefined,
    inputMode: inputMode,
    hideBorder: hideBorder,
    floatLabel: floatLabel,
    label: label,
    name: name
  }) : /*#__PURE__*/_react["default"].createElement(FullWidthFormInput, {
    id: inputId,
    type: type,
    placeholder: placeholder,
    value: value,
    buttonText: buttonText,
    error: error,
    focusOnMount: focusOnMount,
    fontSize: fontSize,
    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,
    hideValueButtonLabel: hideValueButtonLabel,
    revealValueButtonLabel: revealValueButtonLabel,
    floatLabel: floatLabel,
    label: label,
    name: name
  }), errorString);
};

exports.TextInput = TextInput;
TextInput.displayName = 'TextInput';