/*
 * 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 { CLI, Selectors } from '@kui-shell/test';
export function hasValidation(block) {
    return typeof block.valid === 'boolean';
}
export function hasCleanup(block) {
    return typeof block.cleanup === 'boolean';
}
export function checkBlockValidation(ctx, split, block, reverse = false) {
    return __awaiter(this, void 0, void 0, function* () {
        const codeBlock = yield ctx.app.client.$(`${split()} ${Selectors.Markdown.codeBlock(block.index)}`);
        yield codeBlock.waitForDisplayed({ timeout: CLI.waitTimeout });
        const icon = yield codeBlock.$(`.kui--code-block-status [icon="${block.valid ? 'Checkmark' : 'Error'}"]`);
        yield icon.waitForDisplayed({ timeout: CLI.waitTimeout, reverse });
    });
}
export function checkBlockCleanup(ctx, split, block) {
    return __awaiter(this, void 0, void 0, function* () {
        const codeBlock = yield ctx.app.client.$(`${split()} ${Selectors.Markdown.codeBlock(block.index)}`);
        yield codeBlock.waitForDisplayed({ timeout: CLI.waitTimeout });
        const icon = yield codeBlock.$(Selectors.Markdown.cleanupButton);
        yield icon.waitForDisplayed({ timeout: CLI.waitTimeout });
        yield icon.click();
        yield checkBlockValidation(ctx, split, Object.assign({}, block, { valid: true }), true);
    });
}
export function clickToExecuteBlock(split, block) {
    return __awaiter(this, void 0, void 0, function* () {
        const codeBlockSelector = `${split()} ${Selectors.Markdown.codeBlock(block.index)}`;
        const codeBlock = yield this.app.client.$(codeBlockSelector);
        yield codeBlock.waitForDisplayed({ timeout: CLI.waitTimeout });
        const runAction = yield codeBlock.$(Selectors.Markdown.runButton);
        yield codeBlock.moveTo();
        yield runAction.waitForDisplayed({ timeout: CLI.waitTimeout });
        yield runAction.click();
        const result = yield codeBlock.$(Selectors._RESULT);
        yield result.waitForDisplayed({ timeout: CLI.waitTimeout });
        yield this.app.client.waitUntil(() => __awaiter(this, void 0, void 0, function* () {
            const actualText = yield result.getText();
            return actualText.includes(block.output);
        }));
        // check ProgressStepper UI?
        if (block.status) {
            const stepper = yield this.app.client.$(`${block.status.position()} .kui--progress-stepper`);
            yield stepper.waitForDisplayed({ timeout: CLI.waitTimeout });
            const step = yield stepper.$(`li:nth-child(${block.status.index + 1})`);
            yield step.waitForDisplayed({ timeout: CLI.waitTimeout });
            const icon = block.status.status === 'done' ? 'Checkmark' : 'Error';
            const status = yield step.$(`.kui--progress-step-status-icon [icon="${icon}"]`);
            yield status.waitForDisplayed({ timeout: CLI.waitTimeout });
        }
    });
}
//# sourceMappingURL=markdown-helpers.js.map