"use strict";
/**
 * File Writer Utility
 *
 * Handles file writing with conflict detection and directory creation.
 */
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.writeFile = writeFile;
exports.writeFiles = writeFiles;
exports.printResults = printResults;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
/**
 * Write a single file to disk
 *
 * @param file - Generated file data
 * @param options - Write options
 * @returns Write result
 */
function writeFile(file, options = {}) {
    const { dryRun = false, force = false } = options;
    const existed = fs.existsSync(file.path);
    if (dryRun) {
        return {
            path: file.path,
            status: "dry-run",
            existed,
        };
    }
    // Check if file exists and we're not forcing
    if (existed && !force) {
        return {
            path: file.path,
            status: "skipped",
            existed: true,
        };
    }
    // Create directory if needed
    const dir = path.dirname(file.path);
    if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
    }
    // Write the file
    fs.writeFileSync(file.path, file.content, "utf-8");
    return {
        path: file.path,
        status: existed ? "updated" : "created",
        existed,
    };
}
/**
 * Write multiple files to disk
 *
 * @param files - Array of generated files
 * @param options - Write options
 * @returns Array of write results
 */
function writeFiles(files, options = {}) {
    return files.map((file) => writeFile(file, options));
}
/**
 * Print write results to console
 *
 * @param results - Array of write results
 */
function printResults(results) {
    const grouped = {
        created: results.filter((r) => r.status === "created"),
        updated: results.filter((r) => r.status === "updated"),
        skipped: results.filter((r) => r.status === "skipped"),
        dryRun: results.filter((r) => r.status === "dry-run"),
    };
    if (grouped.dryRun.length > 0) {
        console.info("\n📋 DRY RUN - Files that would be created:");
        grouped.dryRun.forEach((r) => {
            console.info(`   ${r.existed ? "🔄" : "✨"} ${r.path}`);
        });
    }
    if (grouped.created.length > 0) {
        console.info("\n✨ Created files:");
        grouped.created.forEach((r) => console.info(`   ${r.path}`));
    }
    if (grouped.updated.length > 0) {
        console.info("\n🔄 Updated files:");
        grouped.updated.forEach((r) => console.info(`   ${r.path}`));
    }
    if (grouped.skipped.length > 0) {
        console.info("\n⏭️  Skipped (already exist, use --force to overwrite):");
        grouped.skipped.forEach((r) => console.info(`   ${r.path}`));
    }
    console.info(`\nTotal: ${results.length} files (${grouped.created.length} created, ${grouped.updated.length} updated, ${grouped.skipped.length} skipped)`);
}
//# sourceMappingURL=file-writer.js.map