import { jsxs, jsx } from "react/jsx-runtime";
import { aI as manifest } from "./use-page-data.CMUQyNAA.js";
import "@sinclair/typebox";
import { a as tx } from "./twind.DdSZmZu6.js";
import { useState } from "react";
import { a as MdKeyboardArrowRight } from "./index.CNpSgpKM.js";
import { T as TextContent } from "./TextContent.fGn2nGQK.js";
import { b as useBrickStyle } from "./use-section-style.DjjgfIQS.js";
import { B as BrickRoot } from "./BrickRoot.D_a_fRJm.js";
function Accordion({ brick, editable }) {
  const styles = useBrickStyle(brick);
  const { props } = brick;
  const items = Array.isArray(props.items) ? props.items : [];
  const { rounding, ...otherStyles } = styles;
  const [itemOpened, setItemOpened] = useState(
    items.findIndex((item) => !!item.defaultOpen) || null
  );
  const [openedItems, setOpenedItems] = useState(() => items.map((item) => !!item.defaultOpen));
  return /* @__PURE__ */ jsxs(
    BrickRoot,
    {
      brick,
      editable,
      manifest,
      className: tx("flex flex-col overflow-hidden relative", Object.values(otherStyles)),
      children: [
        editable && items.length === 0 && /* @__PURE__ */ jsx("div", { className: "absolute top-0 left-0 right-0 bottom-0 flex items-center justify-center text-center p-8", children: /* @__PURE__ */ jsx("div", { className: "text-base", children: "Add content to this accordion in the panel" }) }),
        items.map((item, index) => {
          const isOpen = !props.restrictOneOpen ? openedItems[index] || item.defaultOpen : itemOpened === index;
          return /* @__PURE__ */ jsxs("div", { className: tx("flex flex-col border", rounding, "overflow-hidden"), children: [
            /* @__PURE__ */ jsxs(
              "button",
              {
                type: "button",
                "aria-expanded": isOpen,
                onClick: () => {
                  if (!props.restrictOneOpen) {
                    setOpenedItems((prev) => {
                      const next = [...prev];
                      next[index] = !next[index];
                      return next;
                    });
                  } else {
                    setItemOpened((prev) => prev === index ? null : index);
                  }
                },
                className: tx(
                  "flex flex-grow shrink-0 items-center p-3 gap-1 justify-between w-full",
                  "font-semibold cursor-pointer select-none text-left text-[110%] ",
                  isOpen && "rounded-b-none border-b"
                ),
                children: [
                  /* @__PURE__ */ jsx("div", { className: tx("flex flex-grow shrink-0 flex-row items-center gap-1"), children: /* @__PURE__ */ jsx(
                    TextContent,
                    {
                      propPath: `items.${index}.title`,
                      className: tx("w-full", editable && "cursor-text"),
                      brickId: brick.id,
                      content: item.title,
                      rawContent: item.title,
                      editable,
                      inline: true
                    }
                  ) }),
                  /* @__PURE__ */ jsx(MdKeyboardArrowRight, { className: tx("transition-transform", isOpen && "rotate-90") })
                ]
              }
            ),
            /* @__PURE__ */ jsx("div", { className: tx("transition-all duration-900 ease-in-out p-3 flex-1", !isOpen && "hidden"), children: /* @__PURE__ */ jsx(
              TextContent,
              {
                propPath: `items.${index}.content`,
                className: tx("w-full", editable && "cursor-text"),
                brickId: brick.id,
                content: item.content,
                rawContent: item.content,
                editable
              }
            ) })
          ] }, index);
        })
      ]
    }
  );
}
export {
  Accordion as default
};
//# sourceMappingURL=accordion.BiFOeDBx.js.map
