import { configuredGithubRepo, EXPECTED_GITHUB_SECRETS, expectedLabelsFromConfig, } from "./ui-github-repo-expected.js";
import { defaultGithubRepoGhReads, } from "./ui-github-repo-gh.js";
import { createGithubAuthProbe, } from "./ui-status.js";
export { expectedLabelsFromConfig } from "./ui-github-repo-expected.js";
const GITHUB_REPO_PROBE_ID = "github-repo";
const PROBE_TIMEOUT_MS = 15_000;
const NOT_AUTHENTICATED = "not-authenticated";
const GITHUB_NOT_AUTHENTICATED = "GitHub CLI is not authenticated";
/**
 * Compare expected labels against the remote label set.
 * @param expected - Labels from config
 * @param remote - Labels returned by gh
 * @returns Panel rows with present/missing
 */
function compareLabels(expected, remote) {
    const byName = new Map(remote.map(label => [label.name, label]));
    return expected.map(label => {
        const found = byName.get(label.name);
        return found === undefined
            ? { name: label.name, role: label.role, color: "", present: false }
            : {
                name: label.name,
                role: label.role,
                color: found.color,
                present: true,
            };
    });
}
/**
 * Compare expected secrets against names from `gh secret list`.
 * @param presentNames - Secret names that exist on the repo
 * @returns Presence-only rows (no values)
 */
function compareSecrets(presentNames) {
    const present = new Set(presentNames);
    return EXPECTED_GITHUB_SECRETS.map(secret => ({
        name: secret.name,
        purpose: secret.purpose,
        set: present.has(secret.name),
    }));
}
/**
 * Build the panel value after all gh reads succeed.
 * @param owner - github.org
 * @param repo - github.repo
 * @param settings - Live repository settings
 * @param rulesets - Live rulesets
 * @param labels - Compared label rows
 * @param secrets - Presence-only secret rows
 * @returns Probe value payload
 */
function panelValue(owner, repo, settings, rulesets, labels, secrets) {
    return {
        owner,
        repo,
        settings: { ...settings },
        rulesets: rulesets.map(row => ({ ...row })),
        labels: labels.map(row => ({ ...row })),
        secrets: secrets.map(row => ({ ...row })),
    };
}
/**
 * Create the probe that populates the GitHub repository panel.
 * @param cwd - Project root used as the command working directory
 * @param config - Merged Lisa project config
 * @param dependencies - Injectable auth and gh reads for focused tests
 * @returns Live-status probe for GET /api/status
 */
export function createGithubRepoProbe(cwd, config, dependencies = {}) {
    const reads = {
        ...defaultGithubRepoGhReads,
        ...dependencies.reads,
    };
    const authenticate = dependencies.authenticate ??
        (async (signal) => {
            const auth = await createGithubAuthProbe(cwd).run(signal);
            return auth.state === "value" && auth.value === true
                ? "authenticated"
                : "not-authenticated";
        });
    return {
        id: GITHUB_REPO_PROBE_ID,
        timeoutMs: PROBE_TIMEOUT_MS,
        run: async (signal) => {
            const authState = await authenticate(signal);
            if (authState !== "authenticated") {
                return {
                    state: "unknown",
                    reason: NOT_AUTHENTICATED,
                    message: GITHUB_NOT_AUTHENTICATED,
                };
            }
            const target = configuredGithubRepo(config);
            if (target === undefined) {
                return {
                    state: "unknown",
                    reason: "repo-not-configured",
                    message: "github.org and github.repo must be set in Lisa config",
                };
            }
            const timeoutMs = PROBE_TIMEOUT_MS + 250;
            const [settings, rulesets, remoteLabels, secretNames] = await Promise.all([
                reads.readSettings(target.owner, target.repo, cwd, timeoutMs, signal),
                reads.listRulesets(target.owner, target.repo, cwd, timeoutMs, signal),
                reads.listLabels(target.owner, target.repo, cwd, timeoutMs, signal),
                reads.listSecretNames(target.owner, target.repo, cwd, timeoutMs, signal),
            ]);
            const value = panelValue(target.owner, target.repo, settings, rulesets, compareLabels(expectedLabelsFromConfig(config), remoteLabels), compareSecrets(secretNames));
            const result = {
                state: "value",
                value,
            };
            return result;
        },
    };
}
//# sourceMappingURL=ui-github-repo.js.map