/**
 * DetectStaleGoodFirstIssueUseCase — spec 097, FR-29.
 *
 * Returns the structured list of `good-first-issue`-labeled issues whose
 * last activity is older than the configured threshold. Pure: no side-
 * effects. Caller (CLI command, scheduler tick, web route) decides what
 * to do with the result (notify, surface in dashboard, auto-close, …).
 */
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 { inject, injectable } from 'tsyringe';
const DEFAULT_STALE_DAYS = 30;
const MS_PER_DAY = 24 * 60 * 60 * 1000;
/**
 * Canonical label queried when looking for stale newcomer-friendly issues.
 */
export const GOOD_FIRST_ISSUE_LABEL = 'good-first-issue';
let DetectStaleGoodFirstIssueUseCase = class DetectStaleGoodFirstIssueUseCase {
    fetcher;
    constructor(fetcher) {
        this.fetcher = fetcher;
    }
    async execute(input) {
        const thresholdDays = resolveThresholdDays(input.staleDays);
        const now = (input.now ?? new Date()).getTime();
        const cutoffMs = now - thresholdDays * MS_PER_DAY;
        const candidates = await this.fetcher.listIssuesByLabel(input.owner, input.repo, GOOD_FIRST_ISSUE_LABEL);
        const stale = candidates
            .filter((issue) => Date.parse(issue.lastActivityAt) <= cutoffMs)
            .map((issue) => toStaleIssue(issue, now));
        return { thresholdDays, stale };
    }
};
DetectStaleGoodFirstIssueUseCase = __decorate([
    injectable(),
    __param(0, inject('IExternalIssueFetcher')),
    __metadata("design:paramtypes", [Object])
], DetectStaleGoodFirstIssueUseCase);
export { DetectStaleGoodFirstIssueUseCase };
function resolveThresholdDays(provided) {
    if (provided === undefined)
        return DEFAULT_STALE_DAYS;
    if (!Number.isFinite(provided) || provided <= 0) {
        throw new Error(`staleDays must be a positive number; got ${provided}`);
    }
    return provided;
}
function toStaleIssue(issue, nowMs) {
    return {
        owner: issue.owner,
        repo: issue.repo,
        issueNumber: issue.issueNumber,
        title: issue.title,
        url: issue.url,
        lastActivityAt: issue.lastActivityAt,
        staleForDays: Math.floor((nowMs - Date.parse(issue.lastActivityAt)) / MS_PER_DAY),
    };
}
