'use strict';

var jsxRuntime = require('react/jsx-runtime');
var react = require('@chakra-ui/react');
var React = require('react');
var AccordionIcon = require('./AccordionIcon.cjs');
var Button = require('../Button/Button.cjs');

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
function isMultiAccordion(openItems, allowMultiple) {
  return Array.isArray(openItems) && allowMultiple;
}
function isSingleAccordion(openItems, allowMultiple) {
  return typeof openItems === "number" || typeof openItems === "undefined" || !allowMultiple;
}
const Accordion = (_a) => {
  var _b = _a, {
    items = [],
    defaultIndex,
    allowToggle = true,
    allowMultiple = false,
    showCompleteStep,
    nextStepText = "Next",
    completedSteps = [],
    omitNextButton = false
  } = _b, rest = __objRest(_b, [
    "items",
    "defaultIndex",
    "allowToggle",
    "allowMultiple",
    "showCompleteStep",
    "nextStepText",
    "completedSteps",
    "omitNextButton"
  ]);
  const [completedItems, setCompletedItems] = React.useState(completedSteps);
  const [openItems, setOpenItems] = React.useState(defaultIndex);
  React.useEffect(() => {
    setCompletedItems(completedSteps);
  }, [completedSteps == null ? void 0 : completedSteps.length]);
  return /* @__PURE__ */ jsxRuntime.jsx(react.Box, { borderWidth: "1px", borderRadius: "lg", overflow: "hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
    react.Accordion,
    __spreadProps(__spreadValues({
      index: openItems,
      allowMultiple,
      allowToggle
    }, rest), {
      children: items.map((item, index) => /* @__PURE__ */ jsxRuntime.jsxs(react.AccordionItem, { children: [
        /* @__PURE__ */ jsxRuntime.jsx(react.Heading, { as: "h2", children: /* @__PURE__ */ jsxRuntime.jsxs(
          react.AccordionButton,
          {
            "data-testid": `accordion-button-${index}`,
            onClick: () => {
              if (isMultiAccordion(openItems, allowMultiple)) {
                setOpenItems(
                  (openItems == null ? void 0 : openItems.includes(index)) ? openItems.filter((item2) => item2 !== index) : [.../* @__PURE__ */ new Set([...openItems, index])]
                );
              } else if (isSingleAccordion(openItems, allowMultiple)) {
                setOpenItems(openItems === index ? -1 : index);
              }
            },
            children: [
              /* @__PURE__ */ jsxRuntime.jsx(
                react.Box,
                {
                  "data-testid": `accordion-item-heading-${index}`,
                  as: "span",
                  flex: "1",
                  textAlign: "left",
                  fontWeight: "semibold",
                  py: 2,
                  children: item.heading
                }
              ),
              showCompleteStep && index !== items.length - 1 && /* @__PURE__ */ jsxRuntime.jsx(AccordionIcon, { isComplete: completedItems.includes(index) }),
              !showCompleteStep && /* @__PURE__ */ jsxRuntime.jsx(react.AccordionIcon, {})
            ]
          }
        ) }),
        /* @__PURE__ */ jsxRuntime.jsxs(react.AccordionPanel, { pb: 4, children: [
          /* @__PURE__ */ jsxRuntime.jsx(react.Box, { "data-testid": `accordion-item-description-${index}`, children: item.description }),
          showCompleteStep && !omitNextButton && index !== items.length - 1 && /* @__PURE__ */ jsxRuntime.jsx(
            Button,
            {
              "data-testid": `accordion-complete-button-${index}`,
              mt: 6,
              isDisabled: isMultiAccordion(openItems, allowMultiple) ? openItems.includes(index + 1) : openItems === index + 1,
              variant: "outline",
              onClick: () => {
                setCompletedItems([.../* @__PURE__ */ new Set([...completedItems, index])]);
                if (isMultiAccordion(openItems, allowMultiple)) {
                  setOpenItems([.../* @__PURE__ */ new Set([...openItems, index + 1])]);
                } else if (isSingleAccordion(openItems, allowMultiple)) {
                  setOpenItems(index + 1);
                }
              },
              children: nextStepText
            }
          )
        ] })
      ] }, `accordion-item-${index}`))
    })
  ) });
};

module.exports = Accordion;
//# sourceMappingURL=Accordion.cjs.map
