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';
let TransferCycleItemsUseCase = class TransferCycleItemsUseCase {
    cycleRepo;
    workItemRepo;
    stateRepo;
    constructor(cycleRepo, workItemRepo, stateRepo) {
        this.cycleRepo = cycleRepo;
        this.workItemRepo = workItemRepo;
        this.stateRepo = stateRepo;
    }
    async execute(input) {
        const sourceCycle = await this.cycleRepo.findById(input.sourceCycleId);
        if (!sourceCycle) {
            return { ok: false, error: `Source cycle not found: "${input.sourceCycleId}"` };
        }
        if (input.targetCycleId) {
            const targetCycle = await this.cycleRepo.findById(input.targetCycleId);
            if (!targetCycle) {
                return { ok: false, error: `Target cycle not found: "${input.targetCycleId}"` };
            }
            if (targetCycle.projectId !== sourceCycle.projectId) {
                return { ok: false, error: 'Source and target cycles must belong to the same project.' };
            }
        }
        const workItemIds = await this.cycleRepo.getWorkItemIds(input.sourceCycleId);
        const states = await this.stateRepo.listByProject(sourceCycle.projectId);
        const completedStateIds = new Set(states
            .filter((s) => s.stateGroup === 'Completed' || s.stateGroup === 'Cancelled')
            .map((s) => s.id));
        let transferred = 0;
        let kept = 0;
        for (const itemId of workItemIds) {
            const workItem = await this.workItemRepo.findById(itemId);
            if (!workItem)
                continue;
            if (completedStateIds.has(workItem.stateId)) {
                kept++;
                continue;
            }
            await this.cycleRepo.removeWorkItem(input.sourceCycleId, itemId);
            if (input.targetCycleId) {
                await this.cycleRepo.addWorkItem(input.targetCycleId, itemId);
            }
            transferred++;
        }
        return { ok: true, transferred, kept };
    }
};
TransferCycleItemsUseCase = __decorate([
    injectable(),
    __param(0, inject('ICycleRepository')),
    __param(1, inject('IWorkItemRepository')),
    __param(2, inject('IWorkItemStateRepository')),
    __metadata("design:paramtypes", [Object, Object, Object])
], TransferCycleItemsUseCase);
export { TransferCycleItemsUseCase };
