"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var Utilities_1 = require("../../Utilities");
var Button_1 = require("../../Button");
var Icon_1 = require("../../Icon");
var getClassNames = Utilities_1.classNamesFunction();
var SearchBoxBase = /** @class */ (function (_super) {
    tslib_1.__extends(SearchBoxBase, _super);
    function SearchBoxBase(props) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            value: props.value || '',
            hasFocus: false,
            id: Utilities_1.getId('SearchBox')
        };
        return _this;
    }
    SearchBoxBase.prototype.componentWillReceiveProps = function (newProps) {
        if (newProps.value !== undefined) {
            this._latestValue = newProps.value;
            this.setState({
                value: newProps.value
            });
        }
    };
    SearchBoxBase.prototype.render = function () {
        var _a = this.props, labelText = _a.labelText, className = _a.className, disabled = _a.disabled, underlined = _a.underlined, getStyles = _a.getStyles, theme = _a.theme;
        var _b = this.state, value = _b.value, hasFocus = _b.hasFocus, id = _b.id;
        var classNames = getClassNames(getStyles, {
            theme: theme,
            className: className,
            underlined: underlined,
            hasFocus: hasFocus,
            disabled: disabled,
            hasInput: value.length > 0
        });
        return (React.createElement("div", { ref: this._resolveRef('_rootElement'), className: classNames.root, onFocusCapture: this._onFocusCapture },
            React.createElement("div", { className: classNames.iconContainer },
                React.createElement(Icon_1.Icon, { className: classNames.icon, iconName: 'Search' })),
            React.createElement("input", { id: id, className: classNames.field, placeholder: labelText, onChange: this._onInputChange, onInput: this._onInputChange, onKeyDown: this._onKeyDown, value: value, disabled: this.props.disabled, "aria-label": this.props.ariaLabel ? this.props.ariaLabel : this.props.labelText, ref: this._resolveRef('_inputElement') }),
            value.length > 0 &&
                React.createElement("div", { className: classNames.clearButton },
                    React.createElement(Button_1.IconButton, { styles: { root: { height: 'auto' }, icon: { fontSize: '12px' } }, onClick: this._onClearClick, iconProps: { iconName: 'Clear' } }))));
    };
    /**
     * Sets focus to the search box input field
     */
    SearchBoxBase.prototype.focus = function () {
        if (this._inputElement) {
            this._inputElement.focus();
        }
    };
    SearchBoxBase.prototype._onClear = function (ev) {
        this.props.onClear && this.props.onClear(ev);
        if (!ev.defaultPrevented) {
            this._latestValue = '';
            this.setState({
                value: ''
            });
            this._callOnChange('');
            ev.stopPropagation();
            ev.preventDefault();
            this._inputElement.focus();
        }
    };
    SearchBoxBase.prototype._onFocusCapture = function (ev) {
        this.setState({
            hasFocus: true
        });
        this._events.on(this._rootElement, 'blur', this._onBlur, true);
        if (this.props.onFocus) {
            this.props.onFocus(ev);
        }
    };
    SearchBoxBase.prototype._onClearClick = function (ev) {
        this._onClear(ev);
    };
    SearchBoxBase.prototype._onKeyDown = function (ev) {
        switch (ev.which) {
            case 27 /* escape */:
                this.props.onEscape && this.props.onEscape(ev);
                if (!ev.defaultPrevented) {
                    this._onClear(ev);
                }
                break;
            case 13 /* enter */:
                if (this.props.onSearch && this.state.value.length > 0) {
                    this.props.onSearch(this.state.value);
                }
                break;
            default:
                this.props.onKeyDown && this.props.onKeyDown(ev);
                if (!ev.defaultPrevented) {
                    return;
                }
        }
        // We only get here if the keypress has been handled,
        // or preventDefault was called in case of default keyDown handler
        ev.preventDefault();
        ev.stopPropagation();
    };
    SearchBoxBase.prototype._onBlur = function (ev) {
        this._events.off(this._rootElement, 'blur');
        this.setState({
            hasFocus: false
        });
        if (this.props.onBlur) {
            this.props.onBlur(ev);
        }
    };
    SearchBoxBase.prototype._onInputChange = function (ev) {
        var value = this._inputElement.value;
        if (value === this._latestValue) {
            return;
        }
        this._latestValue = value;
        this.setState({ value: value });
        this._callOnChange(value);
    };
    SearchBoxBase.prototype._callOnChange = function (newValue) {
        var _a = this.props, onChange = _a.onChange, onChanged = _a.onChanged;
        // Call @deprecated method.
        if (onChanged) {
            onChanged(newValue);
        }
        if (onChange) {
            onChange(newValue);
        }
    };
    SearchBoxBase.defaultProps = {
        labelText: 'Search',
    };
    tslib_1.__decorate([
        Utilities_1.autobind
    ], SearchBoxBase.prototype, "_onFocusCapture", null);
    tslib_1.__decorate([
        Utilities_1.autobind
    ], SearchBoxBase.prototype, "_onClearClick", null);
    tslib_1.__decorate([
        Utilities_1.autobind
    ], SearchBoxBase.prototype, "_onKeyDown", null);
    tslib_1.__decorate([
        Utilities_1.autobind
    ], SearchBoxBase.prototype, "_onBlur", null);
    tslib_1.__decorate([
        Utilities_1.autobind
    ], SearchBoxBase.prototype, "_onInputChange", null);
    SearchBoxBase = tslib_1.__decorate([
        Utilities_1.customizable('SearchBox', ['theme'])
    ], SearchBoxBase);
    return SearchBoxBase;
}(Utilities_1.BaseComponent));
exports.SearchBoxBase = SearchBoxBase;
//# sourceMappingURL=SearchBox.base.js.map