"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getActorDisplayNameFromObject = exports.getImagesInfoFromObject = exports.getActorAttributesFromObject = void 0;
const misc_1 = require("../../../../helpers/custom-validators/activitypub/misc");
const constants_1 = require("../../../../initializers/constants");
const core_utils_1 = require("../../../../../shared/core-utils");
const extra_utils_1 = require("../../../../../shared/extra-utils");
function getActorAttributesFromObject(actorObject, followersCount, followingCount) {
    var _a;
    return {
        type: actorObject.type,
        preferredUsername: actorObject.preferredUsername,
        url: actorObject.id,
        publicKey: actorObject.publicKey.publicKeyPem,
        privateKey: null,
        followersCount,
        followingCount,
        inboxUrl: actorObject.inbox,
        outboxUrl: actorObject.outbox,
        followersUrl: actorObject.followers,
        followingUrl: actorObject.following,
        sharedInboxUrl: ((_a = actorObject.endpoints) === null || _a === void 0 ? void 0 : _a.sharedInbox)
            ? actorObject.endpoints.sharedInbox
            : null
    };
}
exports.getActorAttributesFromObject = getActorAttributesFromObject;
function getImagesInfoFromObject(actorObject, type) {
    const iconsOrImages = type === 1
        ? actorObject.icons || actorObject.icon
        : actorObject.image;
    return normalizeIconOrImage(iconsOrImages)
        .map(iconOrImage => {
        const mimetypes = constants_1.MIMETYPES.IMAGE;
        if (iconOrImage.type !== 'Image' || !(0, misc_1.isActivityPubUrlValid)(iconOrImage.url))
            return undefined;
        let extension;
        if (iconOrImage.mediaType) {
            extension = mimetypes.MIMETYPE_EXT[iconOrImage.mediaType];
        }
        else {
            const tmp = (0, core_utils_1.getLowercaseExtension)(iconOrImage.url);
            if (mimetypes.EXT_MIMETYPE[tmp] !== undefined)
                extension = tmp;
        }
        if (!extension)
            return undefined;
        return {
            name: (0, extra_utils_1.buildUUID)() + extension,
            fileUrl: iconOrImage.url,
            height: iconOrImage.height,
            width: iconOrImage.width,
            type
        };
    })
        .filter(i => !!i);
}
exports.getImagesInfoFromObject = getImagesInfoFromObject;
function getActorDisplayNameFromObject(actorObject) {
    return actorObject.name || actorObject.preferredUsername;
}
exports.getActorDisplayNameFromObject = getActorDisplayNameFromObject;
function normalizeIconOrImage(icon) {
    if (Array.isArray(icon))
        return icon;
    if (icon)
        return [icon];
    return [];
}
//# sourceMappingURL=object-to-model-attributes.js.map