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 UpdateWorkItemUseCase = class UpdateWorkItemUseCase {
    workItemRepo;
    activityRepo;
    constructor(workItemRepo, activityRepo) {
        this.workItemRepo = workItemRepo;
        this.activityRepo = activityRepo;
    }
    async execute(workItemId, input, actorId = 'system') {
        const existing = await this.workItemRepo.findById(workItemId);
        if (!existing) {
            return { ok: false, error: `Work item not found: "${workItemId}"` };
        }
        const fields = {};
        const now = new Date();
        const trackChange = async (fieldName, oldVal, newVal) => {
            if (oldVal !== newVal) {
                await this.activityRepo.create({
                    id: randomUUID(),
                    workItemId,
                    fieldName,
                    oldValue: oldVal != null ? String(oldVal) : undefined,
                    newValue: newVal != null ? String(newVal) : undefined,
                    actorId,
                    createdAt: now,
                    updatedAt: now,
                });
            }
        };
        if (input.title !== undefined) {
            fields.title = input.title;
            await trackChange('title', existing.title, input.title);
        }
        if (input.description !== undefined) {
            fields.description = input.description;
        }
        if (input.stateId !== undefined) {
            fields.stateId = input.stateId;
            await trackChange('state', existing.stateId, input.stateId);
        }
        if (input.priority !== undefined) {
            fields.priority = input.priority;
            await trackChange('priority', existing.priority, input.priority);
        }
        if (input.parentId !== undefined) {
            if (input.parentId) {
                if (input.parentId === workItemId) {
                    return { ok: false, error: 'A work item cannot be its own parent.' };
                }
                const depth = await this.getAncestorDepth(input.parentId);
                if (depth >= 3) {
                    return { ok: false, error: 'Maximum nesting depth of 3 levels exceeded.' };
                }
            }
            fields.parentId = input.parentId;
        }
        if (input.sortOrder !== undefined) {
            fields.sortOrder = input.sortOrder;
        }
        if (input.startDate !== undefined) {
            fields.startDate = input.startDate;
        }
        if (input.dueDate !== undefined) {
            fields.dueDate = input.dueDate;
        }
        if (input.estimateValue !== undefined) {
            fields.estimateValue = input.estimateValue;
            await trackChange('estimate', existing.estimateValue, input.estimateValue);
        }
        if (input.customPropertyValues !== undefined) {
            fields.customPropertyValues = input.customPropertyValues;
        }
        await this.workItemRepo.update(workItemId, fields);
        return { ok: true };
    }
    async getAncestorDepth(parentId) {
        let depth = 0;
        let currentId = parentId;
        while (currentId) {
            depth++;
            if (depth > 3)
                return depth;
            const parent = await this.workItemRepo.findById(currentId);
            currentId = parent?.parentId;
        }
        return depth;
    }
};
UpdateWorkItemUseCase = __decorate([
    injectable(),
    __param(0, inject('IWorkItemRepository')),
    __param(1, inject('IActivityLogRepository')),
    __metadata("design:paramtypes", [Object, Object])
], UpdateWorkItemUseCase);
export { UpdateWorkItemUseCase };
