function nonEmptyString(value) {
    const trimmed = value?.trim();
    return trimmed ? trimmed : null;
}
function isDataImageSource(source) {
    return source.trim().toLowerCase().startsWith("data:image/");
}
function escapeMarkdownImageAlt(value) {
    return value.replace(/\\/g, "\\\\").replace(/\]/g, "\\]");
}
function escapeMarkdownImageSource(value) {
    return value.replace(/\\/g, "\\\\").replace(/\)/g, "\\)");
}
export function renderProviderImageOutputAsAssistantMarkdown(image, options = {}) {
    const source = nonEmptyString(image.path) ?? nonEmptyString(image.url);
    if (source && !isDataImageSource(source)) {
        const altText = escapeMarkdownImageAlt(nonEmptyString(image.altText) ?? "Image");
        return {
            type: "assistant_message",
            text: `![${altText}](${escapeMarkdownImageSource(source)})`,
        };
    }
    const data = nonEmptyString(image.data) ?? (source && isDataImageSource(source) ? source : null);
    if (!data) {
        return null;
    }
    let materialized = null;
    try {
        materialized = options.materialize
            ? options.materialize({
                data,
                mimeType: nonEmptyString(image.mimeType),
            })
            : null;
    }
    catch {
        materialized = null;
    }
    if (!materialized?.path || isDataImageSource(materialized.path)) {
        return {
            type: "assistant_message",
            text: "Image output was omitted because it was not available as a file path or URL.",
        };
    }
    const altText = escapeMarkdownImageAlt(nonEmptyString(image.altText) ?? "Image");
    return {
        type: "assistant_message",
        text: `![${altText}](${escapeMarkdownImageSource(materialized.path)})`,
    };
}
//# sourceMappingURL=provider-image-output.js.map