"use strict";
var VideoChannelSyncModel_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideoChannelSyncModel = void 0;
const tslib_1 = require("tslib");
const sequelize_1 = require("sequelize");
const sequelize_typescript_1 = require("sequelize-typescript");
const misc_1 = require("../../helpers/custom-validators/activitypub/misc");
const video_channel_syncs_1 = require("../../helpers/custom-validators/video-channel-syncs");
const constants_1 = require("../../initializers/constants");
const account_1 = require("../account/account");
const user_1 = require("../user/user");
const utils_1 = require("../utils");
const video_channel_1 = require("./video-channel");
let VideoChannelSyncModel = VideoChannelSyncModel_1 = class VideoChannelSyncModel extends sequelize_typescript_1.Model {
    static listByAccountForAPI(options) {
        const getQuery = (forCount) => {
            const videoChannelModel = forCount
                ? video_channel_1.VideoChannelModel.unscoped()
                : video_channel_1.VideoChannelModel;
            return {
                offset: options.start,
                limit: options.count,
                order: (0, utils_1.getChannelSyncSort)(options.sort),
                include: [
                    {
                        model: videoChannelModel,
                        required: true,
                        where: {
                            accountId: options.accountId
                        }
                    }
                ]
            };
        };
        return Promise.all([
            VideoChannelSyncModel_1.unscoped().count(getQuery(true)),
            VideoChannelSyncModel_1.unscoped().findAll(getQuery(false))
        ]).then(([total, data]) => ({ total, data }));
    }
    static countByAccount(accountId) {
        const query = {
            include: [
                {
                    model: video_channel_1.VideoChannelModel.unscoped(),
                    required: true,
                    where: {
                        accountId
                    }
                }
            ]
        };
        return VideoChannelSyncModel_1.unscoped().count(query);
    }
    static loadWithChannel(id) {
        return VideoChannelSyncModel_1.findByPk(id);
    }
    static listSyncs() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const query = {
                include: [
                    {
                        model: video_channel_1.VideoChannelModel.unscoped(),
                        required: true,
                        include: [
                            {
                                model: account_1.AccountModel.unscoped(),
                                required: true,
                                include: [{
                                        attributes: [],
                                        model: user_1.UserModel.unscoped(),
                                        required: true,
                                        where: {
                                            videoQuota: {
                                                [sequelize_1.Op.ne]: 0
                                            },
                                            videoQuotaDaily: {
                                                [sequelize_1.Op.ne]: 0
                                            }
                                        }
                                    }]
                            }
                        ]
                    }
                ]
            };
            return VideoChannelSyncModel_1.unscoped().findAll(query);
        });
    }
    toFormattedJSON() {
        var _a;
        return {
            id: this.id,
            state: {
                id: this.state,
                label: constants_1.VIDEO_CHANNEL_SYNC_STATE[this.state]
            },
            externalChannelUrl: this.externalChannelUrl,
            createdAt: this.createdAt.toISOString(),
            channel: this.VideoChannel.toFormattedSummaryJSON(),
            lastSyncAt: (_a = this.lastSyncAt) === null || _a === void 0 ? void 0 : _a.toISOString()
        };
    }
};
tslib_1.__decorate([
    (0, sequelize_typescript_1.AllowNull)(false),
    (0, sequelize_typescript_1.Default)(null),
    (0, sequelize_typescript_1.Is)('VideoChannelExternalChannelUrl', value => (0, utils_1.throwIfNotValid)(value, misc_1.isUrlValid, 'externalChannelUrl', true)),
    (0, sequelize_typescript_1.Column)(sequelize_typescript_1.DataType.STRING(constants_1.CONSTRAINTS_FIELDS.VIDEO_CHANNEL_SYNCS.EXTERNAL_CHANNEL_URL.max)),
    tslib_1.__metadata("design:type", String)
], VideoChannelSyncModel.prototype, "externalChannelUrl", void 0);
tslib_1.__decorate([
    sequelize_typescript_1.CreatedAt,
    tslib_1.__metadata("design:type", Date)
], VideoChannelSyncModel.prototype, "createdAt", void 0);
tslib_1.__decorate([
    sequelize_typescript_1.UpdatedAt,
    tslib_1.__metadata("design:type", Date)
], VideoChannelSyncModel.prototype, "updatedAt", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.ForeignKey)(() => video_channel_1.VideoChannelModel),
    sequelize_typescript_1.Column,
    tslib_1.__metadata("design:type", Number)
], VideoChannelSyncModel.prototype, "videoChannelId", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.BelongsTo)(() => video_channel_1.VideoChannelModel, {
        foreignKey: {
            allowNull: false
        },
        onDelete: 'cascade'
    }),
    tslib_1.__metadata("design:type", video_channel_1.VideoChannelModel)
], VideoChannelSyncModel.prototype, "VideoChannel", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.AllowNull)(false),
    (0, sequelize_typescript_1.Default)(1),
    (0, sequelize_typescript_1.Is)('VideoChannelSyncState', value => (0, utils_1.throwIfNotValid)(value, video_channel_syncs_1.isVideoChannelSyncStateValid, 'state')),
    sequelize_typescript_1.Column,
    tslib_1.__metadata("design:type", Number)
], VideoChannelSyncModel.prototype, "state", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.AllowNull)(true),
    (0, sequelize_typescript_1.Column)(sequelize_typescript_1.DataType.DATE),
    tslib_1.__metadata("design:type", Date)
], VideoChannelSyncModel.prototype, "lastSyncAt", void 0);
VideoChannelSyncModel = VideoChannelSyncModel_1 = tslib_1.__decorate([
    (0, sequelize_typescript_1.DefaultScope)(() => ({
        include: [
            {
                model: video_channel_1.VideoChannelModel,
                required: true
            }
        ]
    })),
    (0, sequelize_typescript_1.Table)({
        tableName: 'videoChannelSync',
        indexes: [
            {
                fields: ['videoChannelId']
            }
        ]
    })
], VideoChannelSyncModel);
exports.VideoChannelSyncModel = VideoChannelSyncModel;
//# sourceMappingURL=video-channel-sync.js.map