"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertMdToPdf = convertMdToPdf;
const markdown_pdf_1 = __importDefault(require("markdown-pdf"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
/**
 * Converts a Markdown file to PDF.
 *
 * @param {string} inputPath - Path to the input Markdown file
 * @param {string} outputPath - Path to the output PDF file (optional, defaults to same name with .pdf extension)
 * @returns {Promise<void>} Promise that resolves when conversion is complete
 *
 * @example
 * // Convert README.md to README.pdf
 * await convertMdToPdf('README.md', 'README.pdf');
 *
 * @example
 * // Convert with automatic output name
 * await convertMdToPdf('GUIDE.md'); // Creates GUIDE.pdf
 */
async function convertMdToPdf(inputPath, outputPath) {
    return new Promise((resolve, reject) => {
        // Check if input file exists
        if (!fs_1.default.existsSync(inputPath)) {
            reject(new Error(`Input file not found: ${inputPath}`));
            return;
        }
        // Generate output path if not provided
        const finalOutputPath = outputPath ||
            path_1.default.join(path_1.default.dirname(inputPath), path_1.default.basename(inputPath, path_1.default.extname(inputPath)) + '.pdf');
        // Ensure output directory exists
        const outputDir = path_1.default.dirname(finalOutputPath);
        if (!fs_1.default.existsSync(outputDir)) {
            fs_1.default.mkdirSync(outputDir, { recursive: true });
        }
        console.log(`Converting ${inputPath} to ${finalOutputPath}...`);
        // Configure markdown-pdf options
        const options = {
            // Paper format
            paperFormat: 'A4',
            paperOrientation: 'portrait',
            paperBorder: '2cm',
            // Rendering options
            renderDelay: 1000,
            loadTimeout: 10000,
            // CSS styling for better appearance
            cssPath: path_1.default.join(__dirname, 'pdf-styles.css'),
            // Remarkable markdown renderer options
            remarkable: {
                html: true,
                breaks: true,
                typographer: true,
            },
        };
        try {
            // Convert markdown to pdf using callback
            // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment
            const converter = markdown_pdf_1.default(options);
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment
            const fromConverter = converter.from(inputPath);
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
            fromConverter.to(finalOutputPath, () => {
                console.log(`✓ Successfully created PDF: ${finalOutputPath}`);
                resolve();
            });
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            console.error(`✗ Error converting to PDF: ${errorMessage}`);
            reject(new Error(errorMessage));
        }
    });
}
/**
 * Main function to run from command line
 */
async function main() {
    const args = process.argv.slice(2);
    if (args.length === 0) {
        console.log('Usage: ts-node md-to-pdf.ts <input.md> [output.pdf]');
        console.log('');
        console.log('Examples:');
        console.log('  ts-node md-to-pdf.ts README.md');
        console.log('  ts-node md-to-pdf.ts GUIDE.md guide.pdf');
        console.log('  ts-node md-to-pdf.ts template/GUIDE.md docs/guide.pdf');
        process.exit(1);
    }
    const inputPath = args[0];
    const outputPath = args[1]; // Optional
    try {
        await convertMdToPdf(inputPath, outputPath);
        console.log('✓ Conversion complete!');
        process.exit(0);
    }
    catch (error) {
        console.error('✗ Conversion failed:', error);
        process.exit(1);
    }
}
// Run main function if this file is executed directly
if (require.main === module) {
    void main();
}
//# sourceMappingURL=md-to-pdf.js.map