/**
 * Poll Upstream PR Use Case
 *
 * Checks the status of an upstream PR for features in AwaitingUpstream state.
 * When the upstream PR is merged, transitions the feature to Maintain.
 * When closed without merge, updates the PR status but keeps AwaitingUpstream.
 */
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, PrStatus } from '../../../domain/generated/output.js';
let PollUpstreamPrUseCase = class PollUpstreamPrUseCase {
    featureRepo;
    forkService;
    constructor(featureRepo, forkService) {
        this.featureRepo = featureRepo;
        this.forkService = forkService;
    }
    async execute(input) {
        const feature = await this.featureRepo.findById(input.featureId);
        if (!feature) {
            return { status: 'open', transitioned: false };
        }
        if (feature.lifecycle !== SdlcLifecycle.AwaitingUpstream) {
            return { status: 'open', transitioned: false };
        }
        const pr = feature.pr;
        if (!pr?.upstreamPrUrl || !pr?.upstreamPrNumber) {
            return { status: 'open', transitioned: false };
        }
        // Extract upstream repo from the PR URL
        // Format: https://github.com/owner/repo/pull/123
        const urlMatch = pr.upstreamPrUrl.match(/github\.com\/([^/]+\/[^/]+)\/pull/);
        if (!urlMatch) {
            return { status: 'open', transitioned: false };
        }
        const upstreamRepo = urlMatch[1];
        const prStatus = await this.forkService.getUpstreamPrStatus(upstreamRepo, pr.upstreamPrNumber);
        if (prStatus === PrStatus.Merged) {
            // Transition to Maintain
            await this.featureRepo.update({
                ...feature,
                lifecycle: SdlcLifecycle.Maintain,
                pr: {
                    ...pr,
                    upstreamPrStatus: PrStatus.Merged,
                },
                updatedAt: new Date(),
            });
            return { status: 'merged', transitioned: true };
        }
        if (prStatus === PrStatus.Closed) {
            // Keep AwaitingUpstream but update status
            await this.featureRepo.update({
                ...feature,
                pr: {
                    ...pr,
                    upstreamPrStatus: PrStatus.Closed,
                },
                updatedAt: new Date(),
            });
            return { status: 'closed', transitioned: false };
        }
        // Still open — no-op
        return { status: 'open', transitioned: false };
    }
};
PollUpstreamPrUseCase = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __param(1, inject('IGitForkService')),
    __metadata("design:paramtypes", [Object, Object])
], PollUpstreamPrUseCase);
export { PollUpstreamPrUseCase };
