"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetViewportStep = void 0;
const MablStepV2_1 = require("../MablStepV2");
class SetViewportStep extends MablStepV2_1.MablStepV2 {
    static stepName = 'SetViewport';
    static actionCode = 'set_viewport';
    static mablScriptStepNames = [SetViewportStep.actionCode];
    static yamlMablScriptNames = [SetViewportStep.stepName];
    size;
    constructor(descriptor) {
        super(SetViewportStep.stepName, descriptor, SetViewportStep.actionCode);
        if (!this.isValidDescriptor(this.descriptor)) {
            throw new Error(`${SetViewportStep.stepName}: descriptor.size must be defined`);
        }
        this.size = this.descriptor.size;
    }
    isValidDescriptor(value) {
        return value.size !== undefined;
    }
    getStepName() {
        return SetViewportStep.stepName;
    }
    static fromYaml(_stepName, stepDescriptor) {
        return new SetViewportStep(stepDescriptor);
    }
    static fromLegacyMablscript(args, _actions) {
        const stepDescriptor = {
            size: {
                width: args[0]?.size?.width ?? undefined,
                height: args[0]?.size?.height ?? undefined,
            },
        };
        const step = new SetViewportStep(stepDescriptor);
        step.actions = _actions;
        return step;
    }
    toMablscript() {
        let sizeArg;
        if (this.size.height && this.size.width) {
            sizeArg = `{ size : { width : ${this.size.width}, height : ${this.size.height} } }`;
        }
        else if (this.size.width) {
            sizeArg = `{ size : { width : ${this.size.width} } }`;
        }
        else if (this.size.height) {
            sizeArg = `{ size : { height : ${this.size.height} } }`;
        }
        return `set_viewport(${sizeArg})`;
    }
    stepDescription() {
        if (this.size) {
            let message = 'Set viewport size';
            const width = this.size.width;
            const height = this.size.height;
            if (width) {
                message += ` to width ${width}`;
                if (height) {
                    message += ` and height ${height}`;
                }
            }
            else if (height) {
                message += ` to height ${height}`;
            }
            return message;
        }
        return `Invalid set viewport step definition`;
    }
    static create(descriptor) {
        descriptor.id = descriptor.id ?? MablStepV2_1.MablStepV2.generateStepId();
        return new SetViewportStep(descriptor);
    }
}
exports.SetViewportStep = SetViewportStep;
