import React__default, { useState, useEffect } from 'react';
import { AccordionWrapper, CollapsableContainer, AccordionHeader, AccordionHeaderPrimaryRow, AccordionHeaderTitle, AccordionChevron, AccordionHeaderSecondaryRow, AccordionContent } from './styles.js';
import { getId } from '../../utils/index.js';

function Accordion(props) {
    const { isCollapsed = true } = props;
    const [isCollapsedInternal, setCollapseStatus] = useState(isCollapsed);
    const appendTestId = getId(props['data-testid']);
    function onCollapse(event) {
        event.preventDefault();
        const nextState = !isCollapsedInternal;
        setCollapseStatus(nextState);
        if (props.variant !== 'custom') {
            props.onToggle && props.onToggle(nextState);
        }
    }
    useEffect(() => {
        setCollapseStatus(Boolean(isCollapsed));
    }, [isCollapsed]);
    switch (props.variant) {
        case 'default':
        case 'low-emphasis':
            return (React__default.createElement(AccordionWrapper, { hasDivider: props.divider, "data-testid": props['data-testid'], "data-trackid": props['data-trackid'] },
                React__default.createElement(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.createElement(AccordionHeaderPrimaryRow, null,
                        React__default.createElement(AccordionHeaderTitle, { variant: props.variant, "data-testid": appendTestId('title-wrapper') },
                            props.icon && (React__default.createElement("span", { "data-testid": appendTestId('title-icon') }, props.icon)),
                            React__default.createElement("span", { "data-testid": appendTestId('title-text') }, props.primaryText)),
                        !props.hideChevron && (React__default.createElement(AccordionChevron, { "data-testid": appendTestId('header-chevron') }))),
                    props.secondaryText && (React__default.createElement(AccordionHeaderSecondaryRow, { "data-testid": appendTestId('secondary-row') }, props.secondaryText))),
                React__default.createElement(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.createElement(AccordionWrapper, { "data-testid": props['data-testid'] },
                props.header,
                React__default.createElement(CollapsableContainer, { isCollapsed: isCollapsedInternal, "data-testid": appendTestId('content'), "aria-hidden": isCollapsedInternal }, props.content)));
        default:
            throw new Error('Incorrect variant');
    }
}

export { Accordion };
