"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var lodash_1 = require("lodash");
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 = {
            prevActiveItem: _this.props.activeItem,
            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);
            }
        }; };
        _this.renderChild = function (child) {
            var lazy = _this.props.lazy;
            var activeItem = _this.state.activeItem;
            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.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
            });
        };
        return _this;
    }
    Accordion.getDerivedStateFromProps = function (props, state) {
        if (props.activeItem === state.activeItem) {
            return null;
        }
        if (lodash_1.isUndefined(props.activeItem)) {
            return null;
        }
        var activeItem = state.activeItem;
        if (props.activeItem !== state.prevActiveItem) {
            activeItem = props.activeItem;
        }
        return {
            prevActiveItem: props.activeItem,
            activeItem: activeItem
        };
    };
    Accordion.prototype.renderChildren = function () {
        var children = this.props.children;
        return React.Children.map(children, this.renderChild);
    };
    Accordion.prototype.render = function () {
        var testAutomationId = this.props.testAutomationId;
        return React.createElement("div", { "data-test-automation-id": testAutomationId }, this.renderChildren());
    };
    Accordion.Item = AccordionItem_1.AccordionItem;
    Accordion.defaultProps = {
        activeItem: undefined,
        lazy: false,
        onToggle: null,
        testAutomationId: 'accordion'
    };
    return Accordion;
}(React.PureComponent));
exports.Accordion = Accordion;
//# sourceMappingURL=Accordion.js.map