import { DriveFiles, MemorietArchives } from "../../../../models/index.js";
import define from "../../define.js";
export const meta = {
    tags: [
        "memoriet"
    ],
    requireCredential: true,
    kind: "read:account",
    res: {
        type: "array",
        optional: false,
        nullable: false,
        items: {
            type: "object",
            optional: false,
            nullable: false,
            properties: {
                id: {
                    type: "string",
                    optional: false,
                    nullable: false
                },
                createdAt: {
                    type: "string",
                    optional: false,
                    nullable: false
                },
                deletedAt: {
                    type: "string",
                    optional: false,
                    nullable: false
                },
                text: {
                    type: "string",
                    optional: false,
                    nullable: true
                },
                cw: {
                    type: "string",
                    optional: false,
                    nullable: true
                },
                fileIds: {
                    type: "array",
                    optional: false,
                    nullable: false,
                    items: {
                        type: "string"
                    }
                },
                files: {
                    type: "array",
                    optional: false,
                    nullable: false,
                    items: {
                        type: "object",
                        ref: "DriveFile"
                    }
                },
                textLayers: {
                    type: "array",
                    optional: false,
                    nullable: false,
                    items: {
                        type: "object",
                        optional: false,
                        nullable: false
                    }
                },
                visibility: {
                    type: "string",
                    optional: false,
                    nullable: false
                }
            }
        }
    }
};
export const paramDef = {
    type: "object",
    properties: {
        limit: {
            type: "integer",
            minimum: 1,
            maximum: 50,
            default: 20
        },
        offset: {
            type: "integer",
            minimum: 0,
            default: 0
        }
    },
    required: []
};
export default define(meta, paramDef, async (ps, user)=>{
    const archives = await MemorietArchives.find({
        where: {
            userId: user.id
        },
        order: {
            deletedAt: "DESC"
        },
        skip: ps.offset,
        take: ps.limit
    });
    const files = await Promise.all(archives.map((archive)=>DriveFiles.packMany(archive.fileIds)));
    return archives.map((archive, index)=>({
            id: archive.id,
            createdAt: archive.createdAt.toISOString(),
            deletedAt: archive.deletedAt.toISOString(),
            text: archive.text,
            cw: archive.cw,
            fileIds: archive.fileIds,
            files: files[index],
            textLayers: archive.textLayers,
            visibility: archive.visibility
        }));
});
