/*
 * 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.
 */
var __awaiter = (this && this.__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());
    });
};
import React from 'react';
import { isViewButton } from '@kui-shell/core/mdist/api/Response';
import Tooltip from '../../spi/Tooltip';
export default class ToolbarButton extends React.PureComponent {
    constructor() {
        super(...arguments);
        this._buttonOnclick = this.buttonOnclick.bind(this);
    }
    getCommand() {
        return __awaiter(this, void 0, void 0, function* () {
            const { tab, response, button, args } = this.props;
            if (isViewButton(button)) {
                return button.command;
            }
            else {
                const cmd = yield (typeof button.command === 'function' ? button.command(tab, response, args) : button.command);
                if (button.confirm) {
                    return `confirm "${cmd}"`;
                }
                else {
                    return cmd;
                }
            }
        });
    }
    buttonOnclick() {
        return __awaiter(this, void 0, void 0, function* () {
            const cmd = yield this.getCommand();
            const { tab, execUUID, response, button, args } = this.props;
            if (typeof cmd === 'string') {
                if (isViewButton(button) || button.confirm) {
                    return tab.REPL.qexec(cmd, undefined, undefined, { rethrowErrors: true });
                }
                else {
                    if (button.inPlace) {
                        return tab.REPL.reexec(cmd, { execUUID });
                    }
                    else {
                        return tab.REPL.pexec(cmd);
                    }
                }
            }
            else if (typeof cmd === 'function') {
                cmd(tab, response, args);
            }
            else {
                // e.g. this happens for kubectl edit on apply error, this is
                // because the error handling is captured by the
                // Editor/index.tsx
            }
        });
    }
    /** @return text of tooltip */
    get tooltipText() {
        const { button } = this.props;
        return button.label || button.mode;
    }
    /** @return label of the button */
    get buttonLabel() {
        const { button } = this.props;
        return button.icon ? button.icon : button.label || button.mode;
    }
    /** @return whether we need a tooltip wrapper */
    needsTooltip() {
        return this.tooltipText !== this.buttonLabel;
    }
    /** @return a react component for the button */
    content() {
        const { button } = this.props;
        return (React.createElement("span", { className: 'kui--tab-navigatable kui--notab-when-sidecar-hidden sidecar-bottom-stripe-button-as-button sidecar-bottom-stripe-button' +
                (button.icon ? ' kui--toolbar-button-with-icon' : '') },
            React.createElement("div", { role: "presentation", onClick: this._buttonOnclick, "data-mode": button.mode },
                React.createElement("span", { role: "tab" }, this.buttonLabel))));
    }
    render() {
        return !this.needsTooltip() ? this.content() : React.createElement(Tooltip, { content: this.tooltipText }, this.content());
    }
}
//# sourceMappingURL=ToolbarButton.js.map