"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BoardFavoriteQuery = void 0;
const type_graphql_1 = require("type-graphql");
const typeorm_1 = require("typeorm");
const shell_1 = require("@things-factory/shell");
const board_service_1 = require("@things-factory/board-service");
const fav_base_1 = require("@things-factory/fav-base");
const board_favorite_type_1 = require("./board-favorite-type");
const env_1 = require("@things-factory/env");
const ORMCONFIG = env_1.config.get('ormconfig', {});
const DATABASE_TYPE = ORMCONFIG.type;
let BoardFavoriteQuery = class BoardFavoriteQuery {
    async favoriteBoards(params, context) {
        const { user } = context.state;
        const queryBuilder = (0, typeorm_1.getRepository)(board_service_1.Board).createQueryBuilder();
        (0, shell_1.buildQuery)(queryBuilder, params, context);
        var joinStatement = '';
        switch (DATABASE_TYPE) {
            case 'postgres':
                joinStatement = 'CAST(Board.id as VARCHAR) = Favorite.routing';
                break;
            case 'sqlite':
            default:
                joinStatement = 'Board.id = Favorite.routing';
        }
        var qb = queryBuilder
            .innerJoin(fav_base_1.Favorite, 'Favorite', joinStatement)
            .select([
            'Board.id as id',
            'Board.name as name',
            'Board.description as description',
            'Board.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 };
    }
};
__decorate([
    (0, type_graphql_1.Query)(returns => board_favorite_type_1.BoardFavoriteList, { description: 'To fetch multiple BoardFavorites' }),
    __param(0, (0, type_graphql_1.Args)()),
    __param(1, (0, type_graphql_1.Ctx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [typeof (_a = typeof shell_1.ListParam !== "undefined" && shell_1.ListParam) === "function" ? _a : Object, Object]),
    __metadata("design:returntype", Promise)
], BoardFavoriteQuery.prototype, "favoriteBoards", null);
BoardFavoriteQuery = __decorate([
    (0, type_graphql_1.Resolver)(board_favorite_type_1.BoardFavorite)
], BoardFavoriteQuery);
exports.BoardFavoriteQuery = BoardFavoriteQuery;
//# sourceMappingURL=board-favorite-query.js.map