import { MemorietArchives, Memoriets, Notes } from "../../../../models/index.js";
import { noteVisibilities } from "../../../../types.js";
import define from "../../define.js";
import { ApiError } from "../../error.js";
import { HOUR } from "../../../../const.js";
import { createNoteFromApiData } from "../../../../services/note/create-from-api.js";
import { genId } from "../../../../misc/gen-id.js";
export const meta = {
    tags: [
        "memoriet"
    ],
    requireCredential: true,
    limit: {
        duration: HOUR,
        max: 300
    },
    kind: "write:notes",
    res: {
        type: "object",
        optional: false,
        nullable: false,
        properties: {
            memoriet: {
                type: "object",
                optional: false,
                nullable: false,
                properties: {
                    id: {
                        type: "string",
                        optional: false,
                        nullable: false
                    },
                    expiresAt: {
                        type: "string",
                        optional: false,
                        nullable: true
                    },
                    textLayers: {
                        type: "array",
                        optional: false,
                        nullable: false,
                        items: {
                            type: "object",
                            optional: false,
                            nullable: false
                        }
                    },
                    note: {
                        type: "object",
                        optional: false,
                        nullable: false,
                        ref: "Note"
                    }
                }
            }
        }
    },
    errors: {
        noSuchArchive: {
            message: "No such Memoriet archive.",
            code: "NO_SUCH_MEMORIET_ARCHIVE",
            id: "53e0b18f-4f1d-49c0-ad79-ad2944cbe139",
            httpStatusCode: 404
        },
        cannotExpireToPast: {
            message: "Expiration time must be in the future.",
            code: "CANNOT_EXPIRE_TO_PAST",
            id: "12ff555a-fb0c-40ed-b253-1aa5d6ed2461"
        }
    }
};
export const paramDef = {
    type: "object",
    properties: {
        archiveId: {
            type: "string",
            format: "misskey:id"
        },
        visibility: {
            type: "string",
            enum: noteVisibilities
        },
        expiresAt: {
            type: "integer",
            nullable: true
        }
    },
    required: [
        "archiveId"
    ]
};
export default define(meta, paramDef, async (ps, user)=>{
    const archive = await MemorietArchives.findOneBy({
        id: ps.archiveId,
        userId: user.id
    });
    if (!archive) throw new ApiError(meta.errors.noSuchArchive);
    const expiresAt = typeof ps.expiresAt === "number" ? new Date(ps.expiresAt) : null;
    if (expiresAt != null && expiresAt.getTime() <= Date.now()) {
        throw new ApiError(meta.errors.cannotExpireToPast);
    }
    const note = await createNoteFromApiData(user, {
        text: archive.text ? `${archive.text.trim()}\n#Memoriet` : "#Memoriet",
        cw: archive.cw,
        fileIds: archive.fileIds,
        visibility: ps.visibility ?? archive.visibility,
        localOnly: true
    }, new Date());
    const memoriet = await Memoriets.save({
        id: genId(),
        createdAt: new Date(),
        userId: user.id,
        noteId: note.id,
        expiresAt,
        textLayers: archive.textLayers
    });
    return {
        memoriet: {
            id: memoriet.id,
            expiresAt: memoriet.expiresAt?.toISOString() ?? null,
            textLayers: memoriet.textLayers,
            note: await Notes.pack(note, user)
        }
    };
});
