"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = MaskedInput;

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

var _reactInputMask = _interopRequireDefault(require("react-input-mask"));

var _input = _interopRequireDefault(require("./input"));

var _styledComponents = require("./styled-components");

var _excluded = ["startEnhancer", "endEnhancer", "error", "positive", "onChange", "onFocus", "onBlur", "value", "disabled", "readOnly"],
    _excluded2 = ["Input"],
    _excluded3 = ["mask", "maskChar", "overrides"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }

var MaskOverride = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var startEnhancer = _ref.startEnhancer,
      endEnhancer = _ref.endEnhancer,
      error = _ref.error,
      positive = _ref.positive,
      onChange = _ref.onChange,
      onFocus = _ref.onFocus,
      onBlur = _ref.onBlur,
      value = _ref.value,
      disabled = _ref.disabled,
      readOnly = _ref.readOnly,
      restProps = _objectWithoutProperties(_ref, _excluded);

  return /*#__PURE__*/React.createElement(_reactInputMask.default, _extends({
    onChange: onChange,
    onFocus: onFocus,
    onBlur: onBlur,
    value: value,
    disabled: disabled,
    readOnly: readOnly
  }, restProps), function (props) {
    return /*#__PURE__*/React.createElement(_styledComponents.Input, _extends({
      ref: ref,
      onChange: onChange,
      onFocus: onFocus,
      onBlur: onBlur,
      value: value,
      disabled: disabled,
      readOnly: readOnly
    }, props));
  });
});
MaskOverride.displayName = 'MaskOverride';

function MaskedInput(_ref2) {
  var mask = _ref2.mask,
      maskChar = _ref2.maskChar,
      _ref2$overrides = _ref2.overrides;
  _ref2$overrides = _ref2$overrides === void 0 ? {} : _ref2$overrides;

  var _ref2$overrides$Input = _ref2$overrides.Input,
      inputOverride = _ref2$overrides$Input === void 0 ? {} : _ref2$overrides$Input,
      restOverrides = _objectWithoutProperties(_ref2$overrides, _excluded2),
      restProps = _objectWithoutProperties(_ref2, _excluded3);

  var componentOverride = MaskOverride;
  var propsOverride = {};
  var styleOverride = {};

  if (typeof inputOverride === 'function') {
    componentOverride = inputOverride;
  } else if (_typeof(inputOverride) === 'object') {
    componentOverride = inputOverride.component || componentOverride;
    propsOverride = inputOverride.props || {};
    styleOverride = inputOverride.style || {};
  }

  if (_typeof(propsOverride) === 'object') {
    propsOverride = _objectSpread(_objectSpread({}, propsOverride), {}, {
      mask: propsOverride.mask || mask,
      maskChar: propsOverride.maskChar || maskChar
    });
  }

  var nextOverrides = _objectSpread({
    Input: {
      component: componentOverride,
      props: propsOverride,
      style: styleOverride
    }
  }, restOverrides);

  return /*#__PURE__*/React.createElement(_input.default, _extends({}, restProps, {
    overrides: nextOverrides
  }));
}

MaskedInput.defaultProps = {
  maskChar: ' '
};