/*
 * 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.
 */
import React from 'react';
import { i18n } from '@kui-shell/core/mdist/api/i18n';
import TwoFaceIcon from '../../../spi/Icons/TwoFaceIcon';
import { hasUUID, hasBeenRerun, isBeingRerun, isOutputOnly, isRerunable } from './BlockModel';
import { MutabilityContext } from '../../../Client/MutabilityContext';
const strings = i18n('plugin-client-common');
function Action(props) {
    return (React.createElement(TwoFaceIcon, { a: props.icon, b: props.iconB || 'Checkmark', delay: 4000, onClick: props.onClick, classNameB: "green-text", className: "kui--block-action", title: strings(props.title) }));
}
export default class Actions extends React.PureComponent {
    constructor() {
        super(...arguments);
        this._rerunHandler = () => {
            if (hasUUID(this.props.model)) {
                this.props.tab.REPL.reexec(this.props.command, { execUUID: this.props.model.execUUID });
            }
        };
    }
    rerunAction(icon) {
        if (hasUUID(this.props.model) &&
            isRerunable(this.props.model) &&
            !isOutputOnly(this.props.model) &&
            this.props.tab &&
            this.props.command) {
            if (isBeingRerun(this.props.model)) {
                return React.createElement(React.Fragment, null);
            }
            return (React.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.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.createElement(Action, { icon: "WindowClose", onClick: evt => this.props.willRemove(evt, this.props.idx), title: 'Remove this block' })));
    }
    render() {
        return (React.createElement(MutabilityContext.Consumer, null, value => {
            if (!value.editable && value.executable) {
                if (this.props.isExecutable) {
                    return (React.createElement("div", { className: "kui--block-actions-buttons", "data-has-been-rerun": hasBeenRerun(this.props.model) || undefined }, this.rerunAction('Play')));
                }
            }
            else if (!value.editable && !value.executable) {
                return React.createElement(React.Fragment, null);
            }
            else {
                return (React.createElement("div", { className: "kui--block-actions-buttons kui--inverted-color-context" },
                    React.createElement("div", { className: "kui-block-actions-others" },
                        this.copyAction(),
                        value.executable && this.rerunAction('Play')),
                    value.editable && this.removeAction()));
            }
        }));
    }
}
//# sourceMappingURL=Actions.js.map