"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPdfDownloadAction = isPdfDownloadAction;
exports.humanizeDownloadPdfStep = humanizeDownloadPdfStep;
exports.downloadPdfStepToAIString = downloadPdfStepToAIString;
const DownloadBaseStep_1 = __importStar(require("./DownloadBaseStep"));
const mablscript_step_types_1 = require("../../mablscript-step-types");
const DownloadStep_1 = require("@mablhq/test-model/steps/DownloadStep");
const yaml_utils_1 = require("../yaml-utils");
const PDF_STEPS_FEATURE = Object.freeze({ id: 'pdf_steps', label: 'PDF Steps' });
class DownloadPdfStep extends DownloadBaseStep_1.default {
    static getStepType() {
        return mablscript_step_types_1.StepType.DOWNLOAD_PDF;
    }
    constructor(values) {
        super({
            stepType: DownloadPdfStep.getStepType(),
            awaitSymbol: 'await_pdf',
            ...values,
        });
        this.tabUuid = values.awaitParameters?.uuid;
        this.mergeOptions({ feature: PDF_STEPS_FEATURE, timeoutMilliseconds: 60000 });
    }
    getMablStepName() {
        return DownloadStep_1.DownloadStep.pdfStepName;
    }
    buildDescriptor() {
        const descriptor = this.buildBaseDescriptor();
        if (this.tabUuid) {
            descriptor.tabIdentifier = this.tabUuid;
        }
        return descriptor;
    }
    static generateStepFromMablStep(mablDownloadStep) {
        const awaitParameters = {};
        const assertions = this.assertionsTemplate();
        Object.entries(mablDownloadStep.descriptor).forEach(([assertKey, expectedValue]) => {
            const snakeCasePropertyName = (0, DownloadBaseStep_1.toSnakeCasePropertyName)(assertKey);
            const matchedAssertionValueIndex = assertions.findIndex((assertion) => assertion.name === snakeCasePropertyName);
            if (matchedAssertionValueIndex >= 0) {
                assertions[matchedAssertionValueIndex].expected = expectedValue;
            }
        });
        if (isPdfDownloadAction(mablDownloadStep.awaitDownloadAction)) {
            awaitParameters.uuid = mablDownloadStep.awaitDownloadAction.pdfUUID;
        }
        const detectedProperties = DownloadStep_1.DownloadStep.populateDownloadArgsFromDescriptor(mablDownloadStep.descriptor);
        if (mablDownloadStep.descriptor.detected) {
            if (mablDownloadStep.descriptor.detected.file_mime_type) {
                detectedProperties.file_mime_type =
                    mablDownloadStep.descriptor.detected.file_mime_type;
            }
            if (mablDownloadStep.descriptor.detected.file_name) {
                detectedProperties.file_name =
                    mablDownloadStep.descriptor.detected.file_name;
            }
            if (mablDownloadStep.descriptor.detected.file_size_bytes !== undefined) {
                detectedProperties.file_size_bytes =
                    mablDownloadStep.descriptor.detected.file_size_bytes;
            }
        }
        return new DownloadPdfStep({
            detectedProperties,
            assertions,
            awaitParameters,
            aiPrompt: mablDownloadStep.descriptor.aiPrompt,
            fileNameMatchMode: mablDownloadStep.descriptor.fileNameMatchMode,
        });
    }
    static create(descriptor) {
        const step = new DownloadPdfStep(descriptor);
        step.generateStepId();
        return step;
    }
}
exports.default = DownloadPdfStep;
function isPdfDownloadAction(value) {
    return value.pdfUUID;
}
function humanizeDownloadPdfStep(step) {
    return `Open PDF and ${(0, DownloadBaseStep_1.humanizeDownloadBaseStep)(step)}`;
}
function downloadPdfStepToAIString(step) {
    const result = {
        stepType: step.stepType,
    };
    const activeAssertions = step.expectations();
    if (activeAssertions.length > 0) {
        result.assertions = activeAssertions.map((a) => ({
            property: a.name,
            expected: a.expected,
            ...(a.name === DownloadBaseStep_1.DownloadProperty.FileName &&
                step.fileNameMatchMode !== 'equals'
                ? { match_mode: step.fileNameMatchMode }
                : {}),
        }));
    }
    if (step.aiPrompt?.userPrompt) {
        result.ai_prompt = step.aiPrompt.userPrompt;
    }
    return (0, yaml_utils_1.toYaml)(result);
}
