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 CheckIndeterminateIcon from '../icon/check-indeterminate.js';
import PlusIcon from '../icon/plus.js';
import { StyledIconContainer, StyledItemContent, StyledTreeItemList, StyledTreeItem } from './styled-components.js';
import { getOverride, getOverrideProps } from '../helpers/overrides.js';
export default class TreeNode extends React.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "onToggle", () => {
      const {
        onToggle,
        node
      } = this.props;

      if (onToggle) {
        onToggle(node);
      }
    });
  }

  render() {
    const {
      node,
      onToggle,
      overrides = {}
    } = this.props;
    const {
      children,
      isExpanded,
      label
    } = node;
    const hasChildren = children && children.length !== 0;
    const {
      TreeItemList: TreeItemListOverride,
      TreeItem: TreeItemOverride,
      TreeItemContent: TreeItemContentOverride,
      IconContainer: IconContainerOverride,
      ExpandIcon: ExapandIconOverride,
      CollapseIcon: CollapseIconOverride
    } = overrides;
    const TreeItemList = getOverride(TreeItemListOverride) || StyledTreeItemList;
    const TreeItem = getOverride(TreeItemOverride) || StyledTreeItem;
    const TreeItemContent = getOverride(TreeItemContentOverride) || StyledItemContent;
    const IconContainer = getOverride(IconContainerOverride) || StyledIconContainer;
    const ExpandIcon = getOverride(ExapandIconOverride) || PlusIcon;
    const CollapseIcon = getOverride(CollapseIconOverride) || CheckIndeterminateIcon;
    return React.createElement(TreeItem, _extends({
      role: "treeitem",
      "aria-expanded": isExpanded ? true : false,
      $isLeafNode: !hasChildren
    }, getOverrideProps(TreeItemOverride)), React.createElement(TreeItemContent, _extends({
      onClick: this.onToggle
    }, getOverrideProps(TreeItemContentOverride)), hasChildren && React.createElement(IconContainer, getOverrideProps(IconContainerOverride), !isExpanded ? React.createElement(ExpandIcon, getOverrideProps(ExapandIconOverride)) : React.createElement(CollapseIcon, getOverrideProps(CollapseIconOverride))), typeof label === 'function' ? label(node) : label), children && isExpanded && React.createElement(TreeItemList, _extends({
      role: "group",
      $isChildNode: true
    }, getOverrideProps(TreeItemListOverride)), children.map((node, index) => React.createElement(TreeNode, {
      key: index,
      node: node,
      onToggle: onToggle,
      overrides: overrides
    }))));
  }

}