import * as artifact from '@actions/artifact';
import * as glob from '@actions/glob';
function formatBytes(bytes) {
    if (bytes === 0)
        return '0 B';
    const k = 1024;
    const sizes = ['B', 'KB', 'MB', 'GB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return `${Number.parseFloat((bytes / k ** i).toFixed(1))} ${sizes[i]}`;
}
export class GitHubArtifactCapability {
    client = new artifact.DefaultArtifactClient();
    async upload(op, workdir) {
        const globber = await glob.create(op.patterns.join('\n'), {
            matchDirectories: false,
        });
        const files = await globber.glob();
        if (files.length === 0) {
            throw new Error(`No files matched patterns: ${op.patterns.join(', ')}`);
        }
        const { size } = await this.client.uploadArtifact(op.name, files, workdir, {
            compressionLevel: 6,
        });
        const normalizedSize = Number(size ?? 0);
        const sizeBytes = Number.isFinite(normalizedSize) ? normalizedSize : 0;
        console.log(`Uploaded artifact "${op.name}": ${files.length} files, ${formatBytes(sizeBytes)}`);
    }
    async download(op) {
        const { artifact } = await this.client.getArtifact(op.name);
        const { downloadPath } = await this.client.downloadArtifact(artifact.id, {
            path: op.destination,
        });
        console.log(`Downloaded artifact "${op.name}" to ${downloadPath}`);
    }
}
//# sourceMappingURL=artifacts.js.map