"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.buildQuickFixPlan = buildQuickFixPlan;
exports.applyQuickFixPlan = applyQuickFixPlan;
const path = __importStar(require("node:path"));
const remediation_1 = require("../remediation");
function normalizePathForCompare(value) {
    return value.replace(/\\/g, '/').toLowerCase();
}
function isPathInsideWorkspace(workspaceRoot, targetPath) {
    const workspaceAbsolute = path.resolve(workspaceRoot);
    const targetAbsolute = path.resolve(workspaceRoot, targetPath);
    const workspaceNormalized = normalizePathForCompare(workspaceAbsolute);
    const targetNormalized = normalizePathForCompare(targetAbsolute);
    if (targetNormalized === workspaceNormalized) {
        return true;
    }
    return targetNormalized.startsWith(`${workspaceNormalized}/`);
}
function buildQuickFixPlan(results) {
    const fixableIssues = [];
    const nonAutomaticIssues = [];
    for (const result of results) {
        if (!result.quickFix || !result.quickFix.available) {
            continue;
        }
        if (result.quickFix.type !== 'create-file') {
            nonAutomaticIssues.push({
                ruleId: result.id,
                reason: `${result.quickFix.type} (suggestion only)`,
            });
            continue;
        }
        const createFileActions = (result.quickFix.actions ?? []).filter((action) => action.actionType === 'create-file');
        if (createFileActions.length === 0) {
            nonAutomaticIssues.push({
                ruleId: result.id,
                reason: 'create-file metadata is missing create-file actions',
            });
            continue;
        }
        fixableIssues.push({
            ruleId: result.id,
            description: result.quickFix.description,
            actions: createFileActions,
        });
    }
    return {
        fixableIssues,
        nonAutomaticIssues,
    };
}
async function applyQuickFixPlan(workspaceRoot, plan) {
    const created = [];
    const skippedExists = [];
    const skippedInvalidPath = [];
    const skippedUnsupported = [];
    const failed = [];
    for (const issue of plan.fixableIssues) {
        for (const action of issue.actions) {
            if (action.actionType !== 'create-file') {
                skippedUnsupported.push(action.targetPath);
                continue;
            }
            if (!isPathInsideWorkspace(workspaceRoot, action.targetPath)) {
                skippedInvalidPath.push(action.targetPath);
                continue;
            }
            const absoluteTargetPath = path.resolve(workspaceRoot, action.targetPath);
            if (await (0, remediation_1.pathExists)(absoluteTargetPath)) {
                skippedExists.push(action.targetPath);
                continue;
            }
            try {
                const createdFile = await (0, remediation_1.createFileIfMissing)(absoluteTargetPath, action.content ?? '');
                if (createdFile) {
                    created.push(action.targetPath);
                }
                else {
                    skippedExists.push(action.targetPath);
                }
            }
            catch (error) {
                failed.push({
                    targetPath: action.targetPath,
                    error: error instanceof Error ? error.message : 'Unable to create file.',
                });
            }
        }
    }
    return {
        created,
        skippedExists,
        skippedInvalidPath,
        skippedUnsupported,
        failed,
    };
}
//# sourceMappingURL=quickFixEngine.js.map