/**
 * Parse the project's calling `ci.yml` quality workflow inputs.
 * @module cli/ui-ci-quality-jobs-parse
 */
import { readFile } from "node:fs/promises";
import * as path from "node:path";
import { isJsonObject } from "../sync/json-path.js";
import { loadYaml } from "../utils/yaml.js";
/**
 * Read `ci.yml` from the project workflows directory.
 * @param cwd - Project root
 * @returns File contents
 */
export async function readCiYmlFile(cwd) {
    return await readFile(path.join(cwd, ".github", "workflows", "ci.yml"), "utf8");
}
/**
 * Split a comma-separated skip_jobs string into trimmed non-empty ids.
 * @param raw - Raw skip_jobs value from YAML
 * @returns Normalized job ids
 */
function parseSkipJobs(raw) {
    if (typeof raw !== "string") {
        return [];
    }
    return raw
        .split(",")
        .map(part => part.trim())
        .filter(part => part.length > 0);
}
/**
 * Coerce a YAML scalar to boolean with an explicit default.
 * @param value - Candidate YAML value
 * @param fallback - Default when absent or unreadable
 * @returns Boolean
 */
function asBoolean(value, fallback) {
    if (typeof value === "boolean") {
        return value;
    }
    if (value === "true") {
        return true;
    }
    if (value === "false") {
        return false;
    }
    return fallback;
}
/**
 * Coerce a YAML scalar to a trimmed string with a default.
 * @param value - Candidate YAML value
 * @param fallback - Default when absent
 * @returns String
 */
function asString(value, fallback) {
    if (typeof value === "string") {
        return value.trim();
    }
    if (typeof value === "number" || typeof value === "boolean") {
        return String(value);
    }
    return fallback;
}
/**
 * Find the `with:` map on the job that calls Lisa's quality workflow.
 * @param document - Parsed ci.yml root
 * @returns The with-block object, or undefined
 */
function qualityWithBlock(document) {
    if (!isJsonObject(document) || !isJsonObject(document.jobs)) {
        return undefined;
    }
    for (const job of Object.values(document.jobs)) {
        if (!isJsonObject(job) || typeof job.uses !== "string") {
            continue;
        }
        if (!job.uses.includes("quality.yml") &&
            !job.uses.includes("quality-rails.yml")) {
            continue;
        }
        if (isJsonObject(job.with)) {
            return job.with;
        }
        return {};
    }
    return undefined;
}
/**
 * Parse the project's calling `ci.yml` into quality workflow inputs.
 * @param cwd - Project root
 * @param readCiYml - Injectable file reader
 * @returns Normalized inputs
 */
export async function parseCiWorkflowInputs(cwd, readCiYml = readCiYmlFile) {
    const source = await readCiYml(cwd);
    const document = loadYaml(source);
    const withBlock = qualityWithBlock(document);
    if (withBlock === undefined) {
        throw new Error("ci.yml does not call quality.yml or quality-rails.yml with a with-block");
    }
    return {
        skipJobs: parseSkipJobs(withBlock.skip_jobs),
        verifyEnforced: asBoolean(withBlock.verify_enforced, false),
        complianceFramework: asString(withBlock.compliance_framework, "none").toLowerCase(),
        requireApproval: asBoolean(withBlock.require_approval, false),
    };
}
//# sourceMappingURL=ui-ci-quality-jobs-parse.js.map