#!/usr/bin/env node
/* jshint esversion: 6 */ 
const fs = require('fs');

const defaultReplacements = {
    mjs: [
        [/(?<=const.+)(:)(?=.+require)/ig, ' as'],
        [/const (.+?)\s*=\s*require\(\'\.(.+)\'\)/ig, 'import $1 from \'\.$2.mjs\''],
        [/const (.+?)\s*=\s*require\(\'(.+)\'\)/ig, 'import $1 from \'$2\''],
        [/module\.exports =/ig, 'export']
    ]
}

const generateFile = (module) => {
    let { src, dest, title, replacements } = module;
    let text = fs.readFileSync(src, 'utf-8');

    replacements.forEach(rep => {
        text = text.replace(rep[0], rep[1]);
    });

    console.log(`Generate ${dest}`);

    fs.writeFileSync(dest,
`// ${title}
// DO NOT EDIT THIS FILE!
// Source: /${src}

${text}`, 'utf-8');
};

const files = [
    {
        src : 'src/main/ua-parser.js',
        dest : 'src/main/ua-parser.mjs',
        title : 'Generated ESM version of ua-parser-js',
        replacements : [
            [/\(func[\s\S]+strict\';/ig, ''],
            [/esversion\: 3/ig, 'esversion: 6'],
            [/\/[\/\s]+export[\s\S]+/ig,'export {UAParser};'],
            ...defaultReplacements.mjs
        ]
    },
    {
        src : 'src/bot-detection/bot-detection.js',
        dest :'src/bot-detection/bot-detection.mjs',
        title : 'Generated ESM version of ua-parser-js/bot-detection',
        replacements : [...defaultReplacements.mjs]
    },
    {
        src : 'src/browser-detection/browser-detection.js',
        dest :'src/browser-detection/browser-detection.mjs',
        title : 'Generated ESM version of ua-parser-js/browser-detection',
        replacements : [...defaultReplacements.mjs]
    },
    {
        src : 'src/device-detection/device-detection.js',
        dest :'src/device-detection/device-detection.mjs',
        title : 'Generated ESM version of ua-parser-js/device-detection',
        replacements : [...defaultReplacements.mjs]
    },
    {
        src : 'src/enums/ua-parser-enums.js',
        dest :'src/enums/ua-parser-enums.mjs',
        title : 'Generated ESM version of ua-parser-js/enums',
        replacements : [...defaultReplacements.mjs]
    },
    {
        src : 'src/enums/ua-parser-enums.js',
        dest :'src/enums/ua-parser-enums.d.ts',
        title : 'Generated type declarations of ua-parser-js/enums',
        replacements : [
            [/(const .+) = object\.freeze\(/ig, 'export $1: Readonly<'],
            [/(const .+) =( .+;)/ig, 'export $1: typeof$2'],
            [/}\);/ig, '}>;'],
            [/module\.exports =.+/igs, '']
        ]
    },
    {
        src : 'src/extensions/ua-parser-extensions.js',
        dest : 'src/extensions/ua-parser-extensions.mjs',
        title : 'Generated ESM version of ua-parser-js/extensions',
        replacements : [...defaultReplacements.mjs]
    },
    {
        src : 'src/helpers/ua-parser-helpers.js',
        dest : 'src/helpers/ua-parser-helpers.mjs',
        title : 'Generated ESM version of ua-parser-js/helpers',
        replacements : [...defaultReplacements.mjs]
    }
];

files.forEach(module => generateFile(module));