"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigCommand = void 0;
const tslib_1 = require("tslib");
const lodash_1 = require("lodash");
const abstract_command_1 = require("../shared/abstract-command");
class ConfigCommand extends abstract_command_1.AbstractCommand {
    static getCustomConfigResolutions(enabled) {
        return {
            '144p': enabled,
            '240p': enabled,
            '360p': enabled,
            '480p': enabled,
            '720p': enabled,
            '1080p': enabled,
            '1440p': enabled,
            '2160p': enabled
        };
    }
    disableImports() {
        return this.setImportsEnabled(false);
    }
    enableImports() {
        return this.setImportsEnabled(true);
    }
    setImportsEnabled(enabled) {
        return this.updateExistingSubConfig({
            newConfig: {
                import: {
                    videos: {
                        http: {
                            enabled
                        },
                        torrent: {
                            enabled
                        }
                    }
                }
            }
        });
    }
    setChannelSyncEnabled(enabled) {
        return this.updateExistingSubConfig({
            newConfig: {
                import: {
                    videoChannelSynchronization: {
                        enabled
                    }
                }
            }
        });
    }
    enableChannelSync() {
        return this.setChannelSyncEnabled(true);
    }
    disableChannelSync() {
        return this.setChannelSyncEnabled(false);
    }
    enableLive(options = {}) {
        const { allowReplay, transcoding, resolutions = 'max' } = options;
        return this.updateExistingSubConfig({
            newConfig: {
                live: {
                    enabled: true,
                    allowReplay: allowReplay !== null && allowReplay !== void 0 ? allowReplay : true,
                    transcoding: {
                        enabled: transcoding !== null && transcoding !== void 0 ? transcoding : true,
                        resolutions: ConfigCommand.getCustomConfigResolutions(resolutions === 'max')
                    }
                }
            }
        });
    }
    disableTranscoding() {
        return this.updateExistingSubConfig({
            newConfig: {
                transcoding: {
                    enabled: false
                },
                videoStudio: {
                    enabled: false
                }
            }
        });
    }
    enableTranscoding(webtorrent = true, hls = true) {
        return this.updateExistingSubConfig({
            newConfig: {
                transcoding: {
                    enabled: true,
                    allowAudioFiles: true,
                    allowAdditionalExtensions: true,
                    resolutions: ConfigCommand.getCustomConfigResolutions(true),
                    webtorrent: {
                        enabled: webtorrent
                    },
                    hls: {
                        enabled: hls
                    }
                }
            }
        });
    }
    enableMinimumTranscoding(webtorrent = true, hls = true) {
        return this.updateExistingSubConfig({
            newConfig: {
                transcoding: {
                    enabled: true,
                    resolutions: Object.assign(Object.assign({}, ConfigCommand.getCustomConfigResolutions(false)), { '240p': true }),
                    webtorrent: {
                        enabled: webtorrent
                    },
                    hls: {
                        enabled: hls
                    }
                }
            }
        });
    }
    enableStudio() {
        return this.updateExistingSubConfig({
            newConfig: {
                videoStudio: {
                    enabled: true
                }
            }
        });
    }
    getConfig(options = {}) {
        const path = '/api/v1/config';
        return this.getRequestBody(Object.assign(Object.assign({}, options), { path, implicitToken: false, defaultExpectedStatus: 200 }));
    }
    getIndexHTMLConfig(options = {}) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const text = yield this.getRequestText(Object.assign(Object.assign({}, options), { path: '/', implicitToken: false, defaultExpectedStatus: 200 }));
            const match = text.match('<script type="application/javascript">window.PeerTubeServerConfig = (".+?")</script>');
            return JSON.parse(JSON.parse(match[1]));
        });
    }
    getAbout(options = {}) {
        const path = '/api/v1/config/about';
        return this.getRequestBody(Object.assign(Object.assign({}, options), { path, implicitToken: false, defaultExpectedStatus: 200 }));
    }
    getCustomConfig(options = {}) {
        const path = '/api/v1/config/custom';
        return this.getRequestBody(Object.assign(Object.assign({}, options), { path, implicitToken: true, defaultExpectedStatus: 200 }));
    }
    updateCustomConfig(options) {
        const path = '/api/v1/config/custom';
        return this.putBodyRequest(Object.assign(Object.assign({}, options), { path, fields: options.newCustomConfig, implicitToken: true, defaultExpectedStatus: 200 }));
    }
    deleteCustomConfig(options = {}) {
        const path = '/api/v1/config/custom';
        return this.deleteRequest(Object.assign(Object.assign({}, options), { path, implicitToken: true, defaultExpectedStatus: 200 }));
    }
    updateExistingSubConfig(options) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const existing = yield this.getCustomConfig(Object.assign(Object.assign({}, options), { expectedStatus: 200 }));
            return this.updateCustomConfig(Object.assign(Object.assign({}, options), { newCustomConfig: (0, lodash_1.merge)({}, existing, options.newConfig) }));
        });
    }
    updateCustomSubConfig(options) {
        const newCustomConfig = {
            instance: {
                name: 'PeerTube updated',
                shortDescription: 'my short description',
                description: 'my super description',
                terms: 'my super terms',
                codeOfConduct: 'my super coc',
                creationReason: 'my super creation reason',
                moderationInformation: 'my super moderation information',
                administrator: 'Kuja',
                maintenanceLifetime: 'forever',
                businessModel: 'my super business model',
                hardwareInformation: '2vCore 3GB RAM',
                languages: ['en', 'es'],
                categories: [1, 2],
                isNSFW: true,
                defaultNSFWPolicy: 'blur',
                defaultClientRoute: '/videos/recently-added',
                customizations: {
                    javascript: 'alert("coucou")',
                    css: 'body { background-color: red; }'
                }
            },
            theme: {
                default: 'default'
            },
            services: {
                twitter: {
                    username: '@MySuperUsername',
                    whitelisted: true
                }
            },
            client: {
                videos: {
                    miniature: {
                        preferAuthorDisplayName: false
                    }
                },
                menu: {
                    login: {
                        redirectOnSingleExternalAuth: false
                    }
                }
            },
            cache: {
                previews: {
                    size: 2
                },
                captions: {
                    size: 3
                },
                torrents: {
                    size: 4
                }
            },
            signup: {
                enabled: false,
                limit: 5,
                requiresEmailVerification: false,
                minimumAge: 16
            },
            admin: {
                email: 'superadmin1@example.com'
            },
            contactForm: {
                enabled: true
            },
            user: {
                videoQuota: 5242881,
                videoQuotaDaily: 318742
            },
            videoChannels: {
                maxPerUser: 20
            },
            transcoding: {
                enabled: true,
                allowAdditionalExtensions: true,
                allowAudioFiles: true,
                threads: 1,
                concurrency: 3,
                profile: 'default',
                resolutions: {
                    '0p': false,
                    '144p': false,
                    '240p': false,
                    '360p': true,
                    '480p': true,
                    '720p': false,
                    '1080p': false,
                    '1440p': false,
                    '2160p': false
                },
                alwaysTranscodeOriginalResolution: true,
                webtorrent: {
                    enabled: true
                },
                hls: {
                    enabled: false
                }
            },
            live: {
                enabled: true,
                allowReplay: false,
                latencySetting: {
                    enabled: false
                },
                maxDuration: -1,
                maxInstanceLives: -1,
                maxUserLives: 50,
                transcoding: {
                    enabled: true,
                    threads: 4,
                    profile: 'default',
                    resolutions: {
                        '144p': true,
                        '240p': true,
                        '360p': true,
                        '480p': true,
                        '720p': true,
                        '1080p': true,
                        '1440p': true,
                        '2160p': true
                    },
                    alwaysTranscodeOriginalResolution: true
                }
            },
            videoStudio: {
                enabled: false
            },
            import: {
                videos: {
                    concurrency: 3,
                    http: {
                        enabled: false
                    },
                    torrent: {
                        enabled: false
                    }
                },
                videoChannelSynchronization: {
                    enabled: false,
                    maxPerUser: 10
                }
            },
            trending: {
                videos: {
                    algorithms: {
                        enabled: ['hot', 'most-viewed', 'most-liked'],
                        default: 'hot'
                    }
                }
            },
            autoBlacklist: {
                videos: {
                    ofUsers: {
                        enabled: false
                    }
                }
            },
            followers: {
                instance: {
                    enabled: true,
                    manualApproval: false
                }
            },
            followings: {
                instance: {
                    autoFollowBack: {
                        enabled: false
                    },
                    autoFollowIndex: {
                        indexUrl: 'https://instances.joinpeertube.org/api/v1/instances/hosts',
                        enabled: false
                    }
                }
            },
            broadcastMessage: {
                enabled: true,
                level: 'warning',
                message: 'hello',
                dismissable: true
            },
            search: {
                remoteUri: {
                    users: true,
                    anonymous: true
                },
                searchIndex: {
                    enabled: true,
                    url: 'https://search.joinpeertube.org',
                    disableLocalSearch: true,
                    isDefaultSearch: true
                }
            }
        };
        (0, lodash_1.merge)(newCustomConfig, options.newConfig);
        return this.updateCustomConfig(Object.assign(Object.assign({}, options), { newCustomConfig }));
    }
}
exports.ConfigCommand = ConfigCommand;
//# sourceMappingURL=config-command.js.map