"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenEmailStep = void 0;
const FindAction_1 = require("../actions/FindAction");
const mablscriptFind_1 = require("../mablscriptFind");
const MablStepWithFindAction_1 = require("../MablStepWithFindAction");
class OpenEmailStep extends MablStepWithFindAction_1.MablStepWithFindAction {
    static stepName = 'OpenEmail';
    static actionCode = 'open_email';
    static mablScriptStepNames = [OpenEmailStep.actionCode];
    static yamlMablScriptNames = [OpenEmailStep.stepName];
    tabUuid;
    configuration;
    constructor(descriptor, findAction) {
        super(OpenEmailStep.stepName, descriptor, OpenEmailStep.actionCode, findAction);
        if (!this.isValidDescriptor(this.descriptor)) {
            throw new Error(`${OpenEmailStep.stepName}: descriptor.tab must be a string`);
        }
        this.tabUuid = this.descriptor.tab;
        this.configuration = this.descriptor.configuration;
    }
    isValidDescriptor(value) {
        return typeof value.tab === 'string';
    }
    static validateActions(actions) {
        if (actions.length !== 1 || !(actions[0] instanceof FindAction_1.FindAction)) {
            throw new Error('Open email steps should have exactly one sub action and it should be a find');
        }
        return actions[0];
    }
    getStepName() {
        return OpenEmailStep.stepName;
    }
    static fromYaml(_stepName, stepArgs) {
        const findAction = FindAction_1.FindAction.findActionFromStepArgs(stepArgs, mablscriptFind_1.FindType.FIND_EMAIL);
        const step = new OpenEmailStep(stepArgs, findAction);
        return step;
    }
    static fromLegacyMablscript(args, actions) {
        const tabUuid = args[0] ?? '';
        if (actions.length !== 1 || !(actions[0] instanceof FindAction_1.FindAction)) {
            throw new Error('Open email steps should have exactly one sub action and it should be a find');
        }
        const findAction = actions[0];
        const findDescriptor = findAction.toDescriptor();
        const stepDescriptor = buildStepDescriptor(findDescriptor, tabUuid, findDescriptor.findTarget.configuration);
        const step = new OpenEmailStep(stepDescriptor, findAction);
        step.actions = actions;
        return step;
    }
    toMablscript() {
        return `${this.findAction.toMablscript()}.open_email("${this.tabUuid}")`;
    }
    getInputVariables() {
        return this.findAction.getInputVariables();
    }
    stepDescription() {
        const emailDescription = this.humanizeFind();
        return `Open ${emailDescription}`;
    }
}
exports.OpenEmailStep = OpenEmailStep;
function buildStepDescriptor(findEmailDescriptor, tabUuid, configuration) {
    return {
        tab: tabUuid,
        find: findEmailDescriptor,
        configuration,
        actionCode: OpenEmailStep.actionCode,
    };
}
