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 (ifPlatform.indexOf(platform) > -1) {
                content = content.replace(new RegExp(ifRegExp), '');
                content = content.replace(new RegExp(endRegExp), '');
                continue;
            }
        } else {
            if (ifPlatform.indexOf(platform) === -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 (options) {

    const ifRegExp = '//\\s*#ifdef\\s+(.+?)\\n';
    const ifnRegExp = '//\\s*#ifndef\\s+(.+?)\\n';
    const endRegExp = '//\\s*#endif';

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

    // ifndef
    content = main({
        content,
        platform: options.platform,
        ifRegExp: ifnRegExp,
        endRegExp,
        not: true
    });

    return content;
};