"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _core = require("@kui-shell/core");
var _TwoFaceIcon = _interopRequireDefault(require("../../../spi/Icons/TwoFaceIcon"));
var _BlockModel = require("./BlockModel");
var _MutabilityContext = require("../../../Client/MutabilityContext");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
 * Copyright 2020 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 strings = (0, _core.i18n)('plugin-client-common');
function Action(props) {
  return _react.default.createElement(_TwoFaceIcon.default, {
    a: props.icon,
    b: props.iconB || 'Checkmark',
    delay: 4000,
    onClick: props.onClick,
    classNameB: "green-text",
    className: "kui--block-action",
    title: strings(props.title)
  });
}
class Actions extends _react.default.PureComponent {
  constructor() {
    super(...arguments);
    this._rerunHandler = () => {
      if ((0, _BlockModel.hasUUID)(this.props.model)) {
        this.props.tab.REPL.reexec(this.props.command, {
          execUUID: this.props.model.execUUID
        });
      }
    };
  }
  rerunAction(icon) {
    if ((0, _BlockModel.hasUUID)(this.props.model) && (0, _BlockModel.isRerunable)(this.props.model) && !(0, _BlockModel.isOutputOnly)(this.props.model) && this.props.tab && this.props.command) {
      if ((0, _BlockModel.isBeingRerun)(this.props.model)) {
        return _react.default.createElement(_react.default.Fragment, null);
      }
      return _react.default.createElement(Action, {
        icon: icon,
        iconB: "At",
        onClick: this._rerunHandler,
        title: strings(icon === 'Retry' ? 'Re-execute this command' : 'Execute this command')
      });
    }
  }
  copyAction() {
    return this.props.command && _react.default.createElement(Action, {
      icon: "Copy",
      onClick: () => navigator.clipboard.writeText(this.props.command),
      title: "Copy the command line to the clipboard"
    });
  }
  removeAction() {
    return this.props.willRemove && _react.default.createElement(Action, {
      icon: "WindowClose",
      onClick: evt => this.props.willRemove(evt, this.props.idx),
      title: 'Remove this block'
    });
  }
  render() {
    return _react.default.createElement(_MutabilityContext.MutabilityContext.Consumer, null, value => {
      if (!value.editable && value.executable) {
        if (this.props.isExecutable) {
          return _react.default.createElement("div", {
            className: "kui--block-actions-buttons",
            "data-has-been-rerun": (0, _BlockModel.hasBeenRerun)(this.props.model) || undefined
          }, this.rerunAction('Play'));
        }
      } else if (!value.editable && !value.executable) {
        return _react.default.createElement(_react.default.Fragment, null);
      } else {
        return _react.default.createElement("div", {
          className: "kui--block-actions-buttons kui--inverted-color-context"
        }, _react.default.createElement("div", {
          className: "kui-block-actions-others"
        }, this.copyAction(), value.executable && this.rerunAction('Play')), value.editable && this.removeAction());
      }
    });
  }
}
exports.default = Actions;