import { AppApiRoute } from "../../ServiceRoute";
import Route from "../../Types/API/Route";
import FileService from "../Services/FileService";
import QueryHelper from "../Types/Database/QueryHelper";
import { LIMIT_PER_PROJECT } from "../../Types/Database/LimitMax";
export default class FileAttachmentMarkdownUtil {
    static async buildAttachmentMarkdown(input) {
        if (!input.modelId ||
            !input.attachmentIds ||
            input.attachmentIds.length === 0) {
            return "";
        }
        const uniqueIds = Array.from(new Set(input.attachmentIds
            .map((id) => {
            return id.toString();
        })
            .filter((value) => {
            return Boolean(value);
        })));
        if (uniqueIds.length === 0) {
            return "";
        }
        const files = await FileService.findBy({
            query: {
                _id: QueryHelper.any(uniqueIds),
            },
            limit: LIMIT_PER_PROJECT,
            skip: 0,
            select: {
                _id: true,
                name: true,
            },
            props: {
                isRoot: true,
            },
        });
        if (!files.length) {
            return "";
        }
        const fileById = new Map(files
            .filter((file) => {
            return Boolean(file._id);
        })
            .map((file) => {
            return [file._id.toString(), file];
        }));
        const attachmentLines = [];
        for (const id of input.attachmentIds) {
            const key = id.toString();
            const file = fileById.get(key);
            if (!file) {
                continue;
            }
            const fileName = file.name || "Attachment";
            const route = Route.fromString(AppApiRoute.toString())
                .addRoute(input.attachmentApiPath)
                .addRoute(`/${input.modelId.toString()}`)
                .addRoute(`/${key}`);
            attachmentLines.push(`- [${fileName}](${route.toString()})`);
        }
        if (!attachmentLines.length) {
            return "";
        }
        return `\n\n**Attachments:**\n${attachmentLines.join("\n")}\n`;
    }
}
//# sourceMappingURL=FileAttachmentMarkdownUtil.js.map