var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { Component } from 'react';
import styled from 'styled-components';
import { ReactComponent as XIcon } from "../../assets/x.svg";
import { forRef } from "../../decorators";
import { BoxBlock } from "../../modules";
import { debounceFn, isSetField, omit } from "../../utils";
import { AlignCenter } from "../../globalStyled";
export const ClearIconSVG = styled(XIcon) `
  cursor: pointer;
  display: block;
  width: 10px;
  height: 10px;
  margin-top: 2px; // так надо

  &:hover {
    opacity: 0.8;
  }
`;
export const ClearIconDiv = styled(BoxBlock) `
  margin-left: 6px; // так надо
  width: 14px;
  height: 14px;
`;
export const InputDiv = styled(BoxBlock) `
  ${AlignCenter}
  flex: 0 0 100%;
`;
class DebouncedFieldC extends Component {
    constructor(props) {
        super(props);
        this.$ref = React.createRef();
        this.onChange = (event) => {
            const { maxLength, max, minLength, onChange } = this.props;
            const { value } = event.currentTarget;
            const allowedValue = this.checkByMask(value);
            if (!allowedValue || (max && Number(value) > max) || (maxLength && value.length > maxLength)) {
                return false;
            }
            this.setState({ value }, () => {
                const { value: newValue } = this.state;
                if (minLength && newValue.length >= minLength) {
                    this.debouncedOuterChange(newValue);
                }
                else {
                    onChange(newValue);
                }
            });
        };
        this.onOuterChange = () => {
            const { onChange } = this.props;
            const { value } = this.state;
            onChange(value);
        };
        this.clearValue = () => {
            const { onChange, clearValue } = this.props;
            const value = '';
            this.setState({ value });
            onChange(value);
            if (clearValue) {
                clearValue(value);
            }
        };
        const value = isSetField(props.value) ? props.value : '';
        this.state = { value };
        this.debouncedOuterChange = debounceFn(this.onOuterChange, props.debounceTimeout);
    }
    componentDidUpdate(prevProps, prevState) {
        const { value } = this.props;
        const isUpdatedValue = !prevState.value && prevState.value !== value;
        if (isUpdatedValue) {
            this.setState({ value });
        }
    }
    checkByMask(val) {
        const { mask } = this.props;
        if (!mask) {
            return true;
        }
        return mask.test(val);
    }
    render() {
        const _a = this.props, { element = 'input', clearValue } = _a, props = __rest(_a, ["element", "clearValue"]);
        const { value } = this.state;
        const innerProps = omit(props, 'onChange', 'debounceTimeout', 'autoFocus');
        const Element = element;
        return (React.createElement(InputDiv, null,
            React.createElement(Element, Object.assign({}, innerProps, { onChange: this.onChange, value: value || '', ref: this.$ref })),
            clearValue && (React.createElement(ClearIconDiv, null, value && React.createElement(ClearIconSVG, { onClick: this.clearValue })))));
    }
}
DebouncedFieldC.defaultProps = {
    debounceTimeout: 300,
    minLength: 2,
};
export default forRef(DebouncedFieldC);
