function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/*
Copyright (c) 2018-2019 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { getOverrides } from '../helpers/overrides.js';
import { Root as StyledRoot } from './styled-components.js';
import { STATE_CHANGE_TYPE } from './constants.js';
export default class Accordion extends React.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "state", {
      expanded: [],
      ...this.props.initialState
    });
  }

  onPanelChange(key, onChange, ...args) {
    let activeKeys = this.state.expanded;
    const {
      accordion
    } = this.props;

    if (accordion) {
      activeKeys = activeKeys[0] === key ? [] : [key];
    } else {
      activeKeys = [...activeKeys];
      const index = activeKeys.indexOf(key);
      const wasExpanded = index > -1;

      if (wasExpanded) {
        // remove active state
        activeKeys.splice(index, 1);
      } else {
        activeKeys.push(key);
      }
    }

    const newState = {
      expanded: activeKeys
    };
    this.internalSetState(STATE_CHANGE_TYPE.expand, newState);
    if (typeof onChange === 'function') onChange(...args);
  }

  internalSetState(type, changes) {
    const {
      stateReducer,
      onChange
    } = this.props;
    const newState = stateReducer(type, changes, this.state);
    this.setState(newState);
    typeof onChange === 'function' && onChange(newState);
  }

  getItems() {
    const {
      expanded
    } = this.state;
    const {
      accordion,
      disabled,
      children,
      renderPanelContent,
      renderAll,
      overrides
    } = this.props; // eslint-disable-next-line flowtype/no-weak-types

    const newChildren = React.Children.map(children, (child, index) => {
      if (!child) return; // If there is no key provide, use the panel order as default key

      const key = child.key || String(index);
      let isExpanded = false;

      if (accordion) {
        isExpanded = expanded[0] === key;
      } else {
        isExpanded = expanded.indexOf(key) > -1;
      }

      const props = {
        key,
        expanded: isExpanded || child.props.expanded,
        accordion,
        renderPanelContent,
        renderAll,
        overrides: child.props.overrides || overrides,
        disabled: child.props.disabled || disabled,
        onChange: (...args) => this.onPanelChange(key, child.props.onChange, ...args)
      };
      return React.cloneElement(child, props);
    });
    return newChildren;
  }

  getSharedProps() {
    const {
      disabled
    } = this.props;
    return {
      $disabled: disabled
    };
  }

  componentDidMount() {
    if (this.props.renderPanelContent) {
      console.warn('baseui:Accordion The `renderPanelContent` prop is depreacated. Please update your code to use `renderAll`.');
    }
  }

  render() {
    const sharedProps = this.getSharedProps();
    const {
      overrides = {}
    } = this.props;
    const {
      Root: RootOverride
    } = overrides;
    const [Root, rootProps] = getOverrides(RootOverride, StyledRoot);
    return React.createElement(Root, _extends({
      "data-baseweb": "accordion"
    }, sharedProps, rootProps), this.getItems());
  }

}

_defineProperty(Accordion, "defaultProps", {
  accordion: true,
  disabled: false,
  initialState: {
    expanded: []
  },
  renderPanelContent: false,
  renderAll: false,
  onChange: () => {},
  overrides: {},
  stateReducer: (type, newState) => newState
});