function main(options) {
    let { content, platform, ifRegExp, endRegExp, not } = options;
    const ifArr = content.match(new RegExp(ifRegExp, 'g'));
    const endArr = content.match(new RegExp(endRegExp, 'g'));

    if (!ifArr || !endArr) {
        return content;
    }
    
    let res = [];

    while (res = new RegExp(ifRegExp).exec(content)) {
        const ifPlatform = res[1].toLocaleLowerCase();
        const ifStart = res.index;
        const endRes = new RegExp(endRegExp).exec(content.slice(ifStart));
        const endStart = endRes.index + ifStart;
        const endEnd = endStart + endRes[0].length;

        if (!not) {
            if (platform === 'h5' && ifPlatform.indexOf('h5') > -1 || platform === 'bl' && ifPlatform.indexOf('bl') > -1) {
                content = content.replace(new RegExp(ifRegExp), '');
                content = content.replace(new RegExp(endRegExp), '');
                continue;
            }
        } else {
            if (platform === 'h5' && ifPlatform.indexOf('h5') === -1 || platform === 'bl' && ifPlatform.indexOf('bl') === -1) {
                content = content.replace(new RegExp(ifRegExp), '');
                content = content.replace(new RegExp(endRegExp), '');
                continue;
            }
        }
        
        content = content.slice(0, ifStart) + content.slice(endEnd + 1);
    };

    return content;
}

module.exports = function (result, options) {

    const htmlIfRegExp = '<!--\\s*#ifdef\\s+(.+?)\\s*-->';
    const htmlEndRegExp = '<!--\\s*#endif\\s*-->';
    const jsIfRegExp = '//\\s*#ifdef\\s+(.+?)\\n';
    const jsEndRegExp = '//\\s*#endif';
    const cssIfRegExp = '/\\*\\s*#ifdef\\s+(.+?)\\s*\\*/';
    const cssEndRegExp = '/\\*\\s*#endif\\s*\\*/';

    const htmlIfnRegExp = '<!--\\s*#ifndef\\s+(.+?)\\s*-->';
    const jsIfnRegExp = '//\\s*#ifndef\\s+(.+?)\\n';
    const cssIfnRegExp = '/\\*\\s*#ifndef\\s+(.+?)\\s*\\*/';

    // ifdef
    result.template.content = main({
        content: result.template.content,
        platform: options.platform,
        ifRegExp: htmlIfRegExp,
        endRegExp: htmlEndRegExp
    });

    result.script.content = main({
        content: result.script.content,
        platform: options.platform,
        ifRegExp: jsIfRegExp,
        endRegExp: jsEndRegExp
    });

    result.styles && result.styles.forEach(style => {
        style.content = main({
            content: style.content,
            platform: options.platform,
            ifRegExp: cssIfRegExp,
            endRegExp: cssEndRegExp
        });
    });

    // ifndef
    result.template.content = main({
        content: result.template.content,
        platform: options.platform,
        ifRegExp: htmlIfnRegExp,
        endRegExp: htmlEndRegExp,
        not: true
    });

    result.script.content = main({
        content: result.script.content,
        platform: options.platform,
        ifRegExp: jsIfnRegExp,
        endRegExp: jsEndRegExp,
        not: true
    });

    result.styles && result.styles.forEach(style => {
        style.content = main({
            content: style.content,
            platform: options.platform,
            ifRegExp: cssIfnRegExp,
            endRegExp: cssEndRegExp,
            not: true
        });
    });

    return result;
};