'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var styles = require('./styles.js');
var index = require('../../utils/index.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

function Accordion(props) {
    const { isCollapsed = true } = props;
    const [isCollapsedInternal, setCollapseStatus] = React.useState(isCollapsed);
    const appendTestId = index.getId(props['data-testid']);
    function onCollapse(event) {
        event.preventDefault();
        const nextState = !isCollapsedInternal;
        setCollapseStatus(nextState);
        if (props.variant !== 'custom') {
            props.onToggle && props.onToggle(nextState);
        }
    }
    React.useEffect(() => {
        setCollapseStatus(Boolean(isCollapsed));
    }, [isCollapsed]);
    switch (props.variant) {
        case 'default':
        case 'low-emphasis':
            return (React__default['default'].createElement(styles.AccordionWrapper, { hasDivider: props.divider, "data-testid": props['data-testid'], "data-trackid": props['data-trackid'] },
                React__default['default'].createElement(styles.AccordionHeader, { type: "button", id: "accordion-button", disabled: props.disableCollapse, isCollapsed: isCollapsedInternal, onClick: onCollapse, "data-testid": appendTestId('header'), "aria-controls": "accordion-content", "aria-expanded": !isCollapsedInternal },
                    React__default['default'].createElement(styles.AccordionHeaderPrimaryRow, null,
                        React__default['default'].createElement(styles.AccordionHeaderTitle, { variant: props.variant, "data-testid": appendTestId('title-wrapper') },
                            props.icon && (React__default['default'].createElement("span", { "data-testid": appendTestId('title-icon') }, props.icon)),
                            React__default['default'].createElement("span", { "data-testid": appendTestId('title-text') }, props.primaryText)),
                        !props.hideChevron && (React__default['default'].createElement(styles.AccordionChevron, { "data-testid": appendTestId('header-chevron') }))),
                    props.secondaryText && (React__default['default'].createElement(styles.AccordionHeaderSecondaryRow, { "data-testid": appendTestId('secondary-row') }, props.secondaryText))),
                React__default['default'].createElement(styles.AccordionContent, { id: "accordion-content", role: "region", "aria-labelledby": "accordion-button", isCollapsed: isCollapsedInternal, "data-testid": appendTestId('content'), "aria-hidden": isCollapsedInternal }, props.children)));
        case 'custom':
            return (React__default['default'].createElement(styles.AccordionWrapper, { "data-testid": props['data-testid'] },
                props.header,
                React__default['default'].createElement(styles.CollapsableContainer, { isCollapsed: isCollapsedInternal, "data-testid": appendTestId('content'), "aria-hidden": isCollapsedInternal }, props.content)));
        default:
            throw new Error('Incorrect variant');
    }
}

exports.Accordion = Accordion;
