"use strict";
/*
    规范DEMO用例集 http://gitlab.alipay-inc.com/yousai.lys/test_cube/tree/test/cssSupports
    
    DEMO中每个可操作对功能页均支持参数切换，参数的配置规则有两类:
    - [page].js 内 { data: { values: [], currentStyle: "" } }
    - [page].js 内 { data: { styleList: [], currentStyle: "" } }

    拆分方案 :
    - 将配置参数逐条构成新的功能页面
    - currentStyle 初始值设置为当前配置参数
    - 更新功能 app.json 内pages字段（过滤掉已拆分的原页面）
*/
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const Path = __importStar(require("path"));
const FS = __importStar(require("fs"));
const EsAST = __importStar(require("@babel/parser"));
const Utils = __importStar(require("../../../util/src"));
const pickers = Object({
    File: pick4File,
    Program: pick4program,
    ExpressionStatement: pick4ExpressionStatement,
    CallExpression: pick4CallExpression,
    ObjectExpression: pick4ObjectExpression,
    ObjectProperty: pick4ObjectProperty,
});
const encoders = Object({
    ObjectProperty: encode4ObjectProperty,
    ObjectExpression: encode4ObjectExpression,
    Identifier: encode4Identifier,
    StringLiteral: encode4StringLiteral,
    ArrayExpression: encode4ArrayExpression,
});
function split(path, dist = Path.join(process.env.HOME ? process.env.HOME : '', 'Desktop')) {
    const cost = new Utils.Cost();
    let count = 0;
    if (FS.statSync(path).isDirectory()) {
        dist = Path.join(dist, Path.basename(path));
        Utils.FS.rmdirs(dist);
        Utils.FS.mkdirs(dist);
        Utils.FS.copy(path, dist);
        Utils.FS.rmdirs(Path.join(dist, 'src/pages'));
        const app = JSON.parse(FS.readFileSync(Path.join(dist, 'src/app.json')).toString());
        app.pages = [];
        FS.writeFileSync(Path.join(dist, 'src/app.json'), JSON.stringify(app, undefined, 2));
        const subcaseRoot = Path.join(dist, 'src/pages/__rubik');
        Utils.FS.mkdirs(subcaseRoot);
        const saveSubpage = (originRoot, originName, newName, js) => {
            Utils.FS.copy(Path.join(originRoot, `${originName}.axml`), Path.join(subcaseRoot, `${newName}/${newName}.axml`));
            Utils.FS.copy(Path.join(originRoot, `${originName}.acss`), Path.join(subcaseRoot, `${newName}/${newName}.acss`));
            Utils.FS.copy(Path.join(originRoot, `${originName}.json`), Path.join(subcaseRoot, `${newName}/${newName}.json`));
            FS.writeFileSync(Path.join(subcaseRoot, `${newName}/${newName}.js`), js);
            const app = JSON.parse(FS.readFileSync(Path.join(dist, 'src/app.json')).toString());
            if (!app.pages) {
                app.pages = [];
            }
            app.pages = [...app.pages, `pages/__rubik/${newName}/${newName}`];
            FS.writeFileSync(Path.join(dist, 'src/app.json'), JSON.stringify(app, undefined, 2));
            Utils.Console.info(`拆分出子用例 ${newName}`);
            count++;
        };
        Utils.FS.walk(Path.join(path, 'src'), (root, name, stats) => {
            if (stats.isFile() && Path.extname(name) === '.axml') {
                const pageName = name.split('.axml').join('');
                const js = FS.readFileSync(Path.join(root, `${pageName}.js`)).toString();
                const ast = EsAST.parse(js);
                const dataNode = pick(ast, {
                    keyType: 'Identifier',
                    keyName: 'data'
                });
                const dataInfo = encode(dataNode);
                const data = dataInfo.data;
                //  values + currentStyle
                if (data.values && data.currentStyle !== undefined) {
                    const currentStyleNode = pick(dataNode, {
                        keyType: 'Identifier',
                        keyName: 'currentStyle'
                    });
                    let index = 0;
                    for (const avalue of data.values) {
                        const subjs = `${js.substring(0, currentStyleNode.value.start)}\`${data.property}: ${avalue};\`${js.substring(currentStyleNode.value.end)}`;
                        saveSubpage(root, pageName, `${pageName}_values_${index}`, subjs);
                        index++;
                    }
                }
                //  styleList + currentStyle
                if (data.styleList && data.currentStyle !== undefined) {
                    const currentStyleNode = pick(dataNode, {
                        keyType: 'Identifier',
                        keyName: 'currentStyle'
                    });
                    let index = 0;
                    for (const astyle of data.styleList) {
                        const subdata = Object.assign({}, data);
                        subdata.currentStyle = `${astyle}`;
                        const subjs = `${js.substring(0, currentStyleNode.value.start)}\`${astyle};\`${js.substring(currentStyleNode.value.end)}`;
                        saveSubpage(root, pageName, `${pageName}_styleList_${index}`, subjs);
                        index++;
                    }
                }
                //  demoList
                if (data.demoList && data.currentDemo !== undefined && data.currentNote !== undefined) {
                    for (const ademo of data.demoList) {
                        const subdata = Object.assign({}, data);
                        subdata.currentDemo = ademo.name;
                        subdata.currentNode = ademo.note;
                        //  TODO
                    }
                }
            }
        });
    }
    Utils.Console.info(`总计拆分出总用例 [${count}] 个`);
    cost.mark();
    return Promise.resolve();
}
exports.split = split;
//  pick ...
function pick(node, options) {
    const call = pickers[`${node.type}`];
    if (call) {
        return call(node, options);
    }
    return undefined;
}
function pick4File(node, options) {
    return pick(node.program, options);
}
function pick4program(node, options) {
    let result = undefined;
    for (const abody of node.body) {
        result = pick(abody, options);
        if (result) {
            break;
        }
    }
    return result;
}
function pick4ExpressionStatement(node, options) {
    return pick(node.expression, options);
}
function pick4CallExpression(node, options) {
    let result = undefined;
    for (const aargu of node.arguments) {
        result = pick(aargu, options);
        if (result) {
            break;
        }
    }
    return result;
}
function pick4ObjectExpression(node, options) {
    let result = undefined;
    for (const aprop of node.properties) {
        result = pick(aprop, options);
        if (result) {
            break;
        }
    }
    return result;
}
function pick4ObjectProperty(node, options) {
    if (node.key && node.key.type === options.keyType && node.key.name === options.keyName) {
        return node;
    }
    return pick(node.value, options);
} // encode ...
function encode(node) {
    const call = encoders[`${node.type}`];
    if (call) {
        return call(node);
    }
    return undefined;
}
function encode4ObjectProperty(node) {
    if (node.key && node.key.type === 'Identifier' && node.key.name === 'data') {
        return { data: encode(node.value) };
    }
    return undefined;
}
function encode4ObjectExpression(node) {
    const result = Object(undefined);
    for (const aprop of node.properties) {
        if (aprop.type === 'ObjectProperty') {
            const key = encode(aprop.key);
            const value = encode(aprop.value);
            if (key) {
                result[`${key}`] = value;
            }
        }
    }
    return result;
}
function encode4Identifier(node) {
    return node.name;
}
function encode4StringLiteral(node) {
    return node.value;
}
function encode4ArrayExpression(node) {
    const result = new Array();
    for (const aelement of node.elements) {
        result.push(encode(aelement));
    }
    return result;
}
//# sourceMappingURL=tiny-test-cube.js.map