"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntegrationEventCatalogService = 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 IntegrationEventCatalogService = class IntegrationEventCatalogService {
    constructor(prisma, pagination) {
        this.prisma = prisma;
        this.pagination = pagination;
    }
    get delegate() {
        return this.prisma.integration_event_catalog;
    }
    async list(paginationParams) {
        const fields = ['slug', 'name', 'module', 'description', 'status'];
        const OR = this.prisma.createInsensitiveSearch(fields, paginationParams);
        return this.pagination.paginate(this.delegate, paginationParams, {
            where: OR.length > 0 ? { OR } : {},
            orderBy: [{ module: 'asc' }, { slug: 'asc' }],
        });
    }
    async options() {
        return this.delegate.findMany({
            where: { status: 'active' },
            orderBy: [{ module: 'asc' }, { slug: 'asc' }],
            select: {
                id: true,
                slug: true,
                name: true,
                module: true,
                description: true,
            },
        });
    }
    async get(id) {
        const record = await this.delegate.findUnique({ where: { id } });
        if (!record) {
            throw new common_1.NotFoundException(`Event catalog item with id ${id} not found.`);
        }
        return record;
    }
    async create(data) {
        await this.ensureSlugAvailable(data.slug);
        return this.delegate.create({
            data: this.normalize(data),
        });
    }
    async update(id, data) {
        const existing = await this.get(id);
        if (data.slug && data.slug !== existing.slug) {
            await this.ensureSlugAvailable(data.slug);
        }
        return this.delegate.update({
            where: { id },
            data: this.normalize(data, true),
        });
    }
    async delete(data) {
        var _a;
        if (!((_a = data.ids) === null || _a === void 0 ? void 0 : _a.length)) {
            throw new common_1.BadRequestException('You must select at least one item to delete.');
        }
        return this.delegate.deleteMany({
            where: { id: { in: data.ids } },
        });
    }
    async ensureSlugAvailable(slug) {
        const existing = await this.delegate.findUnique({
            where: { slug: String(slug || '').trim() },
            select: { id: true },
        });
        if (existing) {
            throw new common_1.BadRequestException(`Event "${slug}" already exists.`);
        }
    }
    normalize(data, partial = false) {
        var _a, _b, _c;
        const normalized = {};
        const assign = (key, value) => {
            if (!partial || value !== undefined) {
                normalized[key] = value;
            }
        };
        assign('slug', String((_a = data.slug) !== null && _a !== void 0 ? _a : '').trim());
        assign('name', String((_b = data.name) !== null && _b !== void 0 ? _b : '').trim());
        assign('module', String((_c = data.module) !== null && _c !== void 0 ? _c : '').trim());
        assign('description', data.description === undefined || data.description === null
            ? partial
                ? undefined
                : null
            : String(data.description).trim() || null);
        assign('status', data.status === 'inactive' ? 'inactive' : 'active');
        return normalized;
    }
};
exports.IntegrationEventCatalogService = IntegrationEventCatalogService;
exports.IntegrationEventCatalogService = IntegrationEventCatalogService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [api_prisma_1.PrismaService,
        api_pagination_1.PaginationService])
], IntegrationEventCatalogService);
//# sourceMappingURL=integration-event-catalog.service.js.map