import {useControlledState as $2a35a170cf8e413e$export$40bfa8c7b0832715} from "../utils/useControlledState.js";
import {useMemo as $6xQNI$useMemo, useEffect as $6xQNI$useEffect} from "react";

/*
 * Copyright 2024 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */ 

function $eefe38461570519c$export$f36461af0ef4707d(props) {
    let { allowsMultipleExpanded: allowsMultipleExpanded = false, isDisabled: isDisabled = false } = props;
    let [expandedKeys, setExpandedKeys] = (0, $2a35a170cf8e413e$export$40bfa8c7b0832715)((0, $6xQNI$useMemo)(()=>props.expandedKeys ? new Set(props.expandedKeys) : undefined, [
        props.expandedKeys
    ]), (0, $6xQNI$useMemo)(()=>props.defaultExpandedKeys ? new Set(props.defaultExpandedKeys) : new Set(), [
        props.defaultExpandedKeys
    ]), props.onExpandedChange);
    (0, $6xQNI$useEffect)(()=>{
        // Ensure only one item is expanded if allowsMultipleExpanded is false.
        if (!allowsMultipleExpanded && expandedKeys.size > 1) {
            let firstKey = expandedKeys.values().next().value;
            if (firstKey != null) setExpandedKeys(new Set([
                firstKey
            ]));
        }
    });
    return {
        allowsMultipleExpanded: allowsMultipleExpanded,
        isDisabled: isDisabled,
        expandedKeys: expandedKeys,
        setExpandedKeys: setExpandedKeys,
        toggleKey (key) {
            let keys;
            if (allowsMultipleExpanded) {
                keys = new Set(expandedKeys);
                if (keys.has(key)) keys.delete(key);
                else keys.add(key);
            } else keys = new Set(expandedKeys.has(key) ? [] : [
                key
            ]);
            setExpandedKeys(keys);
        }
    };
}


export {$eefe38461570519c$export$f36461af0ef4707d as useDisclosureGroupState};
//# sourceMappingURL=useDisclosureGroupState.js.map
