import { randomUUID } from 'node:crypto';
import { appendFileSync, existsSync } from 'node:fs';
import { EOL } from 'node:os';
import process from 'node:process';
import axios from 'axios';
export const parseIssue = (issue) => {
    const result = /^(((?<owner>[^/]*)\/)?(?<repository>[^#]+)#)?(?<issue>\d+)$/.exec(issue);
    const groups = result?.groups;
    if (groups) {
        return { ...groups };
    }
    throw new Error(`Invalid issue format: ${issue}`);
};
export const getGithubOutputFile = () => {
    const filePath = process.env.GITHUB_OUTPUT;
    return filePath && existsSync(filePath) ? filePath : undefined;
};
export const setGithubTaskOutput = (name, value) => {
    const delimiter = `delimiter_${randomUUID()}`;
    const output = `${name}<<${delimiter}${EOL}${value}${EOL}${delimiter}${EOL}`;
    const filePath = getGithubOutputFile();
    if (filePath) {
        appendFileSync(filePath, output, { encoding: 'utf8' });
    }
    else {
        console.log(output);
    }
};
export const getGithubIssue = async ({ owner, repository, issue }) => {
    const token = process.env.GITHUB_TOKEN;
    const response = await axios.get(`https://api.github.com/repos/${owner}/${repository}/issues/${issue}`, {
        headers: {
            'X-GitHub-Api-Version': '2022-11-28',
            Authorization: token ? `Bearer ${token}` : undefined,
        },
    });
    return response.data;
};
export const getGithubSummaryFile = () => {
    const filePath = process.env.GITHUB_STEP_SUMMARY;
    return filePath && existsSync(filePath) ? filePath : undefined;
};
export const appendToSummary = (summary) => {
    const summaryFile = getGithubSummaryFile();
    if (summaryFile) {
        appendFileSync(summaryFile, summary, { encoding: 'utf8' });
    }
};
