"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 _Progress = require("@patternfly/react-core/dist/esm/components/Progress");
var _CodeBlockEvents = require("../../../../Views/Terminal/Block/CodeBlockEvents");
var _madwizard = require("madwizard");
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 PatternFlyProgress = _react.default.lazy(() => Promise.resolve().then(() => require('@patternfly/react-core/dist/esm/components/Progress')).then(_ => ({
  default: _.Progress
})));
const strings = (0, _i18n.i18n)('plugin-client-common', 'code');
class Progress extends _react.default.PureComponent {
  constructor(props) {
    super(props);
    this.cleaners = [];
    this._onGetReadiness = (handler, codeBlockId) => {
      const ready = this.state.nextCodeBlocks.includes(codeBlockId);
      handler(ready);
    };
    this.state = Progress.getDerivedStateFromProps(props);
  }
  static getDerivedStateFromProps(props, state) {
    const prog = _madwizard.Graph.progress(props.codeBlocks, props.status, props.choices);
    const allCodeBlocks = _madwizard.Graph.blocks(props.codeBlocks);
    if (!state) {
      // initialize all blocks as not ready
      allCodeBlocks.forEach(_ => (0, _CodeBlockEvents.emitCodeBlockReadiness)(_.id, false));
    }
    const nextCodeBlocks = allCodeBlocks.filter(_ => prog.nextOrdinals.includes(_.order)).map(_ => _.id);
    if (state) {
      const noLongerReady = state.nextCodeBlocks.filter(_ => !nextCodeBlocks.includes(_));
      noLongerReady.forEach(id => (0, _CodeBlockEvents.emitCodeBlockReadiness)(id, false));
    }
    nextCodeBlocks.forEach(id => (0, _CodeBlockEvents.emitCodeBlockReadiness)(id, true));
    return Object.assign({}, prog, {
      nextCodeBlocks
    });
  }
  componentDidMount() {
    _madwizard.Graph.blocks(this.props.codeBlocks).forEach(({
      id
    }) => {
      (0, _CodeBlockEvents.onGetCodeBlockReadiness)(id, this._onGetReadiness);
      this.cleaners.push(() => (0, _CodeBlockEvents.offGetCodeBlockReadiness)(id, this._onGetReadiness));
    });
  }
  componentWillUnmount() {
    this.cleaners.forEach(_ => _());
  }
  render() {
    const {
      nDone,
      nError,
      nTotal
    } = this.state;
    const label = nError > 0 ? strings(nError === 1 ? 'xOfyFailingz' : 'xOfyFailingsz', nDone, nError, nTotal) : strings('xOfy', nDone, nTotal);
    const variant = nDone === nTotal ? _Progress.ProgressVariant.success : nError > 0 ? _Progress.ProgressVariant.danger : undefined;
    return _react.default.createElement(PatternFlyProgress, {
      "aria-label": "wizard progress",
      className: "kui--wizard-progress",
      min: 0,
      max: nTotal,
      value: nDone,
      title: this.props.title,
      label: label,
      valueText: label,
      size: "sm",
      variant: variant,
      measureLocation: "outside"
    });
  }
}
exports.default = Progress;