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 __());
    };
})();
import React, { PureComponent } from 'react';
import { isFunction } from 'lodash';
import PropTypes from 'prop-types';
import { AccordionItem } from './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 (isFunction(onToggle)) {
                onToggle(activeItem);
            }
            else {
                _this.setState(function (state) { return ({
                    activeItem: state.activeItem !== activeItem ? activeItem : null
                }); });
            }
            if (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.Children.map(children, function (child) {
            if (!child) {
                return child;
            }
            if (child.type !== 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.cloneElement(child, {
                lazy: lazy,
                isExpanded: name === activeItem,
                onToggle: _this.createClickHandler(onToggle),
                testAutomationId: testAutomationId === AccordionItem.defaultProps.testAutomationId
                    ? "" + AccordionItem.defaultProps.testAutomationId + name
                    : testAutomationId
            });
        });
    };
    Accordion.prototype.render = function () {
        var testAutomationId = this.props.testAutomationId;
        return React.createElement("div", { "data-test-automation-id": testAutomationId }, this.prepareChildren());
    };
    Accordion.Item = AccordionItem;
    Accordion.propTypes = {
        activeItem: PropTypes.string,
        lazy: PropTypes.bool,
        children: PropTypes.node.isRequired,
        onToggle: PropTypes.func,
        testAutomationId: PropTypes.string
    };
    Accordion.defaultProps = {
        activeItem: null,
        lazy: false,
        onToggle: null,
        testAutomationId: 'accordion'
    };
    return Accordion;
}(PureComponent));
export { Accordion };
//# sourceMappingURL=Accordion.js.map