"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AssetResponseDto = exports.AssetResponseSchema = exports.SanitizedAssetResponseDto = void 0;
exports.mapAsset = mapAsset;
const openapi = require("@nestjs/swagger");
const nestjs_zod_1 = require("nestjs-zod");
const decorators_1 = require("../decorators");
const exif_dto_1 = require("./exif.dto");
const person_dto_1 = require("./person.dto");
const tag_dto_1 = require("./tag.dto");
const user_dto_1 = require("./user.dto");
const enum_1 = require("../enum");
const bytes_1 = require("../utils/bytes");
const date_1 = require("../utils/date");
const mime_types_1 = require("../utils/mime-types");
const zod_1 = __importDefault(require("zod"));
const SanitizedAssetResponseSchema = zod_1.default
    .object({
    id: zod_1.default.uuidv4().describe('Asset ID'),
    type: enum_1.AssetTypeSchema,
    thumbhash: zod_1.default
        .string()
        .describe('Thumbhash for thumbnail generation (base64) also used as the c query param for thumbnail cache busting.')
        .nullable(),
    originalMimeType: zod_1.default.string().optional().describe('Original MIME type'),
    localDateTime: zod_1.default
        .string()
        .meta({ format: 'date-time' })
        .describe('The local date and time when the photo/video was taken, derived from EXIF metadata. This represents the photographer\'s local time regardless of timezone, stored as a timezone-agnostic timestamp. Used for timeline grouping by "local" days and months.'),
    duration: zod_1.default.int32().min(0).nullable().describe('Video/gif duration in milliseconds (null for static images)'),
    livePhotoVideoId: zod_1.default.string().nullish().describe('Live photo video ID'),
    hasMetadata: zod_1.default.boolean().describe('Whether asset has metadata'),
    width: zod_1.default.int().min(0).nullable().describe('Asset width'),
    height: zod_1.default.int().min(0).nullable().describe('Asset height'),
})
    .meta({ id: 'SanitizedAssetResponseDto' });
class SanitizedAssetResponseDto extends (0, nestjs_zod_1.createZodDto)(SanitizedAssetResponseSchema) {
    static _OPENAPI_METADATA_FACTORY() {
        return {};
    }
}
exports.SanitizedAssetResponseDto = SanitizedAssetResponseDto;
const AssetStackResponseSchema = zod_1.default
    .object({
    id: zod_1.default.uuidv4().describe('Stack ID'),
    primaryAssetId: zod_1.default.uuidv4().describe('Primary asset ID'),
    assetCount: zod_1.default.int().min(0).describe('Number of assets in stack'),
})
    .meta({ id: 'AssetStackResponseDto' });
exports.AssetResponseSchema = SanitizedAssetResponseSchema.extend(zod_1.default.object({
    createdAt: zod_1.default
        .string()
        .meta({ format: 'date-time' })
        .describe('The UTC timestamp when the asset was originally uploaded to Immich.'),
    ownerId: zod_1.default.uuidv4().describe('Owner user ID'),
    owner: user_dto_1.UserResponseSchema.optional(),
    libraryId: zod_1.default
        .uuidv4()
        .nullish()
        .describe('Library ID')
        .meta(new decorators_1.HistoryBuilder().added('v1').deprecated('v1').getExtensions()),
    originalPath: zod_1.default.string().describe('Original file path'),
    originalFileName: zod_1.default.string().describe('Original file name'),
    fileCreatedAt: zod_1.default
        .string()
        .meta({ format: 'date-time' })
        .describe('The actual UTC timestamp when the file was created/captured, preserving timezone information. This is the authoritative timestamp for chronological sorting within timeline groups. Combined with timezone data, this can be used to determine the exact moment the photo was taken.'),
    fileModifiedAt: zod_1.default
        .string()
        .meta({ format: 'date-time' })
        .describe('The UTC timestamp when the file was last modified on the filesystem. This reflects the last time the physical file was changed, which may be different from when the photo was originally taken.'),
    updatedAt: zod_1.default
        .string()
        .meta({ format: 'date-time' })
        .describe('The UTC timestamp when the asset record was last updated in the database. This is automatically maintained by the database and reflects when any field in the asset was last modified.'),
    isFavorite: zod_1.default.boolean().describe('Is favorite'),
    isArchived: zod_1.default.boolean().describe('Is archived'),
    isTrashed: zod_1.default.boolean().describe('Is trashed'),
    isOffline: zod_1.default.boolean().describe('Is offline'),
    visibility: enum_1.AssetVisibilitySchema,
    exifInfo: exif_dto_1.ExifResponseSchema.optional(),
    tags: zod_1.default.array(tag_dto_1.TagResponseSchema).optional(),
    people: zod_1.default.array(person_dto_1.PersonResponseSchema).optional(),
    checksum: zod_1.default.string().describe('Base64 encoded SHA1 hash'),
    stack: AssetStackResponseSchema.nullish(),
    duplicateId: zod_1.default.uuidv4().nullish().describe('Duplicate group ID'),
    resized: zod_1.default
        .boolean()
        .optional()
        .describe('Is resized')
        .meta(new decorators_1.HistoryBuilder().added('v1').deprecated('v1.113.0').getExtensions()),
    isEdited: zod_1.default
        .boolean()
        .describe('Is edited')
        .meta(new decorators_1.HistoryBuilder().added('v2.5.0').beta('v2.5.0').getExtensions()),
}).shape).meta({ id: 'AssetResponseDto' });
class AssetResponseDto extends (0, nestjs_zod_1.createZodDto)(exports.AssetResponseSchema) {
    static _OPENAPI_METADATA_FACTORY() {
        return {};
    }
}
exports.AssetResponseDto = AssetResponseDto;
const peopleFromFaces = (faces) => {
    if (!faces) {
        return [];
    }
    const peopleMap = new Map();
    for (const face of faces) {
        if (face.person && !peopleMap.has(face.person.id)) {
            peopleMap.set(face.person.id, (0, person_dto_1.mapPerson)(face.person));
        }
    }
    return [...peopleMap.values()];
};
const mapStack = (entity) => {
    if (!entity.stack) {
        return null;
    }
    return {
        id: entity.stack.id,
        primaryAssetId: entity.stack.primaryAssetId,
        assetCount: entity.stack.assetCount ?? entity.stack.assets.length + 1,
    };
};
function mapAsset(entity, options = {}) {
    const { stripMetadata = false, withStack = false } = options;
    if (stripMetadata) {
        const sanitizedAssetResponse = {
            id: entity.id,
            type: entity.type,
            originalMimeType: mime_types_1.mimeTypes.lookup(entity.originalFileName),
            thumbhash: entity.thumbhash ? (0, bytes_1.hexOrBufferToBase64)(entity.thumbhash) : null,
            localDateTime: (0, date_1.asDateTimeString)(entity.localDateTime),
            duration: entity.duration,
            livePhotoVideoId: entity.livePhotoVideoId,
            hasMetadata: false,
            width: entity.width,
            height: entity.height,
        };
        return sanitizedAssetResponse;
    }
    return {
        id: entity.id,
        createdAt: (0, date_1.asDateTimeString)(entity.createdAt),
        ownerId: entity.ownerId,
        owner: entity.owner ? (0, user_dto_1.mapUser)(entity.owner) : undefined,
        libraryId: entity.libraryId,
        type: entity.type,
        originalPath: entity.originalPath,
        originalFileName: entity.originalFileName,
        originalMimeType: mime_types_1.mimeTypes.lookup(entity.originalFileName),
        thumbhash: entity.thumbhash ? (0, bytes_1.hexOrBufferToBase64)(entity.thumbhash) : null,
        fileCreatedAt: (0, date_1.asDateTimeString)(entity.fileCreatedAt),
        fileModifiedAt: (0, date_1.asDateTimeString)(entity.fileModifiedAt),
        localDateTime: (0, date_1.asDateTimeString)(entity.localDateTime),
        updatedAt: (0, date_1.asDateTimeString)(entity.updatedAt),
        isFavorite: options.auth?.user.id === entity.ownerId && entity.isFavorite,
        isArchived: entity.visibility === enum_1.AssetVisibility.Archive,
        isTrashed: !!entity.deletedAt,
        visibility: entity.visibility,
        duration: entity.duration,
        exifInfo: entity.exifInfo ? (0, exif_dto_1.mapExif)(entity.exifInfo) : undefined,
        livePhotoVideoId: entity.livePhotoVideoId,
        tags: entity.tags?.map((tag) => (0, tag_dto_1.mapTag)(tag)),
        people: peopleFromFaces(entity.faces),
        checksum: (0, bytes_1.hexOrBufferToBase64)(entity.checksum),
        stack: withStack ? mapStack(entity) : undefined,
        isOffline: entity.isOffline,
        hasMetadata: true,
        duplicateId: entity.duplicateId,
        resized: true,
        width: entity.width,
        height: entity.height,
        isEdited: entity.isEdited,
    };
}
//# sourceMappingURL=asset-response.dto.js.map