"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.modifyFile = modifyFile;
const fs = __importStar(require("fs"));
const diff_1 = require("diff");
const readFile_1 = require("./readFile");
const config_1 = require("../../config");
const _1 = require(".");
async function modifyFile(fileBlocks, filePath) {
    try {
        const exists = fs.existsSync(filePath);
        const contentToUpdate = exists ? await (0, _1.readBlocks)(filePath) : [];
        const edits = {};
        const before = [...contentToUpdate];
        const beforeContent = before.map((b) => b.content).join("");
        for (const block of fileBlocks) {
            if (!contentToUpdate[block.blockNumber]) {
                contentToUpdate[block.blockNumber] = {
                    blockNumber: block.blockNumber,
                    content: "",
                };
            }
            contentToUpdate[block.blockNumber].content = block.content;
            edits[block.blockNumber] = contentToUpdate[block.blockNumber];
        }
        const newContent = contentToUpdate.map((b) => b.content).join("");
        fs.writeFileSync(filePath, newContent);
        const newBlocks = await (0, readFile_1.readFile)(filePath);
        console.log("====BEFORE====");
        console.log(beforeContent);
        console.log("====AFTER====");
        console.log(newContent);
        const config = await (0, config_1.getConfig)();
        const extension = filePath.split(".").pop();
        const lintResult = await (0, _1.lintFile)(filePath);
        return `
    Your changes generated this diff:
    ${(0, diff_1.createPatch)(filePath, beforeContent, newContent)}

    ${lintResult && "Linting Result"}
    ${lintResult || ""}

    Are you sure that was correct? .
    `;
    }
    catch (e) {
        return e.message;
    }
}
//# sourceMappingURL=modifyFile.js.map