/*
 * Copyright 2021 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 { maybeKuiLink } from '@kui-shell/core';
import Icons from '../spi/Icons';
import { subscribeToLinkUpdates, unsubscribeToLinkUpdates } from './LinkStatus';
import '../../../web/scss/components/ProgressStepper/_index.scss';
function isStatus(status) {
    return (status === 'blank' ||
        status === 'info' ||
        status === 'minor' ||
        status === 'current' ||
        status === 'pending' ||
        status === 'in-progress' ||
        status === 'success' ||
        status === 'warning' ||
        status === 'error' ||
        status === 'unknown');
}
export function statusFromStatusVector(statusVector, isMinor) {
    return statusVector[0] !== 0
        ? 'success'
        : statusVector[1] !== 0
            ? 'error'
            : statusVector[2] !== 0
                ? 'in-progress'
                : isMinor
                    ? 'minor'
                    : 'blank';
}
export function statusToClassName(status) {
    return [`pf-m-${status}`];
}
const icons = {
    info: { icon: 'Info' },
    minor: { icon: '' },
    blank: { icon: '' },
    success: { icon: 'Checkmark' },
    warning: { icon: 'Warning', className: 'yellow-text' },
    error: { icon: 'Error', className: 'red-text' },
    unknown: { icon: 'Unknown', className: 'yellow-text' },
    current: { icon: 'Current' },
    pending: { icon: undefined },
    'in-progress': { icon: 'InProgress', className: 'kui--spin-animation yellow-text' }
};
export function statusToIcon(status) {
    const { icon, className } = icons[status];
    return icon && React.createElement(Icons, { icon: icon, className: className });
}
export class ProgressStep extends React.PureComponent {
    constructor(props) {
        super(props);
        this._statusUpdateHandler = (statusVector) => {
            this.setState(curState => {
                return { status: statusFromStatusVector(statusVector, curState.status === 'minor') };
            });
        };
        this.state = {
            status: typeof props.defaultStatus === 'string' ? props.defaultStatus : 'blank'
        };
        if (typeof props.defaultStatus !== 'string') {
            setTimeout(() => __awaiter(this, void 0, void 0, function* () {
                if (typeof props.defaultStatus !== 'string') {
                    const status = yield props.defaultStatus;
                    this.setState({ status });
                }
            }));
        }
    }
    get status() {
        return statusToClassName(this.state.status);
    }
    icon() {
        if (this.props.customIcon) {
            return this.props.customIcon;
        }
        else {
            return statusToIcon(this.state.status);
        }
    }
    /** Once we have mounted, subscribe to link status update events */
    componentDidMount() {
        if (this.props.liveStatusChannel) {
            subscribeToLinkUpdates(this.props.liveStatusChannel, this._statusUpdateHandler);
        }
    }
    /** Bye! */
    componentWillUnmount() {
        if (this.props.liveStatusChannel) {
            unsubscribeToLinkUpdates(this.props.liveStatusChannel, this._statusUpdateHandler);
        }
    }
    render() {
        return (React.createElement("li", { className: ['pf-c-progress-stepper__step', 'kui--progress-step', ...this.status].join(' '), "aria-label": "completed step," },
            React.createElement("div", { className: "pf-c-progress-stepper__step-connector" },
                React.createElement("span", { className: "pf-c-progress-stepper__step-icon kui--progress-step-status-icon" }, this.icon())),
            React.createElement("div", { className: "pf-c-progress-stepper__step-main" },
                this.props.title && React.createElement("div", { className: "pf-c-progress-stepper__step-title" }, this.props.title),
                this.props.children && React.createElement("div", { className: "pf-c-progress-stepper__step-description" }, this.props.children))));
    }
}
export class ProgressStepper extends React.PureComponent {
    render() {
        return (React.createElement("ol", { className: [
                'pf-c-progress-stepper',
                'kui--progress-stepper',
                `pf-m-${this.props.layout || 'vertical'}`,
                this.props.className
            ].join(' ') }, this.props.children));
    }
}
export function isProgressStepCompatible(props) {
    return (Array.isArray(props.children) &&
        props.children.length >= 3 &&
        props.children[0].type === 'strong' &&
        props.children[2].type === 'em' &&
        Array.isArray(props.children[2].props.children) &&
        props.children[2].props.children.length === 1 &&
        typeof props.children[2].props.children[0] === 'string' &&
        isStatus(props.children[2].props.children[0]));
}
/* export function hasLinkStatus(props: ProgressStepCompatible): props is ProgressStepCompatible<LinkStatusProps> {
  return (
    typeof props.children[0].props === 'object' &&
    Array.isArray(props.children[0].props.children) &&
    props.children[0].props.children.length === 1 &&
    typeof props.children[0].props.children[0].props === 'object' &&
    typeof props.children[0].props.children[0].props.href === 'string' &&
    !!maybeKuiLink(props.children[0].props.children[0].props.href)
  )
} */
export function liveStatusChannel(props) {
    return (typeof props.children[0].props.children[0] === 'object' &&
        maybeKuiLink(props.children[0].props.children[0].props.href));
}
//# sourceMappingURL=ProgressStepper.js.map