import * as __WEBPACK_EXTERNAL_MODULE__chakra_ui_react__ from "@chakra-ui/react";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__AccordionIcon_mjs__ from "./AccordionIcon.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Button_Button_mjs__ from "../Button/Button.mjs";
function isMultiAccordion(openItems, allowMultiple) {
    return Array.isArray(openItems) && allowMultiple;
}
function isSingleAccordion(openItems, allowMultiple) {
    return 'number' == typeof openItems || void 0 === openItems || !allowMultiple;
}
const Accordion = ({ items = [], defaultIndex, allowToggle = true, allowMultiple = false, showCompleteStep, nextStepText = 'Next', completedSteps = [], omitNextButton = false, ...rest })=>{
    const [completedItems, setCompletedItems] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(completedSteps);
    const [openItems, setOpenItems] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(defaultIndex);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        setCompletedItems(completedSteps);
    }, [
        completedSteps?.length
    ]);
    return /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Box, {
        borderWidth: "1px",
        borderRadius: "lg",
        overflow: "hidden"
    }, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Accordion, {
        index: openItems,
        allowMultiple: allowMultiple,
        allowToggle: allowToggle,
        ...rest
    }, items.map((item, index)=>/*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.AccordionItem, {
            key: `accordion-item-${index}`
        }, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Heading, {
            as: "h2"
        }, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.AccordionButton, {
            "data-testid": `accordion-button-${index}`,
            onClick: ()=>{
                if (isMultiAccordion(openItems, allowMultiple)) setOpenItems(openItems?.includes(index) ? openItems.filter((item)=>item !== index) : [
                    ...new Set([
                        ...openItems,
                        index
                    ])
                ]);
                else if (isSingleAccordion(openItems, allowMultiple)) setOpenItems(openItems === index ? -1 : index); // When there is no item to be open, set to -1
            }
        }, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Box, {
            "data-testid": `accordion-item-heading-${index}`,
            as: "span",
            flex: "1",
            textAlign: "left",
            fontWeight: "semibold",
            py: 2
        }, item.heading), showCompleteStep && index !== items.length - 1 && /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__AccordionIcon_mjs__["default"], {
            isComplete: completedItems.includes(index)
        }), !showCompleteStep && /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.AccordionIcon, null))), /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.AccordionPanel, {
            pb: 4
        }, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Box, {
            "data-testid": `accordion-item-description-${index}`
        }, item.description), showCompleteStep && !omitNextButton && index !== items.length - 1 && /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__Button_Button_mjs__["default"], {
            "data-testid": `accordion-complete-button-${index}`,
            mt: 6,
            isDisabled: isMultiAccordion(openItems, allowMultiple) ? openItems.includes(index + 1) : openItems === index + 1,
            variant: "outline",
            onClick: ()=>{
                setCompletedItems([
                    ...new Set([
                        ...completedItems,
                        index
                    ])
                ]);
                if (isMultiAccordion(openItems, allowMultiple)) setOpenItems([
                    ...new Set([
                        ...openItems,
                        index + 1
                    ])
                ]);
                else if (isSingleAccordion(openItems, allowMultiple)) setOpenItems(index + 1);
            }
        }, nextStepText))))));
};
/* ESM default export */ const Accordion_rslib_entry_ = Accordion;
export { Accordion_rslib_entry_ as default };
