import React, { createContext, useContext, useState } from "react";
export const dividerOrientationMap = {
    horizontal: 'vertical',
    vertical: 'horizontal'
};
export const SegmentContext = /*#__PURE__*/ createContext(undefined);
export const useSegmentInner = ()=>{
    const context = useContext(SegmentContext);
    if (!context) {
        throw new Error('useSegment must be used within a SegmentProvider');
    }
    return context;
};
export const useSegment = ()=>{
    const { selectedSegmentItems } = useSegmentInner();
    return {
        selectedSegmentItems
    };
};
export const SegmentProvider = ({ children, defaultSelected, selected: outerSelected, singleSelectedRequired, onChangeSelected })=>{
    const [selectionMode, setSelectionMode] = useState('single');
    const [disabledGroup, setDisabledGroup] = useState(false);
    const [hasDivider, setHasDivider] = useState(false);
    const [orientation, setOrientation] = useState('horizontal');
    const [selectedSegmentItems, setSelectedSegmentItems] = useState(defaultSelected || []);
    const selected = outerSelected ?? selectedSegmentItems;
    const handleSelect = (label)=>{
        if (selectionMode !== 'multiple') {
            setSelectedSegmentItems((prevSelected)=>prevSelected.includes(label) && !singleSelectedRequired ? [] : [
                    label
                ]);
        } else {
            setSelectedSegmentItems((prevSelected)=>prevSelected.includes(label) && !singleSelectedRequired && prevSelected.length !== 1 ? prevSelected.filter((item)=>item !== label) : [
                    ...prevSelected,
                    label
                ]);
        }
        if (onChangeSelected) {
            onChangeSelected(label);
        }
    };
    const contextValue = {
        selectedSegmentItems: selected,
        setSelectedSegmentItems,
        handleSelect,
        selectionMode,
        setSelectionMode,
        disabledGroup,
        setDisabledGroup,
        hasDivider,
        setHasDivider,
        orientation,
        setOrientation
    };
    return /*#__PURE__*/ React.createElement(SegmentContext.Provider, {
        value: contextValue
    }, children);
};
