"use client";
import { useState } from "react";
import { useEnsuredId } from "../useEnsuredId.js";
/**
 * Use this hook to control the expanded state for a group of `ExpansionPanel`.
 *
 * @example
 * Index Based Panels
 * ```tsx
 * import {
 *   ExpansionList,
 *   ExpansionPanel,
 *   useExpansionPanels,
 * } from "@react-md/core";
 * import { type ReactElement } from "react";
 *
 * function Example(): ReactElement {
 *   const { getPanelProps } = useExpansionPanels();
 *
 *   return (
 *     <ExpansionList>
 *       <ExpansionPanel {...getPanelProps(0)} headerChildren="Panel 1">
 *         Panel 1 Contents
 *       </ExpansionPanel>
 *       <ExpansionPanel {...getPanelProps(1)} headerChildren="Panel 2">
 *         Panel 2 Contents
 *       </ExpansionPanel>
 *       <ExpansionPanel {...getPanelProps(2)} headerChildren="Panel 3">
 *         Panel 3 Contents
 *       </ExpansionPanel>
 *     </ExpansionList>
 *   );
 * }
 * ```
 *
 * @example
 * Custom Panel Ids and Expand All Panels by Default
 * ```tsx
 * import {
 *   ExpansionList,
 *   ExpansionPanel,
 *   useExpansionPanels,
 * } from "@react-md/core";
 * import { type ReactElement } from "react";
 *
 * import { BillingAddress } from "./BillingAddress";
 * import { BillingInformation } from "./BillingInformation";
 * import { PersonalInformation } from "./PersonalInformation";
 *
 * const panel1Id = "personal-information-panel";
 * const panel2Id = "billing-information-panel";
 * const panel3Id = "billing-address-panel";
 *
 * function Example(): ReactElement {
 *   const { getPanelProps } = useExpansionPanels({
 *     multiple: true,
 *     defaultExpandedIds: () => [panel1Id, panel2Id, panel3Id],
 *   });
 *
 *   return (
 *     <ExpansionList>
 *       <ExpansionPanel {...getPanelProps(panel1Id)} headerChildren="Personal Information">
 *         <PersonalInformation />
 *       </ExpansionPanel>
 *       <ExpansionPanel {...getPanelProps(panel2Id)} headerChildren="Billing Information">
 *         <BillingInformation />
 *       </ExpansionPanel>
 *       <ExpansionPanel {...getPanelProps(panel3Id)} headerChildren="Billing Address">
 *         <BillingAddress />
 *       </ExpansionPanel>
 *     </ExpansionList>
 *   );
 * }
 * ```
 *
 *
 * @remarks
 * \@since 6.0.0 The hook was renamed from `usePanels` to `useExpansionPanels`,
 * the API changed to return `getPanelProps` instead of a generated list of
 * panel props based on the provided `count`, and the hook no longer supports
 * having all panels expanded by default.
 */ export function useExpansionPanels(options = {}) {
    const { baseId: propBaseId, multiple = false, preventAllCollapsed = false, disableTransition = false, defaultExpandedIds, defaultExpandedIndex, disableContentPadding = false } = options;
    const baseId = useEnsuredId(propBaseId, "expansion-panel");
    const createId = (index)=>`${baseId}-${index + 1}`;
    const [expandedIds, setExpandedIds] = useState(()=>{
        if (typeof defaultExpandedIds === "undefined") {
            const initialList = [];
            if (typeof defaultExpandedIndex === "number" || preventAllCollapsed) {
                initialList.push(createId(defaultExpandedIndex ?? 0));
            }
            return new Set(initialList);
        }
        const ids = typeof defaultExpandedIds === "function" ? defaultExpandedIds() : defaultExpandedIds;
        return new Set(ids);
    });
    return {
        expandedIds,
        setExpandedIds,
        getPanelProps (indexOrPanelId) {
            let id;
            let panelId;
            if (typeof indexOrPanelId === "number") {
                id = createId(indexOrPanelId);
                panelId = id;
            } else {
                panelId = indexOrPanelId;
            }
            const expanded = expandedIds.has(panelId);
            const disabled = expanded && preventAllCollapsed && expandedIds.size === 1;
            return {
                id,
                disabled,
                expanded,
                onExpandClick () {
                    if (disabled) {
                        return;
                    }
                    setExpandedIds((prevIds)=>{
                        const expanded = prevIds.has(panelId);
                        if (!multiple) {
                            return new Set(expanded ? [] : [
                                panelId
                            ]);
                        }
                        const nextIds = new Set(prevIds);
                        if (expanded) {
                            nextIds.delete(panelId);
                        } else {
                            nextIds.add(panelId);
                        }
                        return nextIds;
                    });
                },
                disableTransition,
                disableContentPadding
            };
        }
    };
}

//# sourceMappingURL=useExpansionPanels.js.map