"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.YoutubeDLInfoBuilder = void 0;
const constants_1 = require("../../initializers/constants");
const core_utils_1 = require("../core-utils");
const misc_1 = require("../custom-validators/activitypub/misc");
class YoutubeDLInfoBuilder {
    constructor(info) {
        this.info = Object.assign({}, info);
    }
    getInfo() {
        const obj = this.buildVideoInfo(this.normalizeObject(this.info));
        if (obj.name && obj.name.length < constants_1.CONSTRAINTS_FIELDS.VIDEOS.NAME.min)
            obj.name += ' video';
        return obj;
    }
    normalizeObject(obj) {
        const newObj = {};
        for (const key of Object.keys(obj)) {
            if (key === 'resolution')
                continue;
            const value = obj[key];
            if (typeof value === 'string') {
                newObj[key] = value.normalize();
            }
            else {
                newObj[key] = value;
            }
        }
        return newObj;
    }
    buildOriginallyPublishedAt(obj) {
        let originallyPublishedAt = null;
        const uploadDateMatcher = /^(\d{4})(\d{2})(\d{2})$/.exec(obj.upload_date);
        if (uploadDateMatcher) {
            originallyPublishedAt = new Date();
            originallyPublishedAt.setHours(0, 0, 0, 0);
            const year = parseInt(uploadDateMatcher[1], 10);
            const month = parseInt(uploadDateMatcher[2], 10) - 1;
            const day = parseInt(uploadDateMatcher[3], 10);
            originallyPublishedAt.setFullYear(year, month, day);
        }
        return originallyPublishedAt;
    }
    buildVideoInfo(obj) {
        return {
            name: this.titleTruncation(obj.title),
            description: this.descriptionTruncation(obj.description),
            category: this.getCategory(obj.categories),
            licence: this.getLicence(obj.license),
            language: this.getLanguage(obj.language),
            nsfw: this.isNSFW(obj),
            tags: this.getTags(obj.tags),
            thumbnailUrl: obj.thumbnail || undefined,
            urls: this.buildAvailableUrl(obj),
            originallyPublishedAt: this.buildOriginallyPublishedAt(obj),
            ext: obj.ext
        };
    }
    buildAvailableUrl(obj) {
        const urls = [];
        if (obj.url)
            urls.push(obj.url);
        if (obj.urls) {
            if (Array.isArray(obj.urls))
                urls.push(...obj.urls);
            else
                urls.push(obj.urls);
        }
        const formats = Array.isArray(obj.formats)
            ? obj.formats
            : [];
        for (const format of formats) {
            if (!format.url)
                continue;
            urls.push(format.url);
        }
        const thumbnails = Array.isArray(obj.thumbnails)
            ? obj.thumbnails
            : [];
        for (const thumbnail of thumbnails) {
            if (!thumbnail.url)
                continue;
            urls.push(thumbnail.url);
        }
        if (obj.thumbnail)
            urls.push(obj.thumbnail);
        for (const subtitleKey of Object.keys(obj.subtitles || {})) {
            const subtitles = obj.subtitles[subtitleKey];
            if (!Array.isArray(subtitles))
                continue;
            for (const subtitle of subtitles) {
                if (!subtitle.url)
                    continue;
                urls.push(subtitle.url);
            }
        }
        return urls.filter(u => u && (0, misc_1.isUrlValid)(u));
    }
    titleTruncation(title) {
        return (0, core_utils_1.peertubeTruncate)(title, {
            length: constants_1.CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
            separator: /,? +/,
            omission: ' […]'
        });
    }
    descriptionTruncation(description) {
        if (!description || description.length < constants_1.CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min)
            return undefined;
        return (0, core_utils_1.peertubeTruncate)(description, {
            length: constants_1.CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
            separator: /,? +/,
            omission: ' […]'
        });
    }
    isNSFW(info) {
        return (info === null || info === void 0 ? void 0 : info.age_limit) >= 16;
    }
    getTags(tags) {
        if (Array.isArray(tags) === false)
            return [];
        return tags
            .filter(t => t.length < constants_1.CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > constants_1.CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
            .map(t => t.normalize())
            .slice(0, 5);
    }
    getLicence(licence) {
        if (!licence)
            return undefined;
        if (licence.includes('Creative Commons Attribution'))
            return 1;
        for (const key of Object.keys(constants_1.VIDEO_LICENCES)) {
            const peertubeLicence = constants_1.VIDEO_LICENCES[key];
            if (peertubeLicence.toLowerCase() === licence.toLowerCase())
                return parseInt(key, 10);
        }
        return undefined;
    }
    getCategory(categories) {
        if (!categories)
            return undefined;
        const categoryString = categories[0];
        if (!categoryString || typeof categoryString !== 'string')
            return undefined;
        if (categoryString === 'News & Politics')
            return 11;
        for (const key of Object.keys(constants_1.VIDEO_CATEGORIES)) {
            const category = constants_1.VIDEO_CATEGORIES[key];
            if (categoryString.toLowerCase() === category.toLowerCase())
                return parseInt(key, 10);
        }
        return undefined;
    }
    getLanguage(language) {
        return constants_1.VIDEO_LANGUAGES[language] ? language : undefined;
    }
}
exports.YoutubeDLInfoBuilder = YoutubeDLInfoBuilder;
//# sourceMappingURL=youtube-dl-info-builder.js.map