"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MablStepV2 = void 0;
exports.isMablStepV2 = isMablStepV2;
exports.isMablStepV2WithMobileFindDescriptor = isMablStepV2WithMobileFindDescriptor;
const MablStep_1 = require("./MablStep");
const StepWithMobileFindDescriptor_1 = require("./types/mobile/StepWithMobileFindDescriptor");
const uuid_1 = require("uuid");
class MablStepV2 extends MablStep_1.MablStep {
    static mablScriptStepNames = [];
    static yamlMablScriptNames = ['MablStepV2'];
    descriptor;
    stepVersion = 2;
    constructor(name, descriptor, actionCode) {
        super(name, [], [], actionCode);
        super.setStepId(descriptor.id);
        this.descriptor = descriptor;
        this.descriptor.actionCode = actionCode;
        this.description = descriptor.description;
        this.disabled = descriptor.disabled;
        this.annotation = descriptor.annotation;
    }
    stepId() {
        return this.descriptor.id ?? super.stepId();
    }
    setStepId(id) {
        super.setStepId(id);
        this.descriptor.id = id;
    }
    static fromYaml(stepName, stepDescriptor, actionCode) {
        return new MablStepV2(stepName, stepDescriptor, actionCode);
    }
    static fromLegacyMablscript(_args, _actions) {
        throw new Error('Legacy mablscript conversion not supported for MablStepV2 base class');
    }
    static create(_descriptor) {
        throw new Error('create() must be implemented in subclass');
    }
    static generateStepId() {
        return (0, uuid_1.v4)();
    }
    getStepName() {
        return this.name;
    }
    toStepDescriptor() {
        return this.descriptor;
    }
    getFormattedStep() {
        const stepDescriptor = this.toStepDescriptor();
        const result = {
            ...stepDescriptor,
            description: this.getDescription(),
        };
        if (this.stepId()) {
            result.id = this.stepId();
        }
        if (this.annotation?.description || this.annotation?.note) {
            result.annotation = this.annotation;
        }
        if (this.isDisabled()) {
            result.disabled = true;
        }
        return {
            [this.getStepName()]: result,
        };
    }
    getDescription() {
        return (this.description ?? this.descriptor.description ?? this.stepDescription());
    }
    setDescription(description) {
        this.description = description;
        this.descriptor.description = description;
    }
    stepDescription() {
        throw new Error('Step description not implemented');
    }
    isDisabled() {
        return !!this.disabled;
    }
    setDisabled(disabled) {
        this.disabled = disabled;
        this.descriptor.disabled = disabled;
    }
    canContinueOnFailure() {
        return false;
    }
    canFailAtEnd() {
        return false;
    }
    getResolvedDescription() {
        return this.annotation?.description || this.getDescription();
    }
    toLineDiffFormat(omitStepId = false) {
        const descriptor = { ...this.toStepDescriptor() };
        if (omitStepId) {
            delete descriptor.id;
        }
        const lineDiffProperties = {
            annotation: this.annotation && Object.keys(this.annotation).length > 0
                ? this.annotation
                : undefined,
            disabled: this.isDisabled(),
            descriptor,
        };
        return this.stringify(lineDiffProperties);
    }
}
exports.MablStepV2 = MablStepV2;
function isMablStepV2(value) {
    if (!value) {
        return false;
    }
    const mablStep = value;
    return !!mablStep.getStepVersion && mablStep.getStepVersion() === 2;
}
function isMablStepV2WithMobileFindDescriptor(value) {
    return !!(isMablStepV2(value) && (0, StepWithMobileFindDescriptor_1.isStepWithMobileFindDescriptor)(value?.descriptor));
}
