"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetFilesStep = void 0;
const MablAction_1 = require("../MablAction");
const mablscriptFind_1 = require("../mablscriptFind");
const MablStepWithFindAction_1 = require("../MablStepWithFindAction");
const ActionsUtils_1 = require("./ActionsUtils");
class SetFilesStep extends MablStepWithFindAction_1.MablStepWithFindAction {
    static stepName = 'SetFiles';
    static actionCode = 'set_files';
    static mablScriptStepNames = [SetFilesStep.actionCode];
    static yamlMablScriptNames = [SetFilesStep.stepName];
    files;
    constructor(descriptor, findAction) {
        super(SetFilesStep.stepName, descriptor, SetFilesStep.actionCode, findAction);
        if (!this.isValidDescriptor(this.descriptor)) {
            throw new Error(`${SetFilesStep.stepName}: descriptor.files must be an array`);
        }
        this.files = this.descriptor.files;
    }
    isValidDescriptor(value) {
        return Array.isArray(value.files);
    }
    getStepName() {
        return SetFilesStep.stepName;
    }
    static fromYaml(_stepName, stepArgs) {
        return new SetFilesStep(stepArgs);
    }
    static fromLegacyMablscript(args, actions) {
        const findAction = ActionsUtils_1.ActionsUtils.validateSingleFindAction(actions);
        const stepDescriptor = buildStepDescriptor(findAction, args);
        const step = new SetFilesStep(stepDescriptor, findAction);
        step.actions = actions;
        return step;
    }
    toMablscript() {
        const files = this.files
            .map((key) => (0, MablAction_1.convertObjectToMablscriptArgs)(key))
            .join(', ');
        return `${this.findAction.toMablscript()}.set_files(${files})`;
    }
    getInputVariables() {
        return this.findAction.getInputVariables();
    }
    stepDescription() {
        const fileCount = this.files.length;
        const fileText = fileCount === 1 ? 'file' : 'files';
        const elementDescription = this.humanizeFind();
        return `Set ${fileCount} ${fileText} on ${elementDescription}`;
    }
}
exports.SetFilesStep = SetFilesStep;
function buildStepDescriptor(findAction, fileArgs) {
    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:
            const files = fileArgs.map((arg) => ({
                ...(arg.id && { id: arg.id }),
                ...(arg.name && { name: arg.name }),
                ...((arg.size || arg.size === 0) && { size: arg.size }),
                ...(arg.type && { type: arg.type }),
                ...(arg.url && { url: arg.url }),
            }));
            return {
                files,
                find,
                actionCode: SetFilesStep.actionCode,
            };
        default:
            throw new Error(`Error generating step descriptor for ${SetFilesStep.stepName}: Unexpected find type ${find.findType}`);
    }
}
