/* eslint-disable quotes */
const { _root, _src } = require('./getBlConfig');

module.exports = (components) => {
    let configs = {
        "type": "VariableDeclaration",
        "declarations": [
            {
                "type": "VariableDeclarator",
                "id": {
                    "type": "Identifier",
                    "name": "componentsOptions"
                },
                "init": {
                    "type": "ObjectExpression",
                    "properties": [
                    ]
                }
            }
        ],
        "kind": "let"
    };
    let node = configs.declarations[0].init.properties;
    let imports = [];
    let count = 0;
    
    components.forEach(file => {
        let key = new RegExp(`.${_root}${_src}(\/.+)\.vue$`).exec(file)[1];
        // pages的key不一样, pages's key  like  /pages/detail  /pages/index
        if (/^\/pages\/.+\/index$/.test(key)) {
            key = key.substring(0, key.length - 6);
        } else {
            return;
        }
        // let jsPath = file.replace(/vue$/, 'js');
        let keyValue = key;
        let keyRaw = "'" + key + "'";
        node.push({
            "type": "Property",
            "key": {
                "type": "Literal",
                "value": keyValue,
                "raw": keyRaw
            },
            "computed": false,
            "value": {
                "type": "Identifier",
                "name": "v" + count
            },
            "kind": "init",
            "method": false,
            "shorthand": false
        });
        imports.push({
            "type": "ImportDeclaration",
            "specifiers": [
                {
                    "type": "ImportDefaultSpecifier",
                    "local": {
                        "type": "Identifier",
                        "name": "v" + count
                    }
                }
            ],
            "source": {
                "type": "Literal",
                "value": file,
                "raw": `'${file}'`
            }
        });

        count++;
    });

    return [...imports, configs];
};
