"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 __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var lodash_1 = require("lodash");
var prop_types_1 = __importDefault(require("prop-types"));
var AccordionItem_1 = require("./AccordionItem");
var Accordion = /** @class */ (function (_super) {
    __extends(Accordion, _super);
    function Accordion() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            activeItem: _this.props.activeItem
        };
        _this.createClickHandler = function (childOnToggle) { return function (activeItem) {
            var onToggle = _this.props.onToggle;
            if (lodash_1.isFunction(onToggle)) {
                onToggle(activeItem);
            }
            else {
                _this.setState(function (state) { return ({
                    activeItem: state.activeItem !== activeItem ? activeItem : null
                }); });
            }
            if (lodash_1.isFunction(childOnToggle)) {
                childOnToggle(activeItem);
            }
        }; };
        return _this;
    }
    // TODO: redo with getDerivedStateFromProps
    Accordion.prototype.componentWillReceiveProps = function (nextProps) {
        var activeItem = this.props.activeItem;
        if (activeItem !== nextProps.activeItem) {
            this.setState({ activeItem: nextProps.activeItem });
        }
    };
    Accordion.prototype.prepareChildren = function () {
        var _this = this;
        var _a = this.props, children = _a.children, lazy = _a.lazy;
        var activeItem = this.state.activeItem;
        return react_1.default.Children.map(children, function (child) {
            if (!child) {
                return child;
            }
            if (child.type !== AccordionItem_1.AccordionItem) {
                throw new Error('Accordion only supports children of type AccordionItem');
            }
            var _a = child.props, name = _a.name, onToggle = _a.onToggle, testAutomationId = _a.testAutomationId;
            if (!name) {
                throw new Error('AccordionItem in Accordion requires unique `name` prop');
            }
            return react_1.default.cloneElement(child, {
                lazy: lazy,
                isExpanded: name === activeItem,
                onToggle: _this.createClickHandler(onToggle),
                testAutomationId: testAutomationId === AccordionItem_1.AccordionItem.defaultProps.testAutomationId
                    ? "" + AccordionItem_1.AccordionItem.defaultProps.testAutomationId + name
                    : testAutomationId
            });
        });
    };
    Accordion.prototype.render = function () {
        var testAutomationId = this.props.testAutomationId;
        return react_1.default.createElement("div", { "data-test-automation-id": testAutomationId }, this.prepareChildren());
    };
    Accordion.Item = AccordionItem_1.AccordionItem;
    Accordion.propTypes = {
        activeItem: prop_types_1.default.string,
        lazy: prop_types_1.default.bool,
        children: prop_types_1.default.node.isRequired,
        onToggle: prop_types_1.default.func,
        testAutomationId: prop_types_1.default.string
    };
    Accordion.defaultProps = {
        activeItem: null,
        lazy: false,
        onToggle: null,
        testAutomationId: 'accordion'
    };
    return Accordion;
}(react_1.PureComponent));
exports.Accordion = Accordion;
//# sourceMappingURL=Accordion.js.map