"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.humanizeAccessibilityCheck = humanizeAccessibilityCheck;
exports.accessibilityCheckStepToAIString = accessibilityCheckStepToAIString;
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
const mablscript_step_types_1 = require("../../mablscript-step-types");
const StepWithFind_1 = __importStar(require("./StepWithFind"));
const yaml_utils_1 = require("../yaml-utils");
const AccessibilityCheck_1 = require("@mablhq/test-model/steps/AccessibilityCheck");
class AccessibilityCheckStep extends StepWithFind_1.default {
    static getStepType() {
        return mablscript_step_types_1.StepType.ACCESSIBILITY_CHECK;
    }
    constructor(checkConfig, findDescriptor, findOptions) {
        super(AccessibilityCheckStep.getStepType(), findDescriptor, findOptions);
        this.options = {
            editable: true,
        };
        this.checkConfig = checkConfig;
    }
    generateMablscript() {
        const checkConfigArgs = this.checkConfig
            ? (0, mablscriptFind_1.buildStepArgumentString)({ params: { ...this.checkConfig }, legacy: true })
            : '';
        const checkConfig = `accessibility_check(${checkConfigArgs})`;
        if (this.findDescriptor) {
            return `${this.findDescriptorToMablscript()}.${checkConfig}`;
        }
        return checkConfig;
    }
    inputVariables() {
        return [];
    }
    static generateStepFromMablStep(mablAccessibilityCheckStep, importer) {
        const { findDescriptor, findOptions } = this.getFindDescriptorAndFindOptions(mablAccessibilityCheckStep, importer);
        if (findDescriptor &&
            ((0, mablscriptFind_1.isCustomFindDescriptor)(findDescriptor) ||
                (0, mablscriptFind_1.isFindOneDescriptor)(findDescriptor))) {
            if ((0, mablscriptFind_1.isFindOneDescriptor)(findDescriptor)) {
                findDescriptor.auxiliaryDescriptors?.forEach((descriptor) => (descriptor.type = mablscriptFind_1.FindType.FIND_ONE));
            }
            return new AccessibilityCheckStep(mablAccessibilityCheckStep.checkConfig, findDescriptor, findOptions);
        }
        const step = new AccessibilityCheckStep(mablAccessibilityCheckStep.checkConfig);
        step.setStepId(mablAccessibilityCheckStep.stepId());
        return step;
    }
}
AccessibilityCheckStep.actionCode = AccessibilityCheck_1.AccessibilityCheckStep.actionCode;
exports.default = AccessibilityCheckStep;
function humanizeAccessibilityCheck(step) {
    const failOnInfo = step.checkConfig?.failOn
        ? ` Fail on ${step.checkConfig?.failOn?.severity} severity or worse.`
        : ``;
    if (step.findDescriptor) {
        return `Run an accessibility check on ${(0, StepWithFind_1.humanizeFind)(step)}.${failOnInfo}`;
    }
    else if (step.checkConfig?.runInMainFrame) {
        return `Run an accessibility check against the current page.${failOnInfo}`;
    }
    return `Run an accessibility check against the current frame.${failOnInfo}`;
}
function accessibilityCheckStepToAIString(step) {
    const result = {
        stepType: step.stepType,
    };
    if (step.findDescriptor) {
        result.scope = 'element';
        result.target = (0, StepWithFind_1.findDescriptorToAIObject)(step.findDescriptor, step.findOptions);
    }
    else if (step.checkConfig?.runInMainFrame) {
        result.scope = 'page';
    }
    else {
        result.scope = 'frame';
    }
    if (step.checkConfig?.failOn?.severity) {
        result.fail_on_severity = step.checkConfig.failOn.severity;
    }
    return (0, yaml_utils_1.toYaml)(result);
}
