"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _core = require("@kui-shell/core");
var _PageSidebar = require("@patternfly/react-core/dist/esm/components/Page/PageSidebar");
var _Guidebooks = require("./props/Guidebooks");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
 * Copyright 2022 The Kubernetes Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const Nav = _react.default.lazy(() => Promise.resolve().then(() => require('@patternfly/react-core/dist/esm/components/Nav/Nav')).then(_ => ({
  default: _.Nav
})));
const NavItem = _react.default.lazy(() => Promise.resolve().then(() => require('@patternfly/react-core/dist/esm/components/Nav/NavItem')).then(_ => ({
  default: _.NavItem
})));
const NavList = _react.default.lazy(() => Promise.resolve().then(() => require('@patternfly/react-core/dist/esm/components/Nav/NavList')).then(_ => ({
  default: _.NavList
})));
const NavExpandable = _react.default.lazy(() => Promise.resolve().then(() => require('@patternfly/react-core/dist/esm/components/Nav/NavExpandable')).then(_ => ({
  default: _.NavExpandable
})));
class Sidebar extends _react.default.PureComponent {
  constructor(props) {
    super(props);
    this.cleaners = [];
    this.state = {
      nav: this.nav() // pre-render to help with persistence of isExpanded state
    };
  }

  get currentGuidebook() {
    return this.state ? this.state.currentGuidebook : undefined;
  }
  componentWillUnmount() {
    this.cleaners.forEach(_ => _());
  }
  /** e.g. product version */
  footer() {
    return this.props.productName && this.props.version && _react.default.createElement("div", {
      className: "kui--tab-container-sidebar-other flex-layout"
    }, _react.default.createElement("span", {
      className: "inline-flex flex-fill flex-align-end semi-bold"
    }, "v", this.props.version));
  }
  /** User has clicked to select a given Guidebook */
  onSelect(_) {
    const quiet = !this.props.noTopTabs;
    (0, _core.pexecInCurrentTab)(`${this.props.guidebooksCommand || 'replay'} ${(0, _core.encodeComponent)(_.filepath)}`, undefined, quiet);
    this.setState({
      currentGuidebook: _.notebook
    });
    // toggle the sidebar closed, after the user has clicked to open a
    // guidebook; we add a bit of delay, just cause it looks better
    this.props.toggleOpen();
  }
  /** Render the menu structure of the sidebar */
  menu() {
    // helps deal with isActive; if we don't have a currentGuidebook,
    // use the first one (for now)
    let first = !this.currentGuidebook;
    const renderItem = (_, idx) => {
      const thisIsTheFirstNavItem = (0, _Guidebooks.isGuidebook)(_) && first;
      if (thisIsTheFirstNavItem) {
        first = false;
      }
      return (0, _Guidebooks.isGuidebook)(_) ? _react.default.createElement(NavItem, {
        key: idx,
        "data-title": _.notebook,
        className: "kui--sidebar-nav-item",
        onClick: this.onSelect.bind(this, _),
        isActive: this.props.indicateActiveItem && (_.notebook === this.currentGuidebook || thisIsTheFirstNavItem)
      }, _.notebook) : (0, _Guidebooks.isMenu)(_) ? _react.default.createElement(NavExpandable, {
        key: idx,
        title: _.label,
        "data-title": _.label,
        className: "kui--sidebar-nav-menu",
        isExpanded: _.expanded !== false
      }, _.submenu.map(renderItem)) : undefined;
    };
    return Array.isArray(this.props.guidebooks) && _react.default.createElement(_react.default.Suspense, {
      fallback: _react.default.createElement("div", null)
    }, _react.default.createElement(Nav, {
      className: "kui--tab-container-sidebar-nav"
    }, _react.default.createElement(NavList, null, this.props.guidebooks.map(renderItem))));
  }
  /**
   * This is the entirety of the contents of the sidebar. The method
   * is named `nav` because that is what the PatternFly component
   * calls the corresponding property.
   */
  nav() {
    return Array.isArray(this.props.guidebooks) && _react.default.createElement(_react.default.Fragment, null, this.menu(), this.footer());
  }
  render() {
    return this.state.nav && _react.default.createElement(_PageSidebar.PageSidebar, {
      nav: this.state.nav,
      isNavOpen: this.props.isOpen,
      className: "kui--tab-container-sidebar"
    });
  }
}
exports.default = Sidebar;