"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MiniProgressStepper = void 0;
var _react = _interopRequireDefault(require("react"));
var _core = require("@kui-shell/core");
var _Tooltip = _interopRequireDefault(require("../spi/Tooltip"));
var _Spinner = _interopRequireDefault(require("../Views/Terminal/Block/Spinner"));
var _ProgressStepper = require("./ProgressStepper");
var _LinkStatus = require("./LinkStatus");
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.
 */
var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function (resolve) {
      resolve(value);
    });
  }
  return new (P || (P = Promise))(function (resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }
    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }
    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }
    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};
const strings = (0, _core.i18n)('plugin-client-common', 'code');
class MiniProgressStepper extends _react.default.PureComponent {
  progress() {
    const isComplete = this.isComplete();
    return _react.default.createElement("ol", {
      className: "pf-c-progress-stepper kui--progress-stepper",
      "data-mini": true,
      "data-is-complete": this.isComplete()
    }, this.props.steps.filter(_ => !isComplete || !_.optional).map(props => _react.default.createElement(MiniProgressStep, Object.assign({
      key: props.codeBlockId
    }, props, {
      status: props.status
    }))));
  }
  get nSteps() {
    return this.props.steps.length;
  }
  get nRequiredSteps() {
    return this.props.steps.filter(_ => !_.optional).length;
  }
  get nDone() {
    return this.props.steps.filter(_ => _.status === 'success').length;
  }
  isComplete() {
    // it may be greater, it we have successful validation for
    // optional steps
    return this.nDone >= this.nRequiredSteps;
  }
  render() {
    return this.progress();
  }
}
exports.MiniProgressStepper = MiniProgressStepper;
class MiniProgressStep extends _react.default.PureComponent {
  componentDidMount() {
    if (this.props.validate && this.props.status !== 'in-progress' && this.props.status !== 'success') {
      setTimeout(() => __awaiter(this, void 0, void 0, function* () {
        try {
          (0, _LinkStatus.emitLinkUpdate)(this.props.codeBlockId, 'in-progress');
          yield (0, _core.pexecInCurrentTab)(this.props.validate.toString(), undefined, true, true);
          (0, _LinkStatus.emitLinkUpdate)(this.props.codeBlockId, 'success');
        } catch (err) {
          (0, _LinkStatus.emitLinkUpdate)(this.props.codeBlockId, 'blank');
        }
      }));
    }
  }
  get status() {
    return this.props.status || 'blank';
  }
  get statusClass() {
    return (0, _ProgressStepper.statusToClassName)(this.status);
  }
  icon() {
    return this.status === 'in-progress' ? _react.default.createElement(_Spinner.default, null) : _react.default.createElement(_react.default.Fragment, null);
  }
  get tooltipText() {
    const title = strings('Code Block');
    const status = this.status === 'blank' && this.props.optional ? 'optional' : this.status;
    return `### ${title}
#### Status: ${strings('status.' + status)}

\`\`\`${this.props.language || ''}
${this.props.body}
\`\`\``;
  }
  render() {
    return _react.default.createElement(_Tooltip.default, {
      markdown: this.tooltipText,
      maxWidth: "30rem",
      position: "bottom-start"
    }, _react.default.createElement("li", {
      className: ['pf-c-progress-stepper__step', 'kui--progress-step', ...this.statusClass].join(' '),
      "data-optional": this.props.optional || undefined
    }, _react.default.createElement("div", {
      className: "pf-c-progress-stepper__step-connector"
    }, _react.default.createElement("a", {
      className: "kui--progress-step-status-icon-link",
      href: `#kui-link-${this.props.codeBlockId}`
    }, _react.default.createElement("span", {
      className: "pf-c-progress-stepper__step-icon kui--progress-step-status-icon"
    }, this.icon())))));
  }
}