import { slicedToArray as _slicedToArray, toConsumableArray as _toConsumableArray } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { useState, createContext, useContext } from 'react';

var dividerOrientationMap = {
  horizontal: 'vertical',
  vertical: 'horizontal'
};
var SegmentContext = /*#__PURE__*/createContext(undefined);
var useSegmentInner = function useSegmentInner() {
  var context = useContext(SegmentContext);
  if (!context) {
    throw new Error('useSegment must be used within a SegmentProvider');
  }
  return context;
};
var useSegment = function useSegment() {
  var _useSegmentInner = useSegmentInner(),
    selectedSegmentItems = _useSegmentInner.selectedSegmentItems;
  return {
    selectedSegmentItems: selectedSegmentItems
  };
};
var SegmentProvider = function SegmentProvider(_ref) {
  var children = _ref.children,
    defaultSelected = _ref.defaultSelected,
    outerSelected = _ref.selected,
    singleSelectedRequired = _ref.singleSelectedRequired,
    onChangeSelected = _ref.onChangeSelected;
  var _useState = useState('single'),
    _useState2 = _slicedToArray(_useState, 2),
    selectionMode = _useState2[0],
    setSelectionMode = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    disabledGroup = _useState4[0],
    setDisabledGroup = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    hasDivider = _useState6[0],
    setHasDivider = _useState6[1];
  var _useState7 = useState('horizontal'),
    _useState8 = _slicedToArray(_useState7, 2),
    orientation = _useState8[0],
    setOrientation = _useState8[1];
  var _useState9 = useState(defaultSelected || []),
    _useState0 = _slicedToArray(_useState9, 2),
    selectedSegmentItems = _useState0[0],
    setSelectedSegmentItems = _useState0[1];
  var selected = outerSelected !== null && outerSelected !== void 0 ? outerSelected : selectedSegmentItems;
  var handleSelect = function handleSelect(label) {
    if (selectionMode !== 'multiple') {
      setSelectedSegmentItems(function (prevSelected) {
        return prevSelected.includes(label) && !singleSelectedRequired ? [] : [label];
      });
    } else {
      setSelectedSegmentItems(function (prevSelected) {
        return prevSelected.includes(label) && !singleSelectedRequired && prevSelected.length !== 1 ? prevSelected.filter(function (item) {
          return item !== label;
        }) : [].concat(_toConsumableArray(prevSelected), [label]);
      });
    }
    if (onChangeSelected) {
      onChangeSelected(label);
    }
  };
  var contextValue = {
    selectedSegmentItems: selected,
    setSelectedSegmentItems: setSelectedSegmentItems,
    handleSelect: handleSelect,
    selectionMode: selectionMode,
    setSelectionMode: setSelectionMode,
    disabledGroup: disabledGroup,
    setDisabledGroup: setDisabledGroup,
    hasDivider: hasDivider,
    setHasDivider: setHasDivider,
    orientation: orientation,
    setOrientation: setOrientation
  };
  return /*#__PURE__*/React__default.createElement(SegmentContext.Provider, {
    value: contextValue
  }, children);
};

export { SegmentContext, SegmentProvider, dividerOrientationMap, useSegment, useSegmentInner };
