"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateVariableMobileStep = void 0;
exports.isCreateVariableMobileStep = isCreateVariableMobileStep;
const MablStepV2_1 = require("../../MablStepV2");
const CreateVariableStepDescriptor_1 = require("../../types/CreateVariableStepDescriptor");
const CreateVariableMobileStepDescriptor_1 = require("../../types/mobile/CreateVariableMobileStepDescriptor");
const MobileFindAction_1 = require("./actions/MobileFindAction");
const stepUtil_1 = require("./stepUtil");
class CreateVariableMobileStep extends MablStepV2_1.MablStepV2 {
    static stepName = 'CreateVariable';
    static yamlMablScriptNames = [CreateVariableMobileStep.stepName];
    findAction;
    static stepVersion = 2;
    constructor(descriptor) {
        super(CreateVariableMobileStep.stepName, descriptor, 'as');
        if ((0, CreateVariableMobileStepDescriptor_1.isCreateVariableMobileFindStepDescriptor)(this.descriptor)) {
            this.findAction = MobileFindAction_1.MobileFindAction.fromDescriptor(this.descriptor.find);
            this.actions = [this.findAction];
        }
    }
    stepDescription() {
        const varGenerator = this.descriptor.generator.type;
        switch (varGenerator) {
            case CreateVariableStepDescriptor_1.VariableGenerator.ATTRIBUTE:
                return `Assign variable "${this.descriptor.name}" value from element attribute "${this.descriptor.generator.attribute}"`;
            case CreateVariableStepDescriptor_1.VariableGenerator.PATTERN:
                return `Assign variable "${this.descriptor.name}" value from string generated with format "${this.descriptor.generator.pattern}"`;
            case CreateVariableStepDescriptor_1.VariableGenerator.EMAIL:
                return `Generate a new mabl mailbox email address`;
            case CreateVariableStepDescriptor_1.VariableGenerator.ELEMENT_COUNT:
                return `Assign variable "${this.descriptor.name}" value from element count using ${(0, stepUtil_1.humanizeCustomFind)(this.descriptor)}`;
            case CreateVariableStepDescriptor_1.VariableGenerator.JAVASCRIPT:
                return `Assign variable "${this.descriptor.name}" using script`;
            default:
                return `Assign variable "${this.descriptor.name}" a value`;
        }
    }
    static fromYaml(_stepName, stepDescriptor) {
        if (stepDescriptor.selector &&
            !stepDescriptor.find &&
            stepDescriptor.generator.find) {
            stepDescriptor.find = stepDescriptor.generator.find;
            delete stepDescriptor.generator.find;
            delete stepDescriptor.selector;
            delete stepDescriptor.selectorAncestors;
        }
        return new CreateVariableMobileStep(stepDescriptor);
    }
    static create(descriptor) {
        descriptor.id = descriptor.id ?? MablStepV2_1.MablStepV2.generateStepId();
        return new CreateVariableMobileStep(descriptor);
    }
}
exports.CreateVariableMobileStep = CreateVariableMobileStep;
function isCreateVariableMobileStep(value) {
    return (value?.getStepName &&
        value?.getStepName() === CreateVariableMobileStep.stepName &&
        !!value.descriptor);
}
