"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.MaskedTextInput = void 0;

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

var _react2 = _interopRequireDefault(require("cleave.js/react"));

require("cleave.js/dist/addons/cleave-phone.ca");

require("cleave.js/dist/addons/cleave-phone.gb");

require("cleave.js/dist/addons/cleave-phone.us");

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

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

var _maskedTextInput2 = require("./masked-text-input.css-rules");

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

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

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

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

var StyledWrapper = (0, _styledComponents["default"])(_primitives.Box).withConfig({
  displayName: "StyledWrapper",
  componentId: "sc-1okux8-0"
})(["> input{height:", ";color:", ";font-family:", ";font-size:", ";font-weight:", ";padding-bottom:2px;padding-left:", ";border-radius:6px;border:1px solid ", ";background-image:", ";background-repeat:no-repeat;background-position:", ";width:100%;&:focus{border:1px solid ", ";outline:none;}&&::placeholder{color:", ";opacity:1;}&&::-ms-input-placeholder{color:", ";}&&:-ms-input-placeholder{color:", ";}", " ", "{border:1px solid ", ";}}"], _maskedTextInput2.getInputHeight, _maskedTextInput2.getTextColor, _typography.getFontFamily, _typography.getFontSize, _typography.getFontWeight, _maskedTextInput2.getInputLeftPadding, (0, _branding.getPaletteColor)('greyscale-light'), _maskedTextInput2.getIconDataUri, _maskedTextInput2.getInputIconPosition, (0, _branding.getPaletteColor)('greyscale-medium'), _maskedTextInput2.getPlaceholderColor, _maskedTextInput2.getPlaceholderColor, _maskedTextInput2.getPlaceholderColor, (0, _focus.getBaseFocusState)({
  hoverTransition: false,
  activeTransition: false,
  pointerCursor: false
}), _focus.FOCUS_VISIBLE_SELECTOR, (0, _branding.getPaletteColor)('greyscale-darker'));

var MaskedTextInput = function MaskedTextInput(props) {
  var onRef = props.onRef,
      focusOnMount = props.focusOnMount,
      onChange = props.onChange,
      large = props.large,
      iconType = props.iconType,
      iconPosition = props.iconPosition,
      error = props.error,
      className = props.className;
  var textRef = (0, _react.useRef)(null);
  var registerChild = (0, _react.useCallback)(function (ref) {
    if (onRef != null) {
      onRef(ref);
    }

    textRef.current = ref;
  }, [onRef, textRef]);
  var focusChild = (0, _react.useCallback)(function () {
    if (textRef.current != null) {
      textRef.current.focus();
    }
  }, [textRef]);
  (0, _react.useEffect)(function () {
    if (focusOnMount) {
      focusChild();
    }
  });
  var handleChange = (0, _react.useCallback)(function (event) {
    onChange(event.target.value, event);
  }, [onChange]);
  return _react["default"].createElement(StyledWrapper, {
    className: className,
    fontSize: large ? 'f3' : 'f1',
    bold: large,
    iconType: iconType,
    iconPosition: iconPosition,
    error: error,
    large: large,
    onFocus: props.onFocus,
    onBlur: props.onBlur
  }, _react["default"].createElement(_react2["default"], {
    id: props.id,
    options: props.options,
    htmlRef: registerChild,
    type: props.type || 'text',
    placeholder: props.placeholder || '',
    disabled: props.disabled,
    value: props.value || '',
    onFocus: props.onFocus,
    onBlur: props.onBlur,
    onChange: handleChange,
    inputMode: props.inputMode,
    pattern: props.inputMode === 'numeric' ? '[0-9]*' : null
  }));
};

exports.MaskedTextInput = MaskedTextInput;
MaskedTextInput.displayName = 'MaskedTextInput';
MaskedTextInput.propTypes = _maskedTextInput.maskedTextInputPropTypes;