"use strict";
/**
 * ASL (Abstract Specification Language)
 *
 * Formal TypeScript schema for deterministic full-stack app compilation.
 * This is the canonical input format for the MyContext compiler.
 *
 * ASL is LLM-generated from natural language, then validated by the Planner,
 * and finally consumed by deterministic script generators.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.isASLComplete = isASLComplete;
exports.calculateCompleteness = calculateCompleteness;
exports.getMissingSections = getMissingSections;
exports.validateEntityReferences = validateEntityReferences;
exports.validatePermissionReferences = validatePermissionReferences;
// ============================================================================
// UTILITY TYPES
// ============================================================================
/**
 * Helper to check if ASL is complete enough for generation
 */
function isASLComplete(asl) {
    return !!(asl.version &&
        asl.project &&
        asl.entities &&
        Object.keys(asl.entities).length > 0 &&
        asl.pages &&
        asl.pages.length > 0);
}
/**
 * Calculate completeness percentage
 */
function calculateCompleteness(asl) {
    let score = 0;
    let total = 0;
    // Project (20 points)
    total += 20;
    if (asl.project?.name)
        score += 5;
    if (asl.project?.description)
        score += 5;
    if (asl.project?.framework)
        score += 5;
    if (asl.project?.backend)
        score += 5;
    // Entities (30 points)
    total += 30;
    if (asl.entities && Object.keys(asl.entities).length > 0) {
        score += 15;
        const entityCount = Object.keys(asl.entities).length;
        const fieldsComplete = Object.values(asl.entities).every(e => e.fields && e.fields.length > 0);
        if (fieldsComplete)
            score += 15;
    }
    // Pages (20 points)
    total += 20;
    if (asl.pages && asl.pages.length > 0) {
        score += 10;
        const pagesComplete = asl.pages.every(p => p.path && p.name);
        if (pagesComplete)
            score += 10;
    }
    // Auth (15 points)
    total += 15;
    if (asl.auth?.provider)
        score += 8;
    if (asl.auth?.roles && asl.auth.roles.length > 0)
        score += 7;
    // Permissions (10 points)
    total += 10;
    if (asl.permissions && asl.permissions.length > 0)
        score += 10;
    // Design (5 points)
    total += 5;
    if (asl.design?.theme)
        score += 5;
    return Math.round((score / total) * 100);
}
/**
 * Get missing sections from ASL
 */
function getMissingSections(asl) {
    const missing = [];
    if (!asl.project)
        missing.push("project");
    if (!asl.entities || Object.keys(asl.entities).length === 0)
        missing.push("entities");
    if (!asl.pages || asl.pages.length === 0)
        missing.push("pages");
    if (!asl.auth)
        missing.push("auth");
    if (!asl.permissions || asl.permissions.length === 0)
        missing.push("permissions");
    if (!asl.design)
        missing.push("design");
    return missing;
}
/**
 * Validate entity references in pages
 */
function validateEntityReferences(asl) {
    const errors = [];
    if (!asl.pages || !asl.entities)
        return errors;
    asl.pages.forEach((page, idx) => {
        page.queries?.forEach((query, qIdx) => {
            if (!asl.entities[query.entity]) {
                errors.push(`pages[${idx}].queries[${qIdx}]: Entity "${query.entity}" does not exist`);
            }
        });
        page.mutations?.forEach((mutation, mIdx) => {
            if (!asl.entities[mutation.entity]) {
                errors.push(`pages[${idx}].mutations[${mIdx}]: Entity "${mutation.entity}" does not exist`);
            }
        });
    });
    return errors;
}
/**
 * Validate permission references
 */
function validatePermissionReferences(asl) {
    const errors = [];
    if (!asl.permissions || !asl.entities || !asl.auth)
        return errors;
    asl.permissions.forEach((perm, idx) => {
        // Check role exists
        if (!asl.auth.roles?.find(r => r.name === perm.role)) {
            errors.push(`permissions[${idx}]: Role "${perm.role}" does not exist`);
        }
        // Check resource exists (unless it's "*")
        if (perm.resource !== "*" && !asl.entities[perm.resource]) {
            errors.push(`permissions[${idx}]: Resource "${perm.resource}" does not exist`);
        }
    });
    return errors;
}
//# sourceMappingURL=asl.js.map