"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReleaseStep = void 0;
const MablAction_1 = require("../MablAction");
const mablscriptFind_1 = require("../mablscriptFind");
const ActionsUtils_1 = require("./ActionsUtils");
const ReleaseStepDescriptor_1 = require("../types/ReleaseStepDescriptor");
const MablStepWithFindAction_1 = require("../MablStepWithFindAction");
class ReleaseStep extends MablStepWithFindAction_1.MablStepWithFindAction {
    static stepName = 'Release';
    static actionCode = 'release';
    static mablScriptStepNames = [ReleaseStep.actionCode];
    static yamlMablScriptNames = [ReleaseStep.stepName];
    releaseArgs;
    constructor(descriptor, findAction) {
        super(ReleaseStep.stepName, descriptor, ReleaseStep.actionCode, findAction);
        this.releaseArgs = descriptor.releaseArgs ?? { isHtml5: false };
    }
    static parseArgs(args) {
        if (args.length === 0) {
            return undefined;
        }
        if (args.length !== 1) {
            throw new Error(`Unexpected number of arguments to Release step.`);
        }
        if (!(0, ReleaseStepDescriptor_1.isReleaseArgs)(args[0])) {
            throw new Error(`Invalid argument passed to Release step.`);
        }
        return args[0];
    }
    getStepName() {
        return ReleaseStep.stepName;
    }
    static fromYaml(_stepName, stepArgs) {
        return new ReleaseStep(stepArgs);
    }
    static fromLegacyMablscript(args, actions) {
        const findAction = ActionsUtils_1.ActionsUtils.validateSingleFindAction(actions);
        const releaseArgs = ReleaseStep.parseArgs(args) ?? { isHtml5: false };
        const stepDescriptor = buildStepDescriptor(findAction, releaseArgs);
        const step = new ReleaseStep(stepDescriptor, findAction);
        step.actions = actions;
        return step;
    }
    toMablscript() {
        const args = (0, MablAction_1.convertObjectToMablscriptArgs)(this.releaseArgs);
        return `${this.findAction.toMablscript()}.release(${args})`;
    }
    getInputVariables() {
        return this.findAction.getInputVariables();
    }
    stepDescription() {
        const actionPrefix = this.releaseArgs.isHtml5
            ? 'Simulate release'
            : 'Release';
        const elementDescription = this.humanizeFind();
        return `${actionPrefix} on ${elementDescription}`;
    }
}
exports.ReleaseStep = ReleaseStep;
function buildStepDescriptor(findAction, releaseArgs) {
    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 {
                find,
                actionCode: 'release',
                releaseArgs,
            };
        default:
            throw new Error(`Error generating step descriptor for Release: Unexpected find type ${find.findType}`);
    }
}
