import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2020-2026 GoodData Corporation
import { PureComponent } from "react";
import { InputPure } from "./InputPure.js";
const isValidNumber = (value) => typeof value === "number" && !Number.isNaN(value);
const isNumberOrString = (value) => isValidNumber(value) || (typeof value === "string" && value.length);
const toValidValue = (value) => (isNumberOrString(value) ? value : "");
/**
 * @internal
 */
export class Input extends PureComponent {
    static defaultProps = {
        ...InputPure.defaultProps,
    };
    inputNodeRef = null;
    constructor(props) {
        super(props);
        const { value } = props;
        this.state = {
            value: toValidValue(value ?? ""),
        };
    }
    UNSAFE_componentWillReceiveProps(nextProps) {
        const validValue = toValidValue(nextProps.value ?? "");
        if (this.props.value !== validValue) {
            this.valueChanged(validValue);
        }
    }
    onChange = (value, e) => {
        this.valueChanged(value, e);
    };
    valueChanged(value, e) {
        if (this.state.value !== value) {
            this.setState({
                value,
            });
            this.props.onChange?.(value, e);
        }
    }
    render() {
        return (_jsx(InputPure, { ...this.props, ref: (ref) => {
                this.inputNodeRef = ref;
            }, onChange: this.onChange, value: this.state.value }));
    }
}
