import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "className", "id", "role", "element", "buttonElement", "buttonProps", "buttonClassName", "buttonContentClassName", "buttonContent", "arrowDisplay", "arrowProps", "extraAction", "paddingSize", "borders", "initialIsOpen", "forceState", "isLoading", "isLoadingMessage", "isDisabled", "onToggle"];
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

import React, { useState } from 'react';
import classNames from 'classnames';
import { useEuiMemoizedStyles, useGeneratedHtmlId } from '../../services';
import { EuiLoadingSpinner } from '../loading';
import { EuiAccordionTrigger } from './accordion_trigger';
import { EuiAccordionChildren } from './accordion_children';
import { euiAccordionStyles } from './accordion.styles';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var PADDING_SIZES = ['none', 'xs', 's', 'm', 'l', 'xl'];
export var EuiAccordion = function EuiAccordion(_ref) {
  var _buttonProps$id;
  var children = _ref.children,
    className = _ref.className,
    id = _ref.id,
    _ref$role = _ref.role,
    role = _ref$role === void 0 ? 'group' : _ref$role,
    _ref$element = _ref.element,
    Element = _ref$element === void 0 ? 'div' : _ref$element,
    _ref$buttonElement = _ref.buttonElement,
    buttonElement = _ref$buttonElement === void 0 ? 'button' : _ref$buttonElement,
    buttonProps = _ref.buttonProps,
    buttonClassName = _ref.buttonClassName,
    buttonContentClassName = _ref.buttonContentClassName,
    buttonContent = _ref.buttonContent,
    _ref$arrowDisplay = _ref.arrowDisplay,
    arrowDisplay = _ref$arrowDisplay === void 0 ? 'left' : _ref$arrowDisplay,
    arrowProps = _ref.arrowProps,
    extraAction = _ref.extraAction,
    _ref$paddingSize = _ref.paddingSize,
    paddingSize = _ref$paddingSize === void 0 ? 'none' : _ref$paddingSize,
    _ref$borders = _ref.borders,
    borders = _ref$borders === void 0 ? 'none' : _ref$borders,
    _ref$initialIsOpen = _ref.initialIsOpen,
    initialIsOpen = _ref$initialIsOpen === void 0 ? false : _ref$initialIsOpen,
    forceState = _ref.forceState,
    _ref$isLoading = _ref.isLoading,
    isLoading = _ref$isLoading === void 0 ? false : _ref$isLoading,
    _ref$isLoadingMessage = _ref.isLoadingMessage,
    isLoadingMessage = _ref$isLoadingMessage === void 0 ? false : _ref$isLoadingMessage,
    _ref$isDisabled = _ref.isDisabled,
    isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled,
    onToggle = _ref.onToggle,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useState = useState(forceState ? forceState === 'open' : initialIsOpen),
    _useState2 = _slicedToArray(_useState, 2),
    isOpenState = _useState2[0],
    setIsOpenState = _useState2[1];
  var isOpen = forceState ? forceState === 'open' : isOpenState;
  var onAccordionToggle = function onAccordionToggle() {
    if (forceState) {
      onToggle === null || onToggle === void 0 || onToggle(!isOpen);
    } else {
      var nextState = !isOpenState;
      setIsOpenState(nextState);
      onToggle === null || onToggle === void 0 || onToggle(nextState);
    }
  };
  var generatedId = useGeneratedHtmlId();
  var buttonId = (_buttonProps$id = buttonProps === null || buttonProps === void 0 ? void 0 : buttonProps.id) !== null && _buttonProps$id !== void 0 ? _buttonProps$id : generatedId;
  var classes = classNames('euiAccordion', {
    'euiAccordion-isOpen': isOpen
  }, className);
  var styles = useEuiMemoizedStyles(euiAccordionStyles);
  var cssStyles = [styles.euiAccordion, borders !== 'none' && styles.borders.borders, borders !== 'none' && styles.borders[borders]];
  return ___EmotionJSX(Element, _extends({
    className: classes,
    css: cssStyles
  }, rest), ___EmotionJSX(EuiAccordionTrigger, {
    ariaControlsId: id,
    buttonId: buttonId
    // Force button element to be a legend if the element is a fieldset
    ,
    buttonElement: Element === 'fieldset' ? 'legend' : buttonElement,
    buttonClassName: buttonClassName,
    buttonContent: buttonContent,
    buttonContentClassName: buttonContentClassName,
    buttonProps: buttonProps,
    arrowProps: arrowProps,
    arrowDisplay: arrowDisplay,
    isDisabled: isDisabled,
    isOpen: isOpen,
    onToggle: onAccordionToggle,
    extraAction: isLoading ? ___EmotionJSX(EuiLoadingSpinner, null) : extraAction
  }), ___EmotionJSX(EuiAccordionChildren, {
    role: role,
    id: id,
    "aria-labelledby": buttonId,
    paddingSize: paddingSize,
    isLoading: isLoading,
    isLoadingMessage: isLoadingMessage,
    isOpen: isOpen,
    initialIsOpen: initialIsOpen
  }, children));
};