/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Notification
 */
import "./ValidationTextbox.scss";
import classnames from "classnames";
import * as React from "react";
import { Key } from "ts-key-enum";
import { MessageManager } from "../messages/MessageManager";
import { Input } from "@itwin/itwinui-react";
/** Enum for Input Status used in [[ValidationTextbox]]
 * @alpha
 */
export var InputStatus;
(function (InputStatus) {
    InputStatus[InputStatus["Valid"] = 0] = "Valid";
    InputStatus[InputStatus["Invalid"] = 1] = "Invalid";
})(InputStatus || (InputStatus = {}));
/**
 * Input box that validates text based on provided criteria. Defaults to checking
 * for empty if no method for onValueChanged is provided.
 * @alpha
 */
export class ValidationTextbox extends React.PureComponent {
    constructor(props) {
        super(props);
        /**
         * Determines if value is valid and resolves any defined functions.
         * Also will show or hide error message if defined.
         * @param event   Button press event that triggers validation
         */
        this._validateText = (event) => {
            this.processValidateText(event.target);
        };
        /**
         * Manages special key codes by calling user defined functions
         * @param event   Keyup event
         */
        this._handleKeyUp = (event) => {
            this.processValidateText(event.target);
            switch (event.key) {
                case Key.Escape.valueOf():
                    if (this.props.onEscPressed)
                        this.props.onEscPressed();
                    break;
                case Key.Enter.valueOf():
                    if (this.props.onEnterPressed)
                        this.props.onEnterPressed();
                    break;
            }
        };
        this.state = { isValid: true };
    }
    render() {
        const sizeStyle = {
            width: this.props.size ? `${this.props.size.toString()}em` : "12em",
        };
        const divStyle = {
            ...sizeStyle,
            ...this.props.style,
        };
        const validClassNames = classnames("ValidationTextbox-input");
        const invalidClassNames = classnames("ValidationTextbox-input", "ValidationTextbox-invalid");
        return (React.createElement("div", { className: classnames("uifw-ValidationTextbox", this.props.className), style: divStyle },
            React.createElement(Input, { className: this.state.isValid ? validClassNames : invalidClassNames, onChange: this._validateText, onKeyUp: this._handleKeyUp, onPaste: this._validateText, onCut: this._validateText, onBlur: this._validateText, placeholder: this.props.placeholder ? this.props.placeholder : "", style: sizeStyle }),
            this.props.children));
    }
    processValidateText(target) {
        if (undefined === target)
            return;
        const value = target.value;
        const isValid = this._calculateIsValid(value);
        this.setState({ isValid }, () => {
            if (this.state.isValid) {
                this._hideErrorMessage();
            }
            else {
                if (this.props.errorText && !this.state.isValid)
                    this._showErrorMessage(target);
            }
        });
    }
    /**
     * Determines if value provided is valid by calling user defined
     * function. If no function is provided, the default criteria for
     * validity is if the value has been defined.
     * @param value   The value provided in textbox
     */
    _calculateIsValid(value) {
        if (this.props.onValueChanged)
            return this.props.onValueChanged(value) === InputStatus.Valid
                ? true
                : false;
        return value.length > 0;
    }
    /** Hides error message */
    _hideErrorMessage() {
        MessageManager.hideInputFieldMessage();
    }
    /** Displays error message. */
    _showErrorMessage(target) {
        MessageManager.displayInputFieldMessage(target, this.props.errorText, this.props.detailedErrorText);
        return;
    }
}
//# sourceMappingURL=ValidationTextbox.js.map