"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.MaskedInputDataTids = exports.InternalMaskedInput = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));var _react = _interopRequireDefault(require("react"));

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

var _utils = require("../../lib/utils");
var _ThemeContext = require("../../lib/theming/ThemeContext");

var _MaskCharLowLine = require("../MaskCharLowLine");
var _Emotion = require("../../lib/theming/Emotion");
var _createPropsGetter = require("../../lib/createPropsGetter");

var _InternalMaskedInput2 = require("./InternalMaskedInput.styles");var _excluded = ["maskChar", "alwaysShowMask", "hasLeftIcon", "hasRightIcon", "maxLength", "onValueChange", "onUnexpectedInput", "defaultValue", "style"];
























var MaskedInputDataTids = exports.MaskedInputDataTids = {
  root: 'MaskedInput__root'
};

/** @deprecated Со следующей мажорной версии Input перестанет поддерживать маску.
 * todo: выпилить в 5 версии библиотеки.
 * */var
InternalMaskedInput = exports.InternalMaskedInput = /*#__PURE__*/function (_React$PureComponent) {













  function InternalMaskedInput(_props) {var _this;
    _this = _React$PureComponent.call(this, _props) || this;_this.getProps = (0, _createPropsGetter.createPropsGetter)(InternalMaskedInput.defaultProps);_this.input = null;_this.reactInputMask = null;_this.






















































































    getValue = function (props) {
      if ((0, _utils.isNonNullable)(props.value)) {
        return props.value.toString();
      }

      if ((0, _utils.isNonNullable)(props.defaultValue)) {
        return props.defaultValue.toString();
      }

      return '';
    };_this.

    refInput = function (input) {
      _this.input = input;
    };_this.

    refMaskedInput = function (reactInputMask) {
      _this.reactInputMask = reactInputMask;
    };_this.

    handleChange = function (event) {
      if (event.target.value === _this.state.value) {
        _this.handleUnexpectedInput();
      } else {
        _this.setState({ value: event.target.value, originValue: event.target.value });
        if (_this.props.onValueChange) {
          _this.props.onValueChange(event.target.value);
        }
        if (_this.props.onChange) {
          _this.props.onChange(event);
        }
      }
    };_this.

    handleFocus = function (event) {
      _this.setState({ focused: true });

      if (_this.props.onFocus) {
        _this.props.onFocus(event);
      }
    };_this.

    handleBlur = function (event) {
      _this.setState({ focused: false });

      if (_this.props.onBlur) {
        _this.props.onBlur(event);
      }
    };_this.

    preprocess = function (
    newState,
    oldState,
    userInput,
    options)
    {
      var visibleMaskChars = new Array(options.mask.length).fill(_this.getProps().maskChar);

      if (newState.value !== oldState.value && userInput === null) {
        _this.setState({
          value: newState.value,
          originValue: newState.value
        });
      }

      options.mask.split('').forEach(function (char, index) {
        // @ts-expect-error InternalMaskedInput will be deleted in 5.0
        if (options.permanents.includes(index)) {
          visibleMaskChars[index] = char;
        }

        if (newState.value[index]) {
          visibleMaskChars[index] = newState.value[index];
        }
      });

      var emptyValue = visibleMaskChars.join('');

      if (_this.state.emptyValue !== emptyValue) {
        _this.setState({
          emptyValue: emptyValue
        });
      }

      return newState;
    };_this.

    isMaskVisible = function () {return _this.props.alwaysShowMask || _this.state.focused;};_this.

    handleUnexpectedInput = function () {
      if (_this.props.onUnexpectedInput) {
        _this.props.onUnexpectedInput(_this.state.value);
      }
    };_this.state = { value: _this.getValue(_props), originValue: _this.getValue(_props), emptyValue: '', focused: false };return _this;}(0, _inheritsLoose2.default)(InternalMaskedInput, _React$PureComponent);var _proto = InternalMaskedInput.prototype;_proto.componentDidMount = function componentDidMount() {if (this.reactInputMask) {// FIXME: принудительно вызываем beforeMaskedValueChange, чтобы получить emptyValue
      this.reactInputMask.forceUpdate();}};_proto.componentDidUpdate = function componentDidUpdate(prevProps) {if (this.props.value !== prevProps.value) {this.setState({ value: this.props.value ? this.props.value.toString() : '' });}};_proto.render = function render() {var _this2 = this;return /*#__PURE__*/_react.default.createElement(_ThemeContext.ThemeContext.Consumer, null, function (theme) {_this2.theme = theme;return _this2.renderMain();});};_proto.renderMain = function renderMain() {var _this$props = this.props,maskChar = _this$props.maskChar,alwaysShowMask = _this$props.alwaysShowMask,hasLeftIcon = _this$props.hasLeftIcon,hasRightIcon = _this$props.hasRightIcon,maxLength = _this$props.maxLength,onValueChange = _this$props.onValueChange,onUnexpectedInput = _this$props.onUnexpectedInput,defaultValue = _this$props.defaultValue,style = _this$props.style,inputProps = (0, _objectWithoutPropertiesLoose2.default)(_this$props, _excluded);var _this$state = this.state,emptyValue = _this$state.emptyValue,value = _this$state.value,originValue = _this$state.originValue;var leftHelper = (style == null ? void 0 : style.textAlign) !== 'right' && /*#__PURE__*/_react.default.createElement("span", { style: { color: 'transparent' } }, emptyValue.slice(0, originValue.length));var leftClass = (style == null ? void 0 : style.textAlign) !== 'right' && _InternalMaskedInput2.styles.inputMaskLeft();var rightHelper = emptyValue.slice(originValue.length).split('').map(function (_char, i) {return _char === '_' ? /*#__PURE__*/_react.default.createElement(_MaskCharLowLine.MaskCharLowLine, { key: i }) : _char;});return /*#__PURE__*/_react.default.createElement("span", { "data-tid": MaskedInputDataTids.root, className: _InternalMaskedInput2.styles.container(), "x-ms-format-detection": "none" }, /*#__PURE__*/_react.default.createElement(_reactInputMask.default, (0, _extends2.default)({}, inputProps, { maskChar: null // @ts-expect-error InternalMaskedInput will be deleted in 5.0
        , beforeMaskedValueChange: this.preprocess, alwaysShowMask: false, onChange: this.handleChange, onFocus: this.handleFocus, onBlur: this.handleBlur, value: value, inputRef: this.refInput, ref: this.refMaskedInput, style: (0, _extends2.default)({}, style) })), this.isMaskVisible() && /*#__PURE__*/_react.default.createElement("span", { className: (0, _Emotion.cx)(_InternalMaskedInput2.styles.inputMask(this.theme), leftClass), translate: "no" }, leftHelper, rightHelper));};return InternalMaskedInput;}(_react.default.PureComponent);InternalMaskedInput.__KONTUR_REACT_UI__ = 'InternalMaskedInput';InternalMaskedInput.displayName = 'InternalMaskedInput';InternalMaskedInput.defaultProps = { maskChar: '_' };