/**
 * Unarchive Feature Use Case
 *
 * Restores a feature from Archived to its previousLifecycle value,
 * then clears previousLifecycle.
 *
 * Business Rules:
 * - Only features in the Archived state can be unarchived
 * - previousLifecycle must be non-null for restoration
 * - The lifecycle is restored to the pre-archive state
 * - previousLifecycle is cleared after restoration
 */
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 { SdlcLifecycle } from '../../../domain/generated/output.js';
let UnarchiveFeatureUseCase = class UnarchiveFeatureUseCase {
    featureRepo;
    constructor(featureRepo) {
        this.featureRepo = featureRepo;
    }
    async execute(featureId) {
        // 1. Find feature (exact or prefix match)
        const feature = (await this.featureRepo.findById(featureId)) ??
            (await this.featureRepo.findByIdPrefix(featureId));
        if (!feature) {
            throw new Error(`Feature not found: "${featureId}"`);
        }
        // 2. Validate lifecycle is Archived
        if (feature.lifecycle !== SdlcLifecycle.Archived) {
            throw new Error(`Cannot unarchive feature "${feature.name}": lifecycle is ${feature.lifecycle}. ` +
                `Only archived features can be unarchived.`);
        }
        // 3. Validate previousLifecycle exists
        if (!feature.previousLifecycle) {
            throw new Error(`Cannot unarchive feature "${feature.name}": no previous lifecycle state recorded. ` +
                `The feature may have been archived before this field was introduced.`);
        }
        // 4. Restore lifecycle and clear previousLifecycle
        const updatedFeature = {
            ...feature,
            lifecycle: feature.previousLifecycle,
            previousLifecycle: undefined,
            updatedAt: new Date(),
        };
        await this.featureRepo.update(updatedFeature);
        return updatedFeature;
    }
};
UnarchiveFeatureUseCase = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __metadata("design:paramtypes", [Object])
], UnarchiveFeatureUseCase);
export { UnarchiveFeatureUseCase };
