"use strict";
/*---------------------------------------------------------------------------------------------
* 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
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationTextbox = exports.InputStatus = void 0;
require("./ValidationTextbox.scss");
const classnames_1 = require("classnames");
const React = require("react");
const appui_abstract_1 = require("@itwin/appui-abstract");
const MessageManager_1 = require("../messages/MessageManager");
/** Enum for Input Status used in [[ValidationTextbox]]
 * @alpha
 */
var InputStatus;
(function (InputStatus) {
    InputStatus[InputStatus["Valid"] = 0] = "Valid";
    InputStatus[InputStatus["Invalid"] = 1] = "Invalid";
})(InputStatus = exports.InputStatus || (exports.InputStatus = {}));
/**
 * Input box that validates text based on provided criteria. Defaults to checking
 * for empty if no method for onValueChanged is provided.
 * @alpha
 */
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 appui_abstract_1.SpecialKey.Escape:
                    // istanbul ignore else
                    if (this.props.onEscPressed)
                        this.props.onEscPressed();
                    break;
                case appui_abstract_1.SpecialKey.Enter:
                    // istanbul ignore else
                    if (this.props.onEnterPressed)
                        this.props.onEnterPressed();
                    break;
            }
        };
        this.state = { isValid: true };
    }
    /** @internal */
    render() {
        const sizeStyle = {
            width: this.props.size ? `${this.props.size.toString()}em` : "12em",
        };
        const divStyle = {
            ...sizeStyle,
            ...this.props.style,
        };
        const validClassNames = (0, classnames_1.default)("ValidationTextbox-input");
        const invalidClassNames = (0, classnames_1.default)("ValidationTextbox-input", "ValidationTextbox-invalid");
        return (React.createElement("div", { className: (0, classnames_1.default)("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) {
        // istanbul ignore next
        if (undefined === target)
            return;
        const value = target.value;
        const isValid = this._calculateIsValid(value);
        this.setState({ isValid }, () => {
            if (this.state.isValid) {
                this._hideErrorMessage();
            }
            else {
                // istanbul ignore 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) ? /* istanbul ignore next */ true : false;
        return value.length > 0;
    }
    /** Hides error message */
    _hideErrorMessage() {
        MessageManager_1.MessageManager.hideInputFieldMessage();
    }
    /** Displays error message. */
    _showErrorMessage(target) {
        MessageManager_1.MessageManager.displayInputFieldMessage(target, this.props.errorText, this.props.detailedErrorText);
        return;
    }
}
exports.ValidationTextbox = ValidationTextbox;
//# sourceMappingURL=ValidationTextbox.js.map