"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessibilityCheckStep = exports.AccessibilityCheckSeverity = void 0;
const MablStep_1 = require("../MablStep");
const MablStepV2_1 = require("../MablStepV2");
const FindAction_1 = require("../actions/FindAction");
const ActionsUtils_1 = require("./ActionsUtils");
const mablscriptFind_1 = require("../mablscriptFind");
var AccessibilityCheckSeverity;
(function (AccessibilityCheckSeverity) {
    AccessibilityCheckSeverity["Critical"] = "critical";
    AccessibilityCheckSeverity["Minor"] = "minor";
    AccessibilityCheckSeverity["Moderate"] = "moderate";
    AccessibilityCheckSeverity["Serious"] = "serious";
})(AccessibilityCheckSeverity || (exports.AccessibilityCheckSeverity = AccessibilityCheckSeverity = {}));
class AccessibilityCheckStep extends MablStepV2_1.MablStepV2 {
    static stepName = 'AccessibilityCheck';
    static actionCode = 'accessibility_check';
    static mablScriptStepNames = [AccessibilityCheckStep.actionCode];
    static yamlMablScriptNames = [AccessibilityCheckStep.stepName];
    findAction;
    checkConfig;
    constructor(descriptor, findAction) {
        super(AccessibilityCheckStep.stepName, descriptor, AccessibilityCheckStep.actionCode);
        if (findAction) {
            this.findAction = findAction;
            this.actions = [findAction];
        }
        const maybeCheckConfig = {
            ...(descriptor.tags && { tags: descriptor.tags }),
            ...(descriptor.rules && { rules: descriptor.rules }),
            ...(descriptor.disabledRules && {
                disabledRules: descriptor.disabledRules,
            }),
            ...(descriptor.failOn && { failOn: descriptor.failOn }),
            ...(descriptor.runInMainFrame !== undefined && {
                runInMainFrame: descriptor.runInMainFrame,
            }),
            ...(descriptor.bypassCors !== undefined && {
                bypassCors: descriptor.bypassCors,
            }),
            ...(descriptor.disabledRulesOnIframes && {
                disabledRulesOnIframes: descriptor.disabledRulesOnIframes,
            }),
        };
        const hasCheckConfig = Object.values(maybeCheckConfig).filter((value) => !!value).length > 0;
        if (hasCheckConfig) {
            this.checkConfig = maybeCheckConfig;
        }
    }
    canContinueOnFailure() {
        return true;
    }
    getStepName() {
        return AccessibilityCheckStep.stepName;
    }
    static fromYaml(_stepName, stepArgs) {
        const findAction = stepArgs.find
            ? FindAction_1.FindAction.findActionFromStepArgs(stepArgs)
            : undefined;
        return new AccessibilityCheckStep(stepArgs, findAction);
    }
    static fromLegacyMablscript(args, actions) {
        const checkConfig = args[0];
        const findAction = actions.length
            ? ActionsUtils_1.ActionsUtils.validateSingleFindAction(actions)
            : undefined;
        const stepDescriptor = buildStepDescriptor(findAction, checkConfig);
        const step = new AccessibilityCheckStep(stepDescriptor, findAction);
        step.actions = actions;
        return step;
    }
    toMablscript() {
        let argString = '';
        if (this.checkConfig?.tags ||
            this.checkConfig?.disabledRules ||
            this.checkConfig?.rules) {
            argString = `{${(0, MablStep_1.buildObjectString)(this.checkConfig)}}`;
        }
        let findAction = '';
        if (this.findAction) {
            findAction = `${this.findAction.toMablscript()}.`;
        }
        return `${findAction}${AccessibilityCheckStep.mablScriptStepNames[0]}(${argString})`;
    }
    getInputVariables() {
        return this.findAction?.getInputVariables() ?? [];
    }
    stepDescription() {
        return 'Check accessibility';
    }
    static create(descriptor, findAction) {
        descriptor.id = descriptor.id ?? MablStepV2_1.MablStepV2.generateStepId();
        return new AccessibilityCheckStep(descriptor, findAction);
    }
}
exports.AccessibilityCheckStep = AccessibilityCheckStep;
function buildStepDescriptor(findAction, checkConfig) {
    if (findAction) {
        const find = findAction.toDescriptor();
        switch (find.findType) {
            case mablscriptFind_1.FindType.FIND_FIRST:
            case mablscriptFind_1.FindType.FIND_LAST:
            case mablscriptFind_1.FindType.FIND_ANY:
            case mablscriptFind_1.FindType.FIND_ONE:
                return {
                    ...checkConfig,
                    find,
                    actionCode: AccessibilityCheckStep.actionCode,
                };
            default:
                throw new Error(`Error generating step descriptor for ${AccessibilityCheckStep.stepName}: Unexpected find type ${find.findType}`);
        }
    }
    return {
        actionCode: AccessibilityCheckStep.actionCode,
        ...checkConfig,
    };
}
