"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessFavoriteQuery = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const process_1 = require("../process/process");
const env_1 = require("@things-factory/env");
const fav_base_1 = require("@things-factory/fav-base");
const shell_1 = require("@things-factory/shell");
const process_favorite_type_1 = require("./process-favorite-type");
const ORMCONFIG = env_1.config.get('ormconfig', {});
const DATABASE_TYPE = ORMCONFIG.type;
let ProcessFavoriteQuery = class ProcessFavoriteQuery {
    async favoriteProcesses(params, context) {
        const { user } = context.state;
        const queryBuilder = (0, shell_1.getRepository)(process_1.Process).createQueryBuilder();
        (0, shell_1.buildQuery)(queryBuilder, params, context);
        var joinStatement = '';
        switch (DATABASE_TYPE) {
            case 'postgres':
                joinStatement = 'CAST(Process.id as VARCHAR) = Favorite.routing';
                break;
            case 'sqlite':
            default:
                joinStatement = 'Process.id = Favorite.routing';
        }
        var qb = queryBuilder
            .innerJoin(fav_base_1.Favorite, 'Favorite', joinStatement)
            .select([
            'Process.id as id',
            'Process.name as name',
            'Process.description as description',
            'Process.thumbnail as thumbnail',
            'Favorite.id as favoriteId'
        ])
            .andWhere('Favorite.user_id = :userId', {
            userId: user.id
        });
        const items = await qb.getRawMany();
        const total = await qb.getCount();
        return { items, total };
    }
};
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => process_favorite_type_1.ProcessFavoriteList, { description: 'To fetch multiple ProcessFavorites' }),
    tslib_1.__param(0, (0, type_graphql_1.Args)()),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [shell_1.ListParam, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessFavoriteQuery.prototype, "favoriteProcesses", null);
ProcessFavoriteQuery = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(process_favorite_type_1.ProcessFavorite)
], ProcessFavoriteQuery);
exports.ProcessFavoriteQuery = ProcessFavoriteQuery;
//# sourceMappingURL=process-favorite-query.js.map