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); }
};
import { injectable, inject } from 'tsyringe';
import { randomUUID } from 'node:crypto';
let BulkUpdateWorkItemsUseCase = class BulkUpdateWorkItemsUseCase {
    workItemRepo;
    activityRepo;
    constructor(workItemRepo, activityRepo) {
        this.workItemRepo = workItemRepo;
        this.activityRepo = activityRepo;
    }
    async execute(input, actorId = 'system') {
        const { workItemIds, operation } = input;
        const succeeded = [];
        const failed = [];
        const now = new Date();
        for (const id of workItemIds) {
            try {
                const workItem = await this.workItemRepo.findById(id);
                if (!workItem) {
                    failed.push({ id, error: 'Work item not found' });
                    continue;
                }
                switch (operation.type) {
                    case 'changeState': {
                        const oldState = workItem.stateId;
                        await this.workItemRepo.update(id, { stateId: operation.stateId });
                        await this.activityRepo.create({
                            id: randomUUID(),
                            workItemId: id,
                            fieldName: 'state',
                            oldValue: oldState,
                            newValue: operation.stateId,
                            actorId,
                            createdAt: now,
                            updatedAt: now,
                        });
                        break;
                    }
                    case 'changePriority': {
                        const oldPriority = workItem.priority;
                        await this.workItemRepo.update(id, {
                            priority: operation.priority,
                        });
                        await this.activityRepo.create({
                            id: randomUUID(),
                            workItemId: id,
                            fieldName: 'priority',
                            oldValue: oldPriority,
                            newValue: operation.priority,
                            actorId,
                            createdAt: now,
                            updatedAt: now,
                        });
                        break;
                    }
                    case 'addLabel':
                        await this.workItemRepo.addLabel(id, operation.labelId);
                        break;
                    case 'removeLabel':
                        await this.workItemRepo.removeLabel(id, operation.labelId);
                        break;
                    case 'addAssignee':
                        await this.workItemRepo.addAssignee(id, operation.assigneeId);
                        break;
                    case 'removeAssignee':
                        await this.workItemRepo.removeAssignee(id, operation.assigneeId);
                        break;
                    case 'delete':
                        await this.workItemRepo.softDelete(id);
                        await this.activityRepo.create({
                            id: randomUUID(),
                            workItemId: id,
                            fieldName: 'deleted',
                            oldValue: workItem.title,
                            actorId,
                            createdAt: now,
                            updatedAt: now,
                        });
                        break;
                }
                succeeded.push(id);
            }
            catch (err) {
                const message = err instanceof Error ? err.message : 'Unknown error';
                failed.push({ id, error: message });
            }
        }
        return { ok: failed.length === 0, succeeded, failed };
    }
};
BulkUpdateWorkItemsUseCase = __decorate([
    injectable(),
    __param(0, inject('IWorkItemRepository')),
    __param(1, inject('IActivityLogRepository')),
    __metadata("design:paramtypes", [Object, Object])
], BulkUpdateWorkItemsUseCase);
export { BulkUpdateWorkItemsUseCase };
