import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const srcDir = path.resolve(__dirname, '../node_modules/tinymce');
const destDir = path.resolve(__dirname, '../public/tinymce');

function copyDir(src, dest) {
    fs.mkdirSync(dest, { recursive: true });
    const entries = fs.readdirSync(src, { withFileTypes: true });

    for (const entry of entries) {
        const srcPath = path.join(src, entry.name);
        const destPath = path.join(dest, entry.name);

        if (entry.isDirectory()) {
            copyDir(srcPath, destPath);
        } else {
            fs.copyFileSync(srcPath, destPath);
        }
    }
}

console.log('Copying TinyMCE assets...');
try {
    // Copy skins
    copyDir(path.join(srcDir, 'skins'), path.join(destDir, 'skins'));
    // Copy icons (optional if imported, but good for safety)
    copyDir(path.join(srcDir, 'icons'), path.join(destDir, 'icons'));
    // Copy models (optional)
    copyDir(path.join(srcDir, 'models'), path.join(destDir, 'models'));
    // Copy themes (optional)
    copyDir(path.join(srcDir, 'themes'), path.join(destDir, 'themes'));

    console.log('TinyMCE assets copied successfully.');
} catch (e) {
    console.error('Error copying TinyMCE assets:', e);
}
