const { pathResolve } = require('webpack-kit/utils');

const webpack = require('webpack');
const _getWebpackConfig = require('webpack-kit');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = (getWebpackConfig) => {
    return _getWebpackConfig((webpackConfig, { appConfig, isProd, babelConfig }) => {
        babelConfig.presets.push(["react"]);
        if (!isProd && appConfig.hmr) {
            babelConfig.env.development.plugins.push(["react-hot-loader/babel"]);
        }
        if (appConfig.antd && !appConfig.antd.disable) {
            babelConfig.plugins.push(["import", {
                "libraryName": "antd",
                "style": true
            }]);
            if (appConfig.extractAllCss) {
                const { fileName = 'antd', hash = appConfig.hash, include = './node_modules/antd', theme = {} } = appConfig.antd;
                if (typeof hash === 'boolean' && hash) {
                    hash = 6;
                }
                const pathInclude = [];
                if (typeof include === 'string') {
                    pathInclude.push(pathResolve(include));
                } else if (Array.isArray(include)) {
                    include.forEach(path => pathInclude.push(pathResolve(path)));
                }
                let antdTheme = {};
                if (typeof theme === 'string') {
                    const themePath = pathResolve(theme);
                    if (require('fs').existsSync(themePath)) {
                        antdTheme = require(themePath);
                    }
                } else if (typeof theme === 'object') {
                    antdTheme = theme;
                }
                const appCss = new ExtractTextPlugin({
                    filename: `${appConfig.fileName}${appConfig.hash ? `-[contenthash:${appConfig.hash}]` : ''}.css`,
                    disable: false,
                    allChunks: true
                });
                const antdCss = new ExtractTextPlugin({
                    filename: `${fileName}${hash ? `-[contenthash:${hash}]` : ''}.css`,
                    disable: false,
                    allChunks: true
                });
                const { getCssRule, getPostcssOptions } = require("webpack-kit/cssUtils.js");
                webpackConfig.plugins.push(appCss);
                webpackConfig.plugins.push(antdCss);
                webpackConfig.module.rules.push(getCssRule(appCss, "css", { exclude: pathInclude }));
                webpackConfig.module.rules.push(getCssRule(appCss, "less", { exclude: pathInclude }));
                webpackConfig.module.rules.push(getCssRule(antdCss, "less", {
                    include: pathInclude,
                    "use": antdCss.extract({
                        "fallback": "style-loader",
                        "use": ["css-loader", {
                            "loader": "postcss-loader",
                            "options": getPostcssOptions()
                        }, "less-loader?{\"modifyVars\":" + JSON.stringify(antdTheme) + "}"]
                    })
                }))
            }
        }
        if (typeof getWebpackConfig === "function") {
            return getWebpackConfig(webpackConfig, { appConfig, isProd, babelConfig })
        } else {
            return webpackConfig;
        }
    });
}