"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "EuiAccordion", {
  enumerable: true,
  get: function get() {
    return _accordion.EuiAccordion;
  }
});
exports.OuiAccordion = exports.PADDING_SIZES = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _common = require("../common");

var _icon = require("../icon");

var _loading = require("../loading");

var _resize_observer = require("../observer/resize_observer");

var _i18n = require("../i18n");

var _services = require("../../services");

var _accordion = require("../../eui_components/accordion/accordion");

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

var paddingSizeToClassNameMap = {
  none: '',
  xs: 'ouiAccordion__padding--xs',
  s: 'ouiAccordion__padding--s',
  m: 'ouiAccordion__padding--m',
  l: 'ouiAccordion__padding--l',
  xl: 'ouiAccordion__padding--xl'
};
var PADDING_SIZES = (0, _common.keysOf)(paddingSizeToClassNameMap);
exports.PADDING_SIZES = PADDING_SIZES;

var OuiAccordion = /*#__PURE__*/function (_Component) {
  (0, _inherits2.default)(OuiAccordion, _Component);

  var _super = _createSuper(OuiAccordion);

  function OuiAccordion() {
    var _this;

    (0, _classCallCheck2.default)(this, OuiAccordion);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "childContent", null);
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "childWrapper", null);
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", {
      isOpen: _this.props.forceState ? _this.props.forceState === 'open' : _this.props.initialIsOpen
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "setChildContentHeight", function () {
      var forceState = _this.props.forceState;
      requestAnimationFrame(function () {
        var height = _this.childContent && (forceState ? forceState === 'open' : _this.state.isOpen) ? _this.childContent.clientHeight : 0;
        _this.childWrapper && _this.childWrapper.setAttribute('style', "height: ".concat(height, "px"));
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onToggle", function () {
      var forceState = _this.props.forceState;

      if (forceState) {
        _this.props.onToggle && _this.props.onToggle(forceState === 'open' ? false : true);
      } else {
        _this.setState(function (prevState) {
          return {
            isOpen: !prevState.isOpen
          };
        }, function () {
          if (_this.state.isOpen && _this.childWrapper) {
            _this.childWrapper.focus();
          }

          _this.props.onToggle && _this.props.onToggle(_this.state.isOpen);
        });
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "setChildContentRef", function (node) {
      _this.childContent = node;
    });
    return _this;
  }

  (0, _createClass2.default)(OuiAccordion, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.setChildContentHeight();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      this.setChildContentHeight();
    }
  }, {
    key: "render",
    value: function render() {
      var _buttonProps$id,
          _this2 = this;

      var _this$props = this.props,
          children = _this$props.children,
          buttonContent = _this$props.buttonContent,
          className = _this$props.className,
          id = _this$props.id,
          buttonClassName = _this$props.buttonClassName,
          buttonContentClassName = _this$props.buttonContentClassName,
          extraAction = _this$props.extraAction,
          paddingSize = _this$props.paddingSize,
          initialIsOpen = _this$props.initialIsOpen,
          arrowDisplay = _this$props.arrowDisplay,
          forceState = _this$props.forceState,
          isLoading = _this$props.isLoading,
          isLoadingMessage = _this$props.isLoadingMessage,
          buttonProps = _this$props.buttonProps,
          rest = (0, _objectWithoutProperties2.default)(_this$props, ["children", "buttonContent", "className", "id", "buttonClassName", "buttonContentClassName", "extraAction", "paddingSize", "initialIsOpen", "arrowDisplay", "forceState", "isLoading", "isLoadingMessage", "buttonProps"]);
      var isOpen = forceState ? forceState === 'open' : this.state.isOpen;
      var classes = (0, _classnames.default)('ouiAccordion', {
        'ouiAccordion-isOpen': isOpen
      }, className);
      var paddingClass = paddingSize ? (0, _classnames.default)(paddingSizeToClassNameMap[paddingSize]) : undefined;
      var childrenClasses = (0, _classnames.default)(paddingClass, {
        'ouiAccordion__children-isLoading': isLoading
      });
      var buttonClasses = (0, _classnames.default)('ouiAccordion__button', {
        ouiAccordion__buttonReverse: !extraAction && arrowDisplay === 'right'
      }, buttonClassName, buttonProps === null || buttonProps === void 0 ? void 0 : buttonProps.className);
      var iconClasses = (0, _classnames.default)('ouiAccordion__icon', {
        'ouiAccordion__icon-isOpen': isOpen
      });
      var iconWrapperClasses = (0, _classnames.default)('ouiAccordion__iconWrapper', {
        ouiAccordion__iconButton: extraAction && arrowDisplay === 'right'
      });
      var baseIcon;

      if (arrowDisplay !== 'none') {
        baseIcon = /*#__PURE__*/_react.default.createElement(_icon.OuiIcon, {
          className: iconClasses,
          type: "arrowRight",
          size: "m"
        });
      }

      var icon;
      var iconButton;
      var buttonId = (_buttonProps$id = buttonProps === null || buttonProps === void 0 ? void 0 : buttonProps.id) !== null && _buttonProps$id !== void 0 ? _buttonProps$id : (0, _services.htmlIdGenerator)()();

      if (extraAction && arrowDisplay === 'right') {
        iconButton = /*#__PURE__*/_react.default.createElement("button", {
          "aria-controls": id,
          "aria-expanded": isOpen,
          "aria-labelledby": buttonId,
          tabIndex: -1,
          className: iconWrapperClasses,
          onClick: this.onToggle
        }, baseIcon);
      } else if (arrowDisplay !== 'none') {
        icon = /*#__PURE__*/_react.default.createElement("span", {
          className: iconWrapperClasses
        }, baseIcon);
      }

      var optionalAction = null;

      if (extraAction && !isLoading) {
        optionalAction = /*#__PURE__*/_react.default.createElement("div", {
          className: "ouiAccordion__optionalAction"
        }, extraAction);
      } else if (isLoading) {
        optionalAction = /*#__PURE__*/_react.default.createElement("div", {
          className: "ouiAccordion__optionalAction"
        }, /*#__PURE__*/_react.default.createElement(_loading.OuiLoadingSpinner, null));
      }

      var childrenContent;

      if (isLoading && isLoadingMessage) {
        childrenContent = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_loading.OuiLoadingSpinner, {
          className: "ouiAccordion__spinner"
        }), /*#__PURE__*/_react.default.createElement("span", null, isLoadingMessage && isLoadingMessage !== true ? isLoadingMessage : /*#__PURE__*/_react.default.createElement(_i18n.OuiI18n, {
          token: "ouiAccordion.isLoading",
          default: "Loading"
        })));
      } else {
        childrenContent = children;
      }

      return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
        className: classes
      }, rest), /*#__PURE__*/_react.default.createElement("div", {
        className: "ouiAccordion__triggerWrapper"
      }, /*#__PURE__*/_react.default.createElement("button", (0, _extends2.default)({}, buttonProps, {
        id: buttonId,
        "aria-controls": id,
        "aria-expanded": isOpen,
        onClick: this.onToggle,
        className: buttonClasses,
        type: "button"
      }), icon, /*#__PURE__*/_react.default.createElement("span", {
        className: (0, _classnames.default)('ouiIEFlexWrapFix', buttonContentClassName)
      }, buttonContent)), optionalAction, iconButton), /*#__PURE__*/_react.default.createElement("div", {
        className: "ouiAccordion__childWrapper",
        ref: function ref(node) {
          _this2.childWrapper = node;
        },
        tabIndex: -1,
        role: "region",
        "aria-labelledby": buttonId,
        id: id
      }, /*#__PURE__*/_react.default.createElement(_resize_observer.OuiResizeObserver, {
        onResize: this.setChildContentHeight
      }, function (resizeRef) {
        return /*#__PURE__*/_react.default.createElement("div", {
          ref: function ref(_ref) {
            _this2.setChildContentRef(_ref);

            resizeRef(_ref);
          }
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: childrenClasses
        }, childrenContent));
      })));
    }
  }]);
  return OuiAccordion;
}(_react.Component);
/* OUI -> EUI Aliases: Build-Time */


exports.OuiAccordion = OuiAccordion;
(0, _defineProperty2.default)(OuiAccordion, "defaultProps", {
  initialIsOpen: false,
  paddingSize: 'none',
  arrowDisplay: 'left',
  isLoading: false,
  isLoadingMessage: false
});
OuiAccordion.propTypes = {
  className: _propTypes.default.string,
  "aria-label": _propTypes.default.string,
  "data-test-subj": _propTypes.default.string,
  id: _propTypes.default.string.isRequired,

  /**
       * Class that will apply to the trigger for the accordion.
       */
  buttonClassName: _propTypes.default.string,

  /**
       * Apply more props to the triggering button
       */
  buttonProps: _propTypes.default.shape({
    className: _propTypes.default.string,
    "aria-label": _propTypes.default.string,
    "data-test-subj": _propTypes.default.string
  }),

  /**
       * Class that will apply to the trigger content for the accordion.
       */
  buttonContentClassName: _propTypes.default.string,

  /**
       * The content of the clickable trigger
       */
  buttonContent: _propTypes.default.node,

  /**
       * Will appear right aligned against the button. Useful for separate actions like deletions.
       */
  extraAction: _propTypes.default.node,

  /**
       * The accordion will start in the open state.
       */
  initialIsOpen: _propTypes.default.bool.isRequired,

  /**
       * Optional callback method called on open and close with a single `isOpen` parameter
       */
  onToggle: _propTypes.default.func,

  /**
       * The padding around the exposed accordion content.
       */
  paddingSize: _propTypes.default.oneOf(["none", "xs", "s", "m", "l", "xl"]),

  /**
       * Placement of the arrow indicator, or 'none' to hide it.
       */
  arrowDisplay: _propTypes.default.oneOf(["left", "right", "none"]),

  /**
       * Control the opening of accordion via prop
       */
  forceState: _propTypes.default.oneOf(["closed", "open"]),

  /**
       * Change `extraAction` and children into a loading spinner
       */
  isLoading: _propTypes.default.bool,

  /**
       * Choose whether the loading message replaces the content. Customize the message by passing a node
       */
  isLoadingMessage: _propTypes.default.oneOfType([_propTypes.default.bool.isRequired, _propTypes.default.node.isRequired])
};