"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.SEGMENTED_CONTROL_BASE_CLASS_NAME = void 0;
var _classnames = _interopRequireDefault(require("classnames"));
var _react = require("react");
var _constants = require("../../constants");
var _ResponsiveContext = _interopRequireDefault(require("../ResponsiveContext"));
var _Squeezer = _interopRequireDefault(require("../Squeezer"));
var _DropdownControl = _interopRequireDefault(require("./DropdownControl"));
var _ButtonGroupControl = _interopRequireDefault(require("./ButtonGroupControl"));
var _useResponsive = require("./useResponsive");
var _useSelected = require("./useSelected");
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const SEGMENTED_CONTROL_BASE_CLASS_NAME = exports.SEGMENTED_CONTROL_BASE_CLASS_NAME = `${_constants.CLS_PREFIX}segmented-control`;

/**
 * `Segmented Control` is a group of several [Buttons](#!/Button)
 * each of which functions as a mutually exclusive option.
 * @since 0.0.68
 */
const SegmentedControl = _ref => {
  let {
    label,
    labelPlacement = 'top',
    buttons,
    monospaced = false,
    multiple = false,
    selected: externalSelected,
    onChange,
    inline = false,
    ghost = false,
    className,
    baseClassName = SEGMENTED_CONTROL_BASE_CLASS_NAME,
    ...props
  } = _ref;
  const rootRef = (0, _react.useRef)(null);
  const {
    isResponsive,
    isInResponsiveContext,
    ...squeezerProps
  } = (0, _useResponsive.useResponsive)();
  const {
    selectedValues,
    handleChange
  } = (0, _useSelected.useSelected)({
    multiple,
    externalSelected,
    onChange
  });
  const renderControl = () => {
    const hasIcons = buttons.some(_ref2 => {
      let {
        icon
      } = _ref2;
      return !!icon;
    });
    if (!hasIcons && !multiple && isResponsive) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_DropdownControl.default, {
        buttons: buttons,
        selected: selectedValues[0],
        onClick: handleChange
      });
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ResponsiveContext.default.Provider, {
      value: isResponsive,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ButtonGroupControl.default, {
        buttons: buttons,
        selectedValues: selectedValues,
        onClick: handleChange,
        ghost: ghost,
        isResponsive: isResponsive,
        monospaced: monospaced,
        baseClassName: baseClassName
      })
    });
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    ref: rootRef,
    className: (0, _classnames.default)(baseClassName, {
      [`${baseClassName}--inline`]: inline,
      [`${baseClassName}--collapsed`]: isResponsive,
      [`${baseClassName}--vertical`]: label && labelPlacement === 'top'
    }, className),
    ...props,
    children: [label && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: `${baseClassName}__label`,
      children: label
    }), renderControl(), !isInResponsiveContext && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Squeezer.default, {
      ref: rootRef,
      ...squeezerProps
    })]
  });
};
var _default = exports.default = SegmentedControl;