//@ts-nocheck
import path from 'path'
import postcss from 'rollup-plugin-postcss';
import typescript from '@rollup/plugin-typescript'
import babel,{ getBabelOutputPlugin } from '@rollup/plugin-babel';
import image from '@rollup/plugin-image';
import resolve from '@rollup/plugin-node-resolve';
import { terser } from 'rollup-plugin-terser';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import replace from '@rollup/plugin-replace';
import fs from 'fs';
const config = {
    input: path.resolve(__dirname, './src/Button/index.tsx'),
    treeshake:true,
    onwarn ({ loc, frame, message }) {
        if (loc) {
            // console.warn(`pabala-,${loc.file} (${loc.line}:${loc.column}) ${message}`);
            // if (frame) console.warn(frame);
        } else {
            console.warn('pabala-',message);
        }
    },
    output: [
        {
            dir:path.resolve(__dirname, 'dist/esm'),
            format: 'esm',
        },
        {
            dir:path.resolve(__dirname, 'dist/cjs'),
            format: 'cjs',
        },
        {
            name:'Button',
            format: 'umd',
            file: path.resolve(__dirname, `/umd/Button.umd.js`),
            plugins:[
                terser()
            ]
        },
    ],
    plugins: [
        image(),
        json(),
        replace({
            __buildEnv__: 'production',
            __buildDate__: () => new Date(),
            __buildVersion: 15,
            'process.env.NODE_ENV': JSON.stringify('production')
        }),
        postcss({
            modules: true, // 增加 css-module 功能
            extensions: ['.less', '.css'],
            use: [
                ['less', {
                    javascriptEnabled: true
                }]
            ],
            inject: true, // dev 环境下的 样式是入住到 js 中的，其他环境不会注入
            extract: false // 无论是 dev 还是其他环境这个配置项都不做 样式的抽离
        }),
        resolve({jsnext: true, preferBuiltins: true, browser: true})
    ]
};
const globals =  {
    antd: 'antd',
    react: 'React',
    'antd-mobile':'antdMobile',
    'react-dom': 'ReactDOM'
}

export default (type, cfgfile) =>{

    const pkgs = JSON.parse(fs.readFileSync(path.join(process.cwd(), './package.json')).toString('utf-8'));
    const tsStr = fs.readFileSync(path.join(__dirname,'../../template/pack.tsconfig.json')).toString("utf-8");
    const {
        external: cfgexternal = [],
        commonjs: cfgcommonjs = { include:[]},
    } = cfgfile || {};
    if('split' === type){
        return pkgs.componentList.map( (v)=>{
            const isexsist = fs.existsSync(path.join(process.cwd(),'./config/'+v.name + '.tsconfig.json'));
            if(!isexsist){
                const configExsist = fs.existsSync(path.join(process.cwd(),'./config'));
                if(!configExsist){
                    fs.mkdirSync(path.join(process.cwd(),'./config'))
                }
                fs.writeFileSync(path.join(process.cwd(),'./config/'+v.name + '.tsconfig.json'),
                    tsStr.replace('**temp**',v.name),{encoding:'utf-8'})
            }
            return {
                ...config,
                input: path.resolve(process.cwd(), `./src/${v.name}/index.tsx`),
                output: [
                    {
                        name:'Button',
                        format: 'umd',
                        file: path.resolve(process.cwd(), `./umd/${v.name}.umd.js`),
                        plugins:[
                            terser()
                        ],
                        globals
                    },
                ],
                external: [...Object.keys(pkgs.peerDependencies),...cfgexternal],
                plugins: [
                    ...config.plugins,
                    commonjs({
                        include: [
                            './node_modules/highcharts/**/*',
                            './node_modules/qrcode.react/**/*',
                            ...cfgcommonjs.include
                        ],
                        browser: true
                    }),
                    typescript({
                        tsconfig:'./config/'+v.name + '.tsconfig.json'
                    }),
                    babel({
                        babelHelpers: 'bundled',
                        exclude: path.join(process.cwd(), "node_modules/**"),  // 排除node_modules 下的文件
                    }),
                ]

            }
        });
    }
    else if('single' === type) {
        return [
            {
                ...config,
                input: path.join(process.cwd(), `./src/index.ts`),
                output: [
                    {
                        name:'autohome',
                        format: 'umd',
                        file: path.resolve(process.cwd(), `./dist/autohome.umd.js`),
                        plugins:[
                            terser()
                        ],
                        globals
                    },
                    {
                        name:'autohome',
                        format: 'cjs',
                        file: path.resolve(process.cwd(), `./dist/autohome.js`),
                        plugins:[
                            terser()
                        ],
                        globals
                    },{
                        name:'autohome',
                        format: 'cjs',
                        file: path.resolve(process.cwd(), `./dist/autohome1.js`),
                        plugins:[
                            terser()
                        ],
                        globals
                    },
                ],
                external: [...Object.keys(pkgs.peerDependencies),...cfgexternal],
                plugins: [
                    ...config.plugins,
                    commonjs({
                        include: [
                            './node_modules/highcharts/**/*',
                            './node_modules/qrcode.react/**/*',
                            ...cfgcommonjs.include
                        ],
                        browser: true
                    }),
                    typescript({
                        tsconfig: `./tsconfig.json`
                    }),
                    babel({
                        babelHelpers: 'bundled',
                        exclude: path.join(process.cwd(), "node_modules/**"),  // 排除node_modules 下的文件
                    }),
                ]

            }
        ]

    }
    return rollupcfg;
}
