define(["require", "exports", "tslib", "react", "../../Label", "../../Icon", "../../Utilities", "./TextField.scss", "../../Styling"], function (require, exports, tslib_1, React, Label_1, Icon_1, Utilities_1, stylesImport, Styling_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var styles = stylesImport;
    var TextField = /** @class */ (function (_super) {
        tslib_1.__extends(TextField, _super);
        function TextField(props) {
            var _this = _super.call(this, props) || this;
            _this._textElement = Utilities_1.createRef();
            _this._warnDeprecations({
                'iconClass': 'iconProps',
                'addonString': 'prefix',
                'onRenderAddon': 'onRenderPrefix'
            });
            _this._warnMutuallyExclusive({
                'value': 'defaultValue'
            });
            _this._id = Utilities_1.getId('TextField');
            _this._descriptionId = Utilities_1.getId('TextFieldDescription');
            _this._latestValue = props.value || props.defaultValue || '';
            _this.state = {
                value: _this._latestValue,
                isFocused: false,
                errorMessage: ''
            };
            _this._onInputChange = _this._onInputChange.bind(_this);
            _this._onFocus = _this._onFocus.bind(_this);
            _this._onBlur = _this._onBlur.bind(_this);
            _this._delayedValidate = _this._async.debounce(_this._validate, _this.props.deferredValidationTime);
            _this._lastValidation = 0;
            _this._isDescriptionAvailable = false;
            return _this;
        }
        Object.defineProperty(TextField.prototype, "value", {
            /**
             * Gets the current value of the text field.
             */
            get: function () {
                return this.state.value;
            },
            enumerable: true,
            configurable: true
        });
        TextField.prototype.componentDidMount = function () {
            this._isMounted = true;
            this._adjustInputHeight();
            if (this.props.validateOnLoad) {
                this._validate(this.state.value);
            }
        };
        TextField.prototype.componentWillReceiveProps = function (newProps) {
            var _this = this;
            var onBeforeChange = this.props.onBeforeChange;
            if (newProps.value !== undefined && newProps.value !== this.state.value) {
                if (onBeforeChange) {
                    onBeforeChange(newProps.value);
                }
                this._latestValue = newProps.value;
                this.setState({
                    value: newProps.value,
                    errorMessage: ''
                }, function () {
                    _this._adjustInputHeight();
                });
                this._delayedValidate(newProps.value);
            }
        };
        TextField.prototype.componentWillUnmount = function () {
            this._isMounted = false;
        };
        TextField.prototype.render = function () {
            var _a = this.props, className = _a.className, description = _a.description, disabled = _a.disabled, iconClass = _a.iconClass, iconProps = _a.iconProps, multiline = _a.multiline, required = _a.required, underlined = _a.underlined, borderless = _a.borderless, addonString = _a.addonString, // @deprecated
            prefix = _a.prefix, suffix = _a.suffix, _b = _a.onRenderAddon, onRenderAddon = _b === void 0 ? this._onRenderAddon : _b, // @deprecated
            _c = _a.onRenderPrefix, // @deprecated
            onRenderPrefix = _c === void 0 ? this._onRenderPrefix : _c, _d = _a.onRenderSuffix, onRenderSuffix = _d === void 0 ? this._onRenderSuffix : _d, _e = _a.onRenderLabel, onRenderLabel = _e === void 0 ? this._onRenderLabel : _e;
            var isFocused = this.state.isFocused;
            var errorMessage = this._errorMessage;
            this._isDescriptionAvailable = Boolean(description || errorMessage);
            var textFieldClassName = Utilities_1.css('ms-TextField', styles.root, className, (_f = {},
                _f['is-required ' + styles.rootIsRequiredLabel] = this.props.label && required,
                _f['is-required ' + styles.rootIsRequiredPlaceholderOnly] = !this.props.label && required,
                _f['is-disabled ' + styles.rootIsDisabled] = disabled,
                _f['is-active ' + styles.rootIsActive] = isFocused,
                _f['ms-TextField--multiline ' + styles.rootIsMultiline] = multiline,
                _f['ms-TextField--underlined ' + styles.rootIsUnderlined] = underlined,
                _f['ms-TextField--borderless ' + styles.rootIsBorderless] = borderless,
                _f));
            return (React.createElement("div", { className: textFieldClassName },
                React.createElement("div", { className: Utilities_1.css('ms-TextField-wrapper', styles.wrapper, underlined ? errorMessage && styles.invalid : '') },
                    onRenderLabel(this.props, this._onRenderLabel),
                    React.createElement("div", { className: Utilities_1.css('ms-TextField-fieldGroup', styles.fieldGroup, isFocused && styles.fieldGroupIsFocused, errorMessage && styles.invalid) },
                        (addonString !== undefined || this.props.onRenderAddon) && (React.createElement("div", { className: Utilities_1.css('ms-TextField-prefix', styles.fieldPrefixSuffix) }, onRenderAddon(this.props, this._onRenderAddon))),
                        (prefix !== undefined || this.props.onRenderPrefix) && (React.createElement("div", { className: Utilities_1.css('ms-TextField-prefix', styles.fieldPrefixSuffix) }, onRenderPrefix(this.props, this._onRenderPrefix))),
                        multiline ? this._renderTextArea() : this._renderInput(),
                        (iconClass || iconProps) && React.createElement(Icon_1.Icon, tslib_1.__assign({ className: Utilities_1.css(iconClass, styles.icon) }, iconProps)),
                        (suffix !== undefined || this.props.onRenderSuffix) && (React.createElement("div", { className: Utilities_1.css('ms-TextField-suffix', styles.fieldPrefixSuffix) }, onRenderSuffix(this.props, this._onRenderSuffix))))),
                this._isDescriptionAvailable &&
                    React.createElement("span", { id: this._descriptionId },
                        description && React.createElement("span", { className: Utilities_1.css('ms-TextField-description', styles.description) }, description),
                        errorMessage &&
                            React.createElement("div", { "aria-live": 'assertive' },
                                React.createElement(Utilities_1.DelayedRender, null,
                                    React.createElement("p", { className: Utilities_1.css('ms-TextField-errorMessage', Styling_1.AnimationClassNames.slideDownIn20, styles.errorMessage) },
                                        React.createElement("span", { className: styles.errorText, "data-automation-id": 'error-message' }, errorMessage)))))));
            var _f;
        };
        /**
         * Sets focus on the text field
         */
        TextField.prototype.focus = function () {
            if (this._textElement.value) {
                this._textElement.value.focus();
            }
        };
        /**
         * Selects the text field
         */
        TextField.prototype.select = function () {
            if (this._textElement.value) {
                this._textElement.value.select();
            }
        };
        /**
         * Sets the selection start of the text field to a specified value
         */
        TextField.prototype.setSelectionStart = function (value) {
            if (this._textElement.value) {
                this._textElement.value.selectionStart = value;
            }
        };
        /**
         * Sets the selection end of the text field to a specified value
         */
        TextField.prototype.setSelectionEnd = function (value) {
            if (this._textElement.value) {
                this._textElement.value.selectionEnd = value;
            }
        };
        Object.defineProperty(TextField.prototype, "selectionStart", {
            /**
             * Gets the selection start of the text field
             */
            get: function () {
                return this._textElement.value ? this._textElement.value.selectionStart : -1;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(TextField.prototype, "selectionEnd", {
            /**
             * Gets the selection end of the text field
             */
            get: function () {
                return this._textElement.value ? this._textElement.value.selectionEnd : -1;
            },
            enumerable: true,
            configurable: true
        });
        /**
         * Sets the start and end positions of a selection in a text field.
         * @param start Index of the start of the selection.
         * @param end Index of the end of the selection.
         */
        TextField.prototype.setSelectionRange = function (start, end) {
            if (this._textElement.value) {
                this._textElement.value.setSelectionRange(start, end);
            }
        };
        TextField.prototype._onFocus = function (ev) {
            if (this.props.onFocus) {
                this.props.onFocus(ev);
            }
            this.setState({ isFocused: true });
            if (this.props.validateOnFocusIn) {
                this._validate(this.state.value);
            }
        };
        TextField.prototype._onBlur = function (ev) {
            if (this.props.onBlur) {
                this.props.onBlur(ev);
            }
            this.setState({ isFocused: false });
            if (this.props.validateOnFocusOut) {
                this._validate(this.state.value);
            }
        };
        TextField.prototype._onRenderLabel = function (props) {
            var label = props.label;
            if (label) {
                return (React.createElement(Label_1.Label, { htmlFor: this._id }, label));
            }
            return null;
        };
        // @deprecated
        TextField.prototype._onRenderAddon = function (props) {
            var addonString = props.addonString;
            return (React.createElement("span", { style: { paddingBottom: '1px' } }, addonString));
        };
        TextField.prototype._onRenderPrefix = function (props) {
            var prefix = props.prefix;
            return (React.createElement("span", { style: { paddingBottom: '1px' } }, prefix));
        };
        TextField.prototype._onRenderSuffix = function (props) {
            var suffix = props.suffix;
            return (React.createElement("span", { style: { paddingBottom: '1px' } }, suffix));
        };
        TextField.prototype._getTextElementClassName = function () {
            var textFieldClassName;
            if (this.props.multiline && !this.props.resizable) {
                textFieldClassName = Utilities_1.css('ms-TextField-field ms-TextField-field--unresizable', styles.field, styles.fieldIsUnresizable);
            }
            else {
                textFieldClassName = Utilities_1.css('ms-TextField-field', styles.field);
            }
            return Utilities_1.css(textFieldClassName, this.props.inputClassName, (_a = {},
                _a[styles.hasIcon] = !!this.props.iconClass,
                _a));
            var _a;
        };
        Object.defineProperty(TextField.prototype, "_errorMessage", {
            get: function () {
                var errorMessage = this.state.errorMessage;
                if (!errorMessage) {
                    errorMessage = this.props.errorMessage;
                }
                return errorMessage;
            },
            enumerable: true,
            configurable: true
        });
        TextField.prototype._renderTextArea = function () {
            var textAreaProps = Utilities_1.getNativeProps(this.props, Utilities_1.textAreaProperties, ['defaultValue']);
            return (React.createElement("textarea", tslib_1.__assign({ id: this._id }, textAreaProps, { ref: this._textElement, value: this.state.value, onInput: this._onInputChange, onChange: this._onInputChange, className: this._getTextElementClassName(), "aria-describedby": this._isDescriptionAvailable ? this._descriptionId : null, "aria-invalid": !!this.state.errorMessage, "aria-label": this.props.ariaLabel, onFocus: this._onFocus, onBlur: this._onBlur })));
        };
        TextField.prototype._renderInput = function () {
            var inputProps = Utilities_1.getNativeProps(this.props, Utilities_1.inputProperties, ['defaultValue']);
            return (React.createElement("input", tslib_1.__assign({ type: 'text', id: this._id }, inputProps, { ref: this._textElement, value: this.state.value, onInput: this._onInputChange, onChange: this._onInputChange, className: this._getTextElementClassName(), "aria-label": this.props.ariaLabel, "aria-describedby": this._isDescriptionAvailable ? this._descriptionId : null, "aria-invalid": !!this.state.errorMessage, onFocus: this._onFocus, onBlur: this._onBlur })));
        };
        TextField.prototype._onInputChange = function (event) {
            var _this = this;
            var element = event.target;
            var value = element.value;
            // Avoid doing unnecessary work when the value has not changed.
            if (value === this._latestValue) {
                return;
            }
            this._latestValue = value;
            this.setState({
                value: value,
                errorMessage: ''
            }, function () {
                _this._adjustInputHeight();
                if (_this.props.onChanged) {
                    _this.props.onChanged(value);
                }
            });
            var _a = this.props, validateOnFocusIn = _a.validateOnFocusIn, validateOnFocusOut = _a.validateOnFocusOut;
            if (!(validateOnFocusIn || validateOnFocusOut)) {
                this._delayedValidate(value);
            }
            var onBeforeChange = this.props.onBeforeChange;
            onBeforeChange(value);
        };
        TextField.prototype._validate = function (value) {
            var _this = this;
            var _a = this.props, validateOnFocusIn = _a.validateOnFocusIn, validateOnFocusOut = _a.validateOnFocusOut;
            // In case of _validate called multi-times during executing validate logic with promise return.
            if (this._latestValidateValue === value && !(validateOnFocusIn || validateOnFocusOut)) {
                return;
            }
            this._latestValidateValue = value;
            var onGetErrorMessage = this.props.onGetErrorMessage;
            var result = onGetErrorMessage(value || '');
            if (result !== undefined) {
                if (typeof result === 'string') {
                    this.setState({
                        errorMessage: result
                    });
                    this._notifyAfterValidate(value, result);
                }
                else {
                    var currentValidation_1 = ++this._lastValidation;
                    result.then(function (errorMessage) {
                        if (_this._isMounted && currentValidation_1 === _this._lastValidation) {
                            _this.setState({ errorMessage: errorMessage });
                        }
                        _this._notifyAfterValidate(value, errorMessage);
                    });
                }
            }
            else {
                this._notifyAfterValidate(value, '');
            }
        };
        TextField.prototype._notifyAfterValidate = function (value, errorMessage) {
            if (this._isMounted &&
                value === this.state.value &&
                this.props.onNotifyValidationResult) {
                this.props.onNotifyValidationResult(errorMessage, value);
            }
        };
        TextField.prototype._adjustInputHeight = function () {
            if (this._textElement.value && this.props.autoAdjustHeight && this.props.multiline) {
                var textField = this._textElement.value;
                textField.style.height = '';
                var scrollHeight = textField.scrollHeight + 2; // +2 to avoid vertical scroll bars
                textField.style.height = scrollHeight + 'px';
            }
        };
        TextField.defaultProps = {
            multiline: false,
            resizable: true,
            autoAdjustHeight: false,
            underlined: false,
            borderless: false,
            onChanged: function () { },
            onBeforeChange: function () { },
            onNotifyValidationResult: function () { },
            onGetErrorMessage: function () { return undefined; },
            deferredValidationTime: 200,
            errorMessage: '',
            validateOnFocusIn: false,
            validateOnFocusOut: false,
            validateOnLoad: true,
        };
        tslib_1.__decorate([
            Utilities_1.autobind
        ], TextField.prototype, "_onRenderLabel", null);
        return TextField;
    }(Utilities_1.BaseComponent));
    exports.TextField = TextField;
});
//# sourceMappingURL=TextField.js.map