"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); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScreenService = void 0;
const api_pagination_1 = require("@hed-hog/api-pagination");
const api_prisma_1 = require("@hed-hog/api-prisma");
const common_1 = require("@nestjs/common");
let ScreenService = class ScreenService {
    constructor(prismaService, paginationService) {
        this.prismaService = prismaService;
        this.paginationService = paginationService;
    }
    async updateRoles(screenId, data) {
        await this.prismaService.role_screen.deleteMany({
            where: {
                screen_id: screenId,
            },
        });
        return this.prismaService.role_screen.createMany({
            data: data.ids.map((roleId) => ({
                screen_id: screenId,
                role_id: roleId,
            })),
            skipDuplicates: true,
        });
    }
    async updateRoutes(screenId, { ids }) {
        ids = (await this.prismaService.route.findMany({
            where: {
                id: {
                    in: ids,
                },
            },
            select: {
                id: true,
            },
        })).map((route) => route === null || route === void 0 ? void 0 : route.id);
        await this.prismaService.route_screen.deleteMany({
            where: {
                screen_id: screenId,
            },
        });
        return this.prismaService.route_screen.createMany({
            data: ids.map((routeId) => ({
                screen_id: screenId,
                route_id: routeId,
            })),
            skipDuplicates: true,
        });
    }
    async listRoutes(screenId, paginationParams) {
        return this.paginationService.paginate(this.prismaService.route, paginationParams, {
            include: {
                route_screen: {
                    where: {
                        screen_id: screenId,
                    },
                    select: {
                        route_id: true,
                        screen_id: true,
                    },
                },
            },
        });
    }
    async listRoles(locale, screenId, paginationParams) {
        return this.paginationService.paginate(this.prismaService.role, paginationParams, {
            include: {
                role_screen: {
                    where: {
                        screen_id: screenId,
                    },
                    select: {
                        role_id: true,
                        screen_id: true,
                    },
                },
            },
        });
    }
    async list(locale, paginationParams) {
        const fields = ['slug', 'icon'];
        const OR = this.prismaService.createInsensitiveSearch(fields, paginationParams);
        const result = await this.paginationService.paginate(this.prismaService.screen, paginationParams, {
            where: {
                OR,
            },
            include: {
                screen_locale: {
                    where: {
                        locale: {
                            code: locale,
                        },
                    },
                    select: {
                        name: true,
                        description: true,
                    },
                },
            },
        }, 'screen_locale');
        return result;
    }
    async get(screenId) {
        return this.prismaService.screen.findUnique({ where: { id: screenId } });
    }
    async create({ slug, icon }) {
        return this.prismaService.screen.create({
            data: {
                slug,
                icon,
            },
        });
    }
    async update({ id, data }) {
        return this.prismaService.screen.update({
            where: { id },
            data,
        });
    }
    async delete({ ids }) {
        if (ids == undefined || ids == null) {
            throw new common_1.BadRequestException(`You must select at least one screen to delete.`);
        }
        return this.prismaService.screen.deleteMany({
            where: {
                id: {
                    in: ids,
                },
            },
        });
    }
};
exports.ScreenService = ScreenService;
exports.ScreenService = ScreenService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, common_1.Inject)((0, common_1.forwardRef)(() => api_prisma_1.PrismaService))),
    __param(1, (0, common_1.Inject)((0, common_1.forwardRef)(() => api_pagination_1.PaginationService))),
    __metadata("design:paramtypes", [api_prisma_1.PrismaService,
        api_pagination_1.PaginationService])
], ScreenService);
//# sourceMappingURL=screen.service.js.map