'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 __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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 classnames_1 = __importDefault(require("classnames"));
var FormGroup_1 = __importDefault(require("react-bootstrap/lib/FormGroup"));
var ControlLabel_1 = __importDefault(require("react-bootstrap/lib/ControlLabel"));
var HelpBlock_1 = __importDefault(require("react-bootstrap/lib/HelpBlock"));
var FormControl_1 = require("./FormControl");
var Block_1 = require("../Block");
var lodash_1 = __importDefault(require("lodash"));
var FormGroup = /** @class */ (function (_super) {
    __extends(FormGroup, _super);
    function FormGroup(props, context) {
        var _this = _super.call(this, props, context) || this;
        _this.ariaDescId = "descId-" + FormGroup.initialAriaDescId++;
        return _this;
    }
    FormGroup.prototype.getAriaDescProps = function (hasError) {
        return hasError ? { 'aria-describedby': this.props.ariaDescId || this.ariaDescId } : {};
    };
    FormGroup.prototype.getValue = function () {
        if (this.refs.control) {
            return this.refs.control.getValue();
        }
        return null;
    };
    FormGroup.prototype.getInputDOMNode = function () {
        if (this.refs.control) {
            return this.refs.control.getInputDOMNode();
        }
        return null;
    };
    FormGroup.prototype.controlLabelFallback = function () {
        return true;
    };
    FormGroup.prototype.renderLabel = function () {
        var _a = this.getPropsFromContext(this.props), label = _a.label, controlLabel = _a.controlLabel, labelClassName = _a.labelClassName, disabled = _a.disabled, readOnly = _a.readOnly, id = _a.id, htmlFor = _a.htmlFor;
        var controlLabelNode = null;
        if (!controlLabel && label && this.controlLabelFallback()) {
            controlLabel = label;
        }
        if (controlLabel) {
            controlLabelNode = readOnly ? (react_1.default.createElement(Block_1.Block, { condensed: true, className: labelClassName },
                react_1.default.createElement("strong", null, controlLabel))) : (react_1.default.createElement(ControlLabel_1.default, { htmlFor: htmlFor || id, "data-test-automation-id": "label", className: classnames_1.default(labelClassName, { 'disabled-label': disabled }) }, controlLabel));
        }
        return controlLabelNode;
    };
    FormGroup.prototype.render = function () {
        var _a = this.getPropsFromContext(this.props), validationState = _a.validationState, bsStyle = _a.bsStyle, controlId = _a.controlId, help = _a.help, wrapperClassName = _a.wrapperClassName, groupClassName = _a.groupClassName, onChange = _a.onChange, // to prevent double onChange -- extract and don't use in BSFormGroup props,
        noOffset = _a.noOffset, // no bottom Offset
        props = __rest(_a, ["validationState", "bsStyle", "controlId", "help", "wrapperClassName", "groupClassName", "onChange", "noOffset"]) // properly handle old-style props
        ;
        onChange = null;
        if (!validationState && bsStyle) {
            validationState = bsStyle;
        }
        if (!controlId) {
            controlId = this.props.testAutomationId;
        }
        groupClassName = classnames_1.default(groupClassName, { 'rc-form-group-noOffset': noOffset });
        var control = wrapperClassName ? (react_1.default.createElement("div", { className: wrapperClassName }, this.renderInput(help))) : (this.renderInput(help));
        // we have to remove tabIndex and id from group props, otherwise Filter for ex. will have 2 focusable elements
        var _b = this.getSafeProps(props), tabIndex = _b.tabIndex, id = _b.id, className = _b.className, groupProps = __rest(_b, ["tabIndex", "id", "className"]);
        var filteredProps = this.filterGroupProps(groupProps);
        return (react_1.default.createElement(FormGroup_1.default, __assign({ validationState: validationState, "data-test-automation-id": this.props.testAutomationId, controlId: controlId, className: classnames_1.default(className, groupClassName) }, filteredProps),
            this.renderLabel(),
            control,
            help ? (react_1.default.createElement(HelpBlock_1.default, { id: this.props.ariaDescId || this.ariaDescId, "data-test-automation-id": "helpBlock" }, help)) : (help)));
    };
    FormGroup.prototype.filterGroupProps = function (props) {
        var attrs = ['name', 'value', 'aria-label', 'aria-hidden', 'aria-checked', 'removeName'];
        var cloneProps = lodash_1.default.clone(props);
        attrs.forEach(function (item) {
            delete cloneProps[item];
        });
        return cloneProps;
    };
    FormGroup.prototype.renderInput = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        return this.props.children;
    };
    FormGroup.initialAriaDescId = Date.now();
    FormGroup.propTypes = __assign({}, FormControl_1.FormControl.propTypes, { groupClassName: prop_types_1.default.string, ariaDescId: prop_types_1.default.string, noOffset: prop_types_1.default.bool });
    FormGroup.defaultProps = __assign({}, FormControl_1.FormControl.defaultProps, { ariaDescId: null });
    return FormGroup;
}(FormControl_1.FormControl));
exports.FormGroup = FormGroup;
//# sourceMappingURL=FormGroup.js.map