import FileService from "../Services/FileService";
import ObjectID from "../../Types/ObjectID";
import logger from "./Logger";
/*
 * Inline images uploaded through the markdown editor are addressed by
 * a high-entropy `imageAccessToken` rather than their ObjectID. The
 * editor inserts URLs of the form
 *
 *   {FILE_URL}/file/image/access-token/{token}
 *
 * Whenever a markdown field is published or unpublished we need to
 * flip the `isPublic` flag on the underlying File rows so the token
 * route serves anonymous traffic only when the parent allows it.
 */
const ACCESS_TOKEN_REGEX = /\/file\/image\/access-token\/([a-fA-F0-9]+)/g;
export const extractImageAccessTokens = (markdown) => {
    if (!markdown) {
        return [];
    }
    const tokens = new Set();
    const matches = markdown.matchAll(ACCESS_TOKEN_REGEX);
    for (const match of matches) {
        if (match[1]) {
            tokens.add(match[1]);
        }
    }
    return Array.from(tokens);
};
export const setIsPublicForMarkdownImages = async (markdown, isPublic) => {
    const tokens = extractImageAccessTokens(markdown);
    if (tokens.length === 0) {
        return;
    }
    for (const token of tokens) {
        try {
            const file = await FileService.findOneBy({
                query: {
                    imageAccessToken: token,
                },
                select: {
                    _id: true,
                },
                props: {
                    isRoot: true,
                    ignoreHooks: true,
                },
            });
            if (!file || !file._id) {
                continue;
            }
            await FileService.updateOneById({
                id: new ObjectID(file._id.toString()),
                data: {
                    isPublic,
                },
                props: {
                    isRoot: true,
                    ignoreHooks: true,
                },
            });
        }
        catch (err) {
            logger.error(`Failed to update isPublic for file token ${token}: ${String(err)}`);
        }
    }
};
//# sourceMappingURL=InlineImageAccessTokenSync.js.map