import { PromoNotes } from "../../../../models/index.js";
import define from "../../define.js";
import { ApiError } from "../../error.js";
import { getNote } from "../../common/getters.js";
import { readPromo } from "../../common/read-promo.js";
export const meta = {
    tags: [
        "notes"
    ],
    requireCredential: true,
    errors: {
        noSuchNote: {
            message: "No such note.",
            code: "NO_SUCH_NOTE",
            id: "d785b897-fcd3-4fe9-8fc3-b85c26e6c932"
        }
    }
};
export const paramDef = {
    type: "object",
    properties: {
        noteId: {
            type: "string",
            format: "misskey:id"
        }
    },
    required: [
        "noteId"
    ]
};
export default define(meta, paramDef, async (ps, user)=>{
    const [note, promo] = await Promise.all([
        getNote(ps.noteId, user, {
            allowAdservice: true
        }).catch((err)=>{
            if (err.id === "9725d0ce-ba28-4dde-95a7-2cbb2c15de24") throw new ApiError(meta.errors.noSuchNote);
            throw err;
        }),
        PromoNotes.findOneBy({
            noteId: ps.noteId
        })
    ]);
    if (note == null || promo == null || promo.expiresAt.getTime() <= Date.now() || promo.remainingCredits <= 0) {
        throw new ApiError(meta.errors.noSuchNote);
    }
    await readPromo(note, promo, user);
});
