"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.DashboardService = void 0;
const api_locale_1 = require("@hed-hog/api-locale");
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 DashboardService = class DashboardService {
    constructor(prismaService, paginationService) {
        this.prismaService = prismaService;
        this.paginationService = paginationService;
    }
    async getAllDashboards(paginationParams) {
        const fields = ['slug'];
        const OR = this.prismaService.createInsensitiveSearch(fields, paginationParams);
        if (paginationParams.search) {
            OR.push({
                dashboard_locale: {
                    some: {
                        name: {
                            contains: paginationParams.search,
                            mode: 'insensitive',
                        },
                    },
                },
            });
        }
        return this.paginationService.paginate(this.prismaService.dashboard, paginationParams, {
            include: {
                dashboard_locale: {
                    include: {
                        locale: true,
                    },
                },
                dashboard_role: {
                    include: {
                        role: {
                            include: {
                                role_locale: {
                                    include: {
                                        locale: true,
                                    },
                                },
                            },
                        },
                    },
                },
            },
            where: {
                OR
            },
            orderBy: {
                id: 'desc',
            },
        }, 'dashboard');
    }
    async getDashboard(id, locale) {
        const dashboard = await this.prismaService.dashboard.findUnique({
            where: { id },
            include: {
                dashboard_locale: {
                    include: {
                        locale: true,
                    },
                },
                dashboard_role: {
                    include: {
                        role: {
                            include: {
                                role_locale: {
                                    include: {
                                        locale: true,
                                    },
                                },
                            },
                        },
                    },
                },
                dashboard_item: {
                    include: {
                        dashboard_component: {
                            include: {
                                dashboard_component_locale: {
                                    include: {
                                        locale: true,
                                    },
                                },
                            },
                        },
                    },
                },
            },
        });
        if (!dashboard) {
            throw new common_1.NotFoundException((0, api_locale_1.getLocaleText)('dashboardNotFound', locale, 'Dashboard not found'));
        }
        return dashboard;
    }
    async createDashboard(data, locale) {
        const dashboard = await this.prismaService.dashboard.create({
            data: {
                slug: data.slug,
            },
        });
        if (data.locale) {
            for (const [localeCode, localeData] of Object.entries(data.locale)) {
                const localeRecord = await this.prismaService.locale.findFirst({
                    where: { code: localeCode },
                });
                if (localeRecord) {
                    await this.prismaService.dashboard_locale.create({
                        data: {
                            dashboard_id: dashboard.id,
                            locale_id: localeRecord.id,
                            name: localeData.name,
                        },
                    });
                }
            }
        }
        return this.getDashboard(dashboard.id, locale);
    }
    async updateDashboard(id, data, locale) {
        await this.prismaService.dashboard.update({
            where: { id },
            data: {
                slug: data.slug,
            },
        });
        if (data.locale) {
            await this.prismaService.dashboard_locale.deleteMany({
                where: { dashboard_id: id },
            });
            for (const [localeCode, localeData] of Object.entries(data.locale)) {
                const localeRecord = await this.prismaService.locale.findFirst({
                    where: { code: localeCode },
                });
                if (localeRecord) {
                    await this.prismaService.dashboard_locale.create({
                        data: {
                            dashboard_id: id,
                            locale_id: localeRecord.id,
                            name: localeData.name,
                        },
                    });
                }
            }
        }
        return this.getDashboard(id, locale);
    }
    async deleteDashboard(id, locale) {
        const dashboard = await this.prismaService.dashboard.findUnique({
            where: { id },
        });
        if (!dashboard) {
            throw new common_1.NotFoundException((0, api_locale_1.getLocaleText)('dashboardNotFound', locale, 'Dashboard not found'));
        }
        await this.prismaService.dashboard.delete({
            where: { id },
        });
        return { success: true };
    }
};
exports.DashboardService = DashboardService;
exports.DashboardService = DashboardService = __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])
], DashboardService);
//# sourceMappingURL=dashboard.service.js.map