import { execFileSync } from "node:child_process";
export const DERIVE_PUBLISH_ARTIFACTS_SCHEMA_VERSION = 1;
function nonEmptyString(value) {
    return typeof value === "string" && value.trim().length > 0;
}
function normalizeTag(version) {
    const trimmed = version.trim();
    return trimmed.startsWith("v") ? trimmed : `v${trimmed}`;
}
function defaultReadNpmVersion(packageName, distTag = "latest") {
    try {
        const out = execFileSync("npm", ["view", packageName, "version", "--json", `--tag=${distTag}`], {
            encoding: "utf8",
            stdio: ["ignore", "pipe", "pipe"]
        }).trim();
        const parsed = JSON.parse(out);
        if (typeof parsed === "string" && parsed.trim()) {
            return parsed.trim();
        }
        if (Array.isArray(parsed) && typeof parsed[0] === "string") {
            return parsed[0].trim();
        }
        return nonEmptyString(out) ? out : null;
    }
    catch {
        return null;
    }
}
function defaultReadGhRelease(workspacePath, tag) {
    try {
        const out = execFileSync("gh", ["release", "view", tag, "--json", "url,tagName,publishedAt"], {
            cwd: workspacePath,
            encoding: "utf8",
            stdio: ["ignore", "pipe", "pipe"],
            env: { ...process.env, GH_PAGER: "", PAGER: "" }
        }).trim();
        const parsed = JSON.parse(out);
        return parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)
            ? parsed
            : null;
    }
    catch {
        return null;
    }
}
export function derivePublishArtifactsFragment(args) {
    const tag = normalizeTag(args.version);
    const artifacts = [
        {
            kind: "git-tag",
            ref: tag,
            version: args.version
        }
    ];
    const degraded = [];
    const gh = (args.collectors?.readGhRelease ?? defaultReadGhRelease)(args.workspacePath, tag);
    if (gh && nonEmptyString(gh.url)) {
        artifacts.push({
            kind: "github-release",
            url: gh.url,
            tagName: nonEmptyString(gh.tagName) ? gh.tagName : tag,
            publishedAt: gh.publishedAt ?? null
        });
    }
    else {
        degraded.push(`GitHub release not found for ${tag} (gh auth or unpublished)`);
    }
    const npmVersion = (args.collectors?.readNpmVersion ?? defaultReadNpmVersion)(args.packageName, args.distTag ?? "latest");
    if (npmVersion) {
        artifacts.push({
            kind: "npm",
            package: args.packageName,
            version: npmVersion,
            distTag: args.distTag ?? "latest"
        });
    }
    else {
        degraded.push(`npm registry lookup failed for ${args.packageName}`);
    }
    return {
        schemaVersion: DERIVE_PUBLISH_ARTIFACTS_SCHEMA_VERSION,
        fragmentKind: "publishArtifacts",
        version: args.version,
        publishArtifacts: artifacts,
        degraded
    };
}
