"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DownloadStep = void 0;
const MablStepV2_1 = require("../MablStepV2");
const MablAction_1 = require("../MablAction");
const AwaitDownloadAction_1 = require("../actions/AwaitDownloadAction");
const AwaitPDFDownloadAction_1 = require("../actions/AwaitPDFDownloadAction");
const mablscriptFind_1 = require("../mablscriptFind");
class DownloadStep extends MablStepV2_1.MablStepV2 {
    static stepName = 'DownloadStep';
    static pdfStepName = 'DownloadPdfStep';
    static actionCode = 'assert_download';
    static mablScriptStepNames = [DownloadStep.actionCode];
    static yamlMablScriptNames = [
        DownloadStep.stepName,
        DownloadStep.pdfStepName,
    ];
    awaitDownloadAction;
    constructor(stepName, descriptor) {
        super(stepName, descriptor, DownloadStep.actionCode);
        const isPdfDownload = stepName === DownloadStep.pdfStepName;
        if (isPdfDownload) {
            this.awaitDownloadAction = new AwaitPDFDownloadAction_1.AwaitPDFDownloadAction('await_pdf', [
                { uuid: descriptor.tabIdentifier },
            ]);
        }
        else {
            this.awaitDownloadAction = new AwaitDownloadAction_1.AwaitDownloadAction('await_download', []);
        }
        this.actions = [this.awaitDownloadAction];
    }
    static populateDownloadArgsFromDescriptor(descriptor) {
        return {
            file_name: descriptor.fileName,
            file_mime_type: descriptor.mimeType,
            file_md5: descriptor.fileMd5,
            file_sha256: descriptor.fileSha256,
            file_size_bytes: descriptor.sizeBytes,
            file_size_bytes_max: descriptor.sizeBytesMax,
            file_size_bytes_min: descriptor.sizeBytesMin,
            ai_prompt: descriptor.aiPrompt,
            file_name_match_mode: descriptor.fileNameMatchMode,
        };
    }
    getStepName() {
        if (this.isPDFDownload()) {
            return DownloadStep.pdfStepName;
        }
        return DownloadStep.stepName;
    }
    isPDFDownload() {
        return this.awaitDownloadAction instanceof AwaitPDFDownloadAction_1.AwaitPDFDownloadAction;
    }
    static fromYaml(stepName, stepArgs) {
        return new DownloadStep(stepName, stepArgs);
    }
    static fromLegacyMablscript(args, actions) {
        DownloadStep.validateLegacyActions(actions);
        const fileAttributes = (args[0] ?? {});
        const awaitDownloadAction = actions[0];
        const stepDescriptor = {
            fileName: fileAttributes.file_name,
            mimeType: fileAttributes.file_mime_type,
            sizeBytes: fileAttributes.file_size_bytes,
            sizeBytesMax: fileAttributes.file_size_bytes_max,
            sizeBytesMin: fileAttributes.file_size_bytes_min,
            fileMd5: fileAttributes.file_md5,
            fileSha256: fileAttributes.file_sha256,
            aiPrompt: fileAttributes.ai_prompt,
            fileNameMatchMode: fileAttributes.file_name_match_mode,
        };
        const isPdfDownload = awaitDownloadAction instanceof AwaitPDFDownloadAction_1.AwaitPDFDownloadAction;
        if (isPdfDownload) {
            stepDescriptor.tabIdentifier = awaitDownloadAction.pdfUUID;
        }
        const stepName = isPdfDownload
            ? DownloadStep.pdfStepName
            : DownloadStep.stepName;
        const step = new DownloadStep(stepName, stepDescriptor);
        return step;
    }
    static validateLegacyActions(actions) {
        if (actions.length !== 1 ||
            !(actions[0] instanceof AwaitDownloadAction_1.AwaitDownloadAction ||
                actions[0] instanceof AwaitPDFDownloadAction_1.AwaitPDFDownloadAction)) {
            throw new Error(`Download step should contain one await download or one await pdf sub action`);
        }
    }
    toMablscript() {
        const meaningfulAttributes = DownloadStep.populateDownloadArgsFromDescriptor(this.toStepDescriptor());
        const mablArgs = (0, mablscriptFind_1.buildStepArgumentString)({
            params: meaningfulAttributes,
            legacy: true,
        });
        return `${this.awaitDownloadAction.toMablscript()}.assert_download(${mablArgs ?? '{}'})`;
    }
    getInputVariables() {
        return MablAction_1.MablAction.findUniqueVariableReferencesInValue(this.descriptor);
    }
    stepDescription() {
        return 'Assert file download';
    }
    static create(descriptor, stepName = DownloadStep.stepName) {
        descriptor.id = descriptor.id ?? MablStepV2_1.MablStepV2.generateStepId();
        return new DownloadStep(stepName, descriptor);
    }
}
exports.DownloadStep = DownloadStep;
