"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _i18n = require("@kui-shell/core/mdist/api/i18n");
var _Title = require("@patternfly/react-core/dist/esm/components/Title");
var _Wizard = require("@patternfly/react-core/dist/esm/components/Wizard");
var _Icons = _interopRequireDefault(require("../../../../spi/Icons"));
var _Footer = _interopRequireDefault(require("./Footer"));
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.
 */
/**
 * This component offers a fairly thin wrapper over PatternFly's Wizard, with a few extra features:
 *
 * 1. allows control of header theming
 * 2. adds a minimize/maximize toggler to the header
 *
 */

const strings = (0, _i18n.i18n)('plugin-client-common');
class KWizard extends _react.default.PureComponent {
  constructor(props) {
    super(props);
    this._toggleCollapsedHeader = () => this.setState(curState => ({
      collapsedHeader: !curState.collapsedHeader
    }));
    this.onBack = () => this.setState(curState => ({
      activeStep: Math.max(1, curState.activeStep - 1)
    }));
    this.onNext = () => this.setState(curState => ({
      activeStep: Math.min(this.props.steps.length, curState.activeStep + 1)
    }));
    this.onClose = () => {
      /* noop */
    };
    this.state = {
      activeStep: props.startAtStep || 1,
      collapsedHeader: props.collapsedHeader || false
    };
  }
  headerActions() {
    return _react.default.createElement("div", {
      className: "kui--wizard-header-action-buttons"
    }, _react.default.createElement("a", {
      className: "kui--wizard-collapse-button kui--block-action",
      onClick: this._toggleCollapsedHeader
    }, _react.default.createElement(_Icons.default, {
      icon: this.state.collapsedHeader ? 'WindowMaximize' : 'WindowMinimize'
    })));
  }
  title() {
    const label = this.props.title;
    return _react.default.createElement(_Title.Title, {
      headingLevel: "h2",
      size: _Title.TitleSizes['3xl'],
      className: "kui--wizard-header-title pf-c-wizard__title",
      "aria-label": typeof label === 'string' ? label : 'wizard title'
    }, label);
  }
  description() {
    return _react.default.createElement("div", {
      className: "pf-c-wizard__description"
    }, this.props.description);
  }
  header() {
    return _react.default.createElement(_react.default.Fragment, null, _react.default.createElement("div", {
      className: "kui--wizard-header"
    }, this.headerActions(), this.title(), this.description(), this.props.descriptionFooter));
  }
  footer() {
    if (this.props.steps.length === 1) {
      // use this if you want no footer (Next, Previous buttons)
      return _react.default.createElement(_react.default.Fragment, null);
    } else {
      // use this if you want the default footer
      return _react.default.createElement(_Footer.default, {
        onNext: this.onNext,
        onBack: this.onBack,
        onClose: this.onClose,
        isValid: this.state.activeStep < this.props.steps.length,
        firstStep: this.state.activeStep === 1,
        activeStep: this.props.steps[this.state.activeStep - 1],
        nextButtonText: strings('Next'),
        backButtonText: strings('Back'),
        cancelButtonText: strings('Cancel'),
        boxShadow: this.props.boxShadow,
        leftButtons: this.props.leftButtons,
        rightButtons: this.props.rightButtons,
        topContent: this.props.topContent
      });
    }
  }
  render() {
    const {
      steps
    } = this.props;
    // hmm there doesn't seem to be a better way to get the last
    // step's Next button to be disabled; it isn't by default
    if (steps[steps.length - 1]) {
      steps[steps.length - 1].enableNext = false;
    }
    // re: key={startAtStep} see https://github.com/patternfly/patternfly-react/issues/7184
    return _react.default.createElement("div", {
      className: "kui--wizard",
      "data-collapsed-header": this.state.collapsedHeader || undefined
    }, this.header(), _react.default.createElement("div", {
      className: "kui--wizard-main-content"
    }, _react.default.createElement(_Wizard.Wizard, {
      key: this.state.activeStep,
      steps: steps.length === 0 ? [{
        name: '',
        component: ''
      }] : steps,
      startAtStep: this.state.activeStep,
      footer: this.footer()
    })));
  }
}
exports.default = KWizard;