'use strict';
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var react_dom_1 = __importDefault(require("react-dom"));
var lodash_1 = __importDefault(require("lodash"));
function isIndeterminate(checked) {
    return null === checked; // do not supply undefined
}
exports.isIndeterminate = isIndeterminate;
var FormControl = /** @class */ (function (_super) {
    __extends(FormControl, _super);
    function FormControl() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.focus = function () {
            var node = _this.getInputDOMNode();
            if (node) {
                node.focus();
            }
        };
        return _this;
    }
    FormControl.prototype.componentDidUpdate = function (oldProps, oldState) {
        var props = this.props;
        var context = this.context;
        var form = context.form;
        var isNeededAutoFocus = lodash_1.default.isFunction(context.getNeededAutoFocus) && context.getNeededAutoFocus();
        var hasError = form && props.name in form.value && !!form.errors[props.name];
        if (isNeededAutoFocus && hasError) {
            if (lodash_1.default.isFunction(context.setNeededAutoFocus)) {
                context.setNeededAutoFocus(false);
            }
            this.focus();
        }
    };
    FormControl.prototype.getInputDOMNode = function () {
        return react_dom_1.default.findDOMNode(this.refs.input);
    };
    FormControl.prototype.click = function () {
        var node = this.getInputDOMNode();
        if (node) {
            node.click();
        }
    };
    FormControl.prototype.getValue = function () {
        var node = this.getInputDOMNode();
        if (node) {
            return node.value;
        }
        return null;
    };
    FormControl.prototype.outputConverter = function (e) {
        return e && e.target ? e.target.value : e;
    };
    FormControl.prototype.inputConverter = function (value) {
        return { value: value };
    };
    FormControl.prototype.getPropsFromContext = function (foo) {
        var _this = this;
        if (foo === void 0) { foo = null; }
        var props = this.props;
        var form = this.context.form;
        if (form && props.name in form.value) {
            var errors = form.errors[props.name];
            var showHelpBlock = errors && props.hasFeedbackBlock; //FIXME errors could be an array
            props = lodash_1.default.assign({
                help: showHelpBlock ? errors : null,
                bsStyle: errors ? 'error' : undefined,
                readOnly: form.readOnly,
                onChange: function (e) {
                    var _a;
                    var value = (_a = {}, _a[props.name] = _this.outputConverter(e), _a);
                    form.setValue(value, value);
                }
            }, this.inputConverter(form.value[props.name]), props);
        }
        return props;
    };
    /**
     * Removes FormGroup stuff from FormControl props
     * @param {object} props
     * @return {object}
     */
    FormControl.prototype.getSafeProps = function (props) {
        var label = props.label, validationState = props.validationState, bsStyle = props.bsStyle, controlId = props.controlId, controlLabel = props.controlLabel, help = props.help, hiddenLabel = props.hiddenLabel, labelClassName = props.labelClassName, wrapperClassName = props.wrapperClassName, groupClassName = props.groupClassName, hasFormGroupContainer = props.hasFormGroupContainer, hasFeedbackBlock = props.hasFeedbackBlock, readOnly = props.readOnly, testAutomationId = props.testAutomationId, isSingleSelect = props.isSingleSelect, minValue = props.minValue, maxValue = props.maxValue, children = props.children, dangerouslySetInnerHTML = props.dangerouslySetInnerHTML, noOffset = props.noOffset, ariaDescId = props.ariaDescId, ariaLabelledById = props.ariaLabelledById, rest = __rest(props, ["label", "validationState", "bsStyle", "controlId", "controlLabel", "help", "hiddenLabel", "labelClassName", "wrapperClassName", "groupClassName", "hasFormGroupContainer", "hasFeedbackBlock", "readOnly", "testAutomationId", "isSingleSelect", "minValue", "maxValue", "children", "dangerouslySetInnerHTML", "noOffset", "ariaDescId", "ariaLabelledById"]);
        return rest;
    };
    FormControl.contextTypes = {
        form: prop_types_1.default.object,
        getNeededAutoFocus: prop_types_1.default.func,
        setNeededAutoFocus: prop_types_1.default.func
    };
    FormControl.propTypes = {
        name: prop_types_1.default.string.isRequired,
        hasFormGroupContainer: prop_types_1.default.bool,
        hasFeedbackBlock: prop_types_1.default.bool,
        testAutomationId: prop_types_1.default.string
    };
    FormControl.defaultProps = {
        hasFormGroupContainer: false,
        hasFeedbackBlock: true,
        testAutomationId: ''
    };
    return FormControl;
}(react_1.default.Component));
exports.FormControl = FormControl;
//# sourceMappingURL=FormControl.js.map