"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildComplianceExport = buildComplianceExport;
const path = __importStar(require("node:path"));
const architectureValidation_1 = require("../architectureValidation");
const config_1 = require("../config");
const complianceExportSchemaVersion = '1.0';
function compareViolations(left, right) {
    const severityCompare = (0, architectureValidation_1.getSeveritySortOrder)(left.severity) -
        (0, architectureValidation_1.getSeveritySortOrder)(right.severity);
    if (severityCompare !== 0) {
        return severityCompare;
    }
    const ruleCompare = left.ruleId.localeCompare(right.ruleId);
    if (ruleCompare !== 0) {
        return ruleCompare;
    }
    const fileCompare = (left.file ?? '').localeCompare(right.file ?? '');
    if (fileCompare !== 0) {
        return fileCompare;
    }
    return left.message.localeCompare(right.message);
}
function toComplianceViolation(result) {
    return {
        ruleId: result.id,
        severity: result.severity,
        ...(result.category ? { category: result.category } : {}),
        message: result.message,
        ...(result.sourceFile ? { file: result.sourceFile } : {}),
    };
}
function resolvePolicyStatus(policy) {
    if (policy?.message && policy.message.trim().length > 0) {
        return policy.message.trim();
    }
    if (policy?.policySource) {
        return policy.remotePolicyLoaded
            ? 'Remote policy loaded successfully.'
            : 'Remote policy unavailable. Using local policy only.';
    }
    return 'Local policy only.';
}
async function buildComplianceExport(params) {
    const { workspaceRoot, validationRun, rulePackId } = params;
    const projectIdentity = await (0, config_1.loadProjectIdentity)(workspaceRoot);
    const repositoryName = path.basename(workspaceRoot);
    const violations = validationRun.status.results
        .filter((result) => !result.passed)
        .map((result) => toComplianceViolation(result))
        .sort(compareViolations);
    return {
        schemaVersion: complianceExportSchemaVersion,
        repository: {
            id: projectIdentity?.repositoryId ?? repositoryName,
            name: repositoryName,
            path: workspaceRoot,
            ...(projectIdentity?.organizationId
                ? { organizationId: projectIdentity.organizationId }
                : {}),
            ...(projectIdentity?.team ? { team: projectIdentity.team } : {}),
            ...(projectIdentity?.system ? { system: projectIdentity.system } : {}),
        },
        architecture: {
            ...(validationRun.config.architectureStyle
                ? { style: validationRun.config.architectureStyle }
                : {}),
            ...(rulePackId ? { rulePack: rulePackId } : {}),
        },
        policy: {
            ...(validationRun.status.policy?.policySource
                ? { source: validationRun.status.policy.policySource }
                : {}),
            status: resolvePolicyStatus(validationRun.status.policy),
        },
        summary: {
            score: validationRun.status.summary.healthScore,
            errors: validationRun.status.summary.errors,
            warnings: validationRun.status.summary.warnings,
            info: validationRun.status.summary.info,
            passed: validationRun.status.summary.passed,
            suppressed: validationRun.status.summary.suppressed,
            ignoredSuppressions: validationRun.status.summary.ignoredSuppressions,
        },
        violations,
        generatedAtUtc: params.generatedAtUtc ?? new Date().toISOString(),
    };
}
//# sourceMappingURL=complianceExportBuilder.js.map