'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var maskInputCompose = require('@salutejs/react-maskinput');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);
var maskInputCompose__default = /*#__PURE__*/_interopDefault(maskInputCompose);

var _excluded = ["mask", "maskFormat"];
var defaultFormatChars = [{
  str: '*',
  regexp: /./
}, {
  str: '0',
  regexp: /[0-9]/
}, {
  str: 'a',
  regexp: /[a-zA-Z]/
}, {
  str: 'я',
  regexp: /[а-яА-ЯёЁ]/
}];
var composeMask = function composeMask(InputComponent) {
  var MaskedInput = maskInputCompose__default.default(InputComponent);
  return /*#__PURE__*/React.forwardRef(function (_ref, outerRef) {
    var mask = _ref.mask,
      _ref$maskFormat = _ref.maskFormat,
      maskFormat = _ref$maskFormat === void 0 ? defaultFormatChars : _ref$maskFormat,
      rest = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
    // TODO: replace inside react-maskinput 'getReference' on 'forwardRef'
    var _useState = React.useState(null),
      _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
      ref = _useState2[0],
      setRef = _useState2[1];
    React.useImperativeHandle(outerRef, function () {
      return ref;
    }, [ref]);
    if (!mask) {
      return /*#__PURE__*/React__default.default.createElement(InputComponent, _rollupPluginBabelHelpers.extends({
        ref: setRef
      }, rest));
    }
    return /*#__PURE__*/React__default.default.createElement(MaskedInput, _rollupPluginBabelHelpers.extends({
      getReference: setRef,
      mask: mask,
      maskFormat: maskFormat
    }, rest));
  });
};

exports.composeMask = composeMask;
