/**
 * GitHub API client for internal-urls resolver
 */
export class GitHubClient {
    token;
    baseUrl = "https://api.github.com";
    constructor(token) {
        this.token = token || process.env.GITHUB_TOKEN;
    }
    async fetch(endpoint) {
        try {
            const url = `${this.baseUrl}${endpoint}`;
            const headers = {
                Accept: "application/vnd.github.v3+json",
            };
            if (this.token) {
                headers.Authorization = `token ${this.token}`;
            }
            const response = await fetch(url, { headers });
            if (response.status === 404) {
                return null; // Not found
            }
            if (!response.ok) {
                console.warn(`GitHub API error: ${response.status} ${response.statusText}`);
                return null;
            }
            return (await response.json());
        }
        catch (error) {
            console.warn(`GitHub API fetch error: ${error}`);
            return null;
        }
    }
    async getPR(owner, repo, prNumber) {
        const data = await this.fetch(`/repos/${owner}/${repo}/pulls/${prNumber}`);
        if (!data)
            return null;
        return {
            number: typeof data.number === "number" ? data.number : Number(prNumber),
            title: typeof data.title === "string" ? data.title : "",
            state: data.state === "closed" ? "closed" : "open",
            body: typeof data.body === "string" ? data.body : "",
            author: typeof data.user?.login === "string" ? data.user.login : "unknown",
            createdAt: typeof data.created_at === "string" ? data.created_at : "",
            updatedAt: typeof data.updated_at === "string" ? data.updated_at : "",
            url: typeof data.html_url === "string" ? data.html_url : "",
        };
    }
    async getIssue(owner, repo, issueNumber) {
        const data = await this.fetch(`/repos/${owner}/${repo}/issues/${issueNumber}`);
        if (!data)
            return null;
        const labels = Array.isArray(data.labels)
            ? data.labels
                .map((label) => {
                if (typeof label === "string")
                    return label;
                const typedLabel = label;
                return typeof typedLabel.name === "string" ? typedLabel.name : undefined;
            })
                .filter((label) => Boolean(label) && label !== "pull_request")
            : [];
        return {
            number: typeof data.number === "number" ? data.number : Number(issueNumber),
            title: typeof data.title === "string" ? data.title : "",
            state: data.state === "closed" ? "closed" : "open",
            body: typeof data.body === "string" ? data.body : "",
            author: typeof data.user?.login === "string" ? data.user.login : "unknown",
            createdAt: typeof data.created_at === "string" ? data.created_at : "",
            updatedAt: typeof data.updated_at === "string" ? data.updated_at : "",
            url: typeof data.html_url === "string" ? data.html_url : "",
            labels,
        };
    }
}
// Global client instance
let globalClient;
export function getGitHubClient(token) {
    if (!globalClient) {
        globalClient = new GitHubClient(token);
    }
    return globalClient;
}
//# sourceMappingURL=github-api.js.map