import { stringifyWorkflowPrListWhere } from '../../../core/models/workflow-types.js';
import { fetchOpenPrList } from './system-git-context.js';
import { getCachedCandidateSnapshot, readPreviousSelectedNumber, selectNextCandidate, } from './system-selection-helpers.js';
function matchesSimpleWildcard(value, pattern) {
    const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*');
    return new RegExp(`^${escaped}$`).test(value);
}
function matchesPrWhere(pr, where) {
    if (where?.author !== undefined && pr.author !== where.author) {
        return false;
    }
    if (where?.base_branch !== undefined && pr.base_branch !== where.base_branch) {
        return false;
    }
    if (where?.head_branch !== undefined && !matchesSimpleWildcard(pr.head_branch, where.head_branch)) {
        return false;
    }
    if (where?.managed_by_takt !== undefined && pr.managed_by_takt !== where.managed_by_takt) {
        return false;
    }
    if (where?.labels !== undefined && !where.labels.every((label) => pr.labels.includes(label))) {
        return false;
    }
    if (where?.same_repository !== undefined && pr.same_repository !== where.same_repository) {
        return false;
    }
    if (where?.draft !== undefined && pr.draft !== where.draft) {
        return false;
    }
    return true;
}
function listMatchingPrs(projectCwd, where) {
    const openPrs = fetchOpenPrList(projectCwd);
    const filtered = openPrs.filter((pr) => matchesPrWhere(pr, where));
    filtered.sort((left, right) => right.updated_at.localeCompare(left.updated_at));
    return filtered;
}
function getPrCandidateSnapshot(projectCwd, where, resolutionContext) {
    const cacheKey = `pr_candidates:${stringifyWorkflowPrListWhere(where)}`;
    return getCachedCandidateSnapshot(cacheKey, () => listMatchingPrs(projectCwd, where), resolutionContext);
}
function toPrSummary({ number, author, base_branch, head_branch, managed_by_takt, labels, same_repository, draft, }) {
    return {
        number,
        author,
        base_branch,
        head_branch,
        managed_by_takt,
        labels,
        same_repository,
        draft,
    };
}
export function resolvePrListInput(input, projectCwd, resolutionContext) {
    return getPrCandidateSnapshot(projectCwd, input.where, resolutionContext).map(toPrSummary);
}
export function resolvePrSelectionInput(input, projectCwd, state, stepName, resolutionContext) {
    if (!state) {
        throw new Error('pr_selection requires workflow state');
    }
    if (!stepName) {
        throw new Error('pr_selection requires step name');
    }
    const candidates = getPrCandidateSnapshot(projectCwd, input.where, resolutionContext);
    const selectedPr = selectNextCandidate(candidates, readPreviousSelectedNumber(state, stepName, input.as));
    if (!selectedPr) {
        return { exists: false };
    }
    return {
        exists: true,
        ...toPrSummary(selectedPr),
    };
}
//# sourceMappingURL=system-pr-input-resolver.js.map