"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get SegmentContext () {
        return SegmentContext;
    },
    get SegmentProvider () {
        return SegmentProvider;
    },
    get dividerOrientationMap () {
        return dividerOrientationMap;
    },
    get useSegment () {
        return useSegment;
    },
    get useSegmentInner () {
        return useSegmentInner;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const dividerOrientationMap = {
    horizontal: 'vertical',
    vertical: 'horizontal'
};
const SegmentContext = /*#__PURE__*/ (0, _react.createContext)(undefined);
const useSegmentInner = ()=>{
    const context = (0, _react.useContext)(SegmentContext);
    if (!context) {
        throw new Error('useSegment must be used within a SegmentProvider');
    }
    return context;
};
const useSegment = ()=>{
    const { selectedSegmentItems } = useSegmentInner();
    return {
        selectedSegmentItems
    };
};
const SegmentProvider = ({ children, defaultSelected, selected: outerSelected, singleSelectedRequired, onChangeSelected })=>{
    const [selectionMode, setSelectionMode] = (0, _react.useState)('single');
    const [disabledGroup, setDisabledGroup] = (0, _react.useState)(false);
    const [hasDivider, setHasDivider] = (0, _react.useState)(false);
    const [orientation, setOrientation] = (0, _react.useState)('horizontal');
    const [selectedSegmentItems, setSelectedSegmentItems] = (0, _react.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.default.createElement(SegmentContext.Provider, {
        value: contextValue
    }, children);
};
