import { text } from "node:stream/consumers";
import { createHostRunner } from "@poe-code/process-runner";
const DEFAULT_ENDPOINT = "https://api.github.com/graphql";
const USER_AGENT = "poe-code-task-list/0.0.1";
const AUTH_ERROR = "gh auth token failed; install gh, run 'gh auth login', or pass auth: { token }";
export function createGhClient(options) {
    const fetchImpl = options.fetch ?? fetch;
    const endpoint = options.endpoint || DEFAULT_ENDPOINT;
    return {
        async graphql(query, variables) {
            const response = await fetchImpl(endpoint, {
                method: "POST",
                headers: {
                    Authorization: `Bearer ${options.token}`,
                    "Content-Type": "application/json",
                    "User-Agent": USER_AGENT
                },
                body: JSON.stringify({ query, variables })
            });
            const body = await response.text();
            if (response.status !== 200) {
                throw new Error(`GitHub GraphQL request failed with status ${response.status}: ${body}`);
            }
            const parsed = JSON.parse(body);
            if (parsed.data !== undefined && parsed.data !== null) {
                return parsed.data;
            }
            const firstError = parsed.errors?.[0];
            if (firstError !== undefined) {
                throw new Error(firstError.message ?? "GitHub GraphQL request failed");
            }
            return parsed.data;
        }
    };
}
export async function resolveAuth(options) {
    if (options.explicitToken !== undefined) {
        return options.explicitToken;
    }
    const runner = options.runner ?? createHostRunner();
    const handle = runner.exec({
        command: "gh",
        args: ["auth", "token"],
        stdout: "pipe",
        stderr: "pipe"
    });
    const [stdout, , result] = await Promise.all([
        handle.stdout === null ? Promise.resolve("") : text(handle.stdout),
        handle.stderr === null ? Promise.resolve("") : text(handle.stderr),
        handle.result
    ]);
    const token = stdout.trim();
    if (result.exitCode !== 0 || token.length === 0) {
        throw new Error(AUTH_ERROR);
    }
    return token;
}
export function resolveEndpoint(options = {}) {
    const env = options.env ?? process.env;
    const host = env.GH_HOST;
    if (host !== undefined && host !== "" && host !== "github.com") {
        return `https://${host}/api/graphql`;
    }
    return DEFAULT_ENDPOINT;
}
