<commit_before>'use strict';

var ref = require('../lib/reference')('v3');

module.exports = function upgrade(v2) {
    return converter(v2);
};

function converter(v2) {
    var v3 = {
        version: 3
    };

    if (v2.sprite) {
        v3.sprite = v2.sprite;
    }

    if (v2.constants) {
        v3.constants = {};
        for (var k in v2.constants) {
            v3.constants[(k.charAt(0) !== '@' ? '@' : '') + k] = v2.constants[k];
        }
    }

    if (v2.sources) {
        v3.sources = v2.sources;
    }

    var memo = {};

    v3.layers = v2.layers.map(function(layer) {
        return convertLayer(memo, layer, v2.buckets||{}, v2.styles||{}, v2.constants||{});
    });

    return v3;
}

/*
Converter.prototype.convertStyles = function(styles) {
    this.types = {};

    for (var style_name in styles) {
        var style = styles[style_name];
        for (var name in style) {

            var old_rules = style[name];
            var rules = {};
            for (var old_rule_name in old_rules) {
                var new_rule_name = old_rule_name.replace(/(^|-)point-/, '$1icon-');

                if (new_rule_name == 'icon-size') {
                    rules[new_rule_name] = old_rules[old_rule_name];
                    rules[new_rule_name] = Math.max(rules[new_rule_name][0], rules[new_rule_name][1]);
                } else if (new_rule_name != 'icon-antialias') {
                    rules[new_rule_name] = old_rules[old_rule_name];
                }
            }

            style[name] = rules;

            for (var rule_name in rules) {
                var match = rule_name.match(/(?:^|-)(icon|line|fill|text)-/);
                if (match) {
                    this.types[name] = match[1];
                    break;
                }
            }

            if (rules['fill-color'] && rules['line-color']) {
                rules['fill-stroke-color'] = rules['line-color'];
                delete rules['line-color'];
            }
        }
    }

    return styles;
};
*/

function convertLayer(memo, v2, buckets, styles, constants) {
    var j, k, val, known;
    var v3 = {};
    v3.id = v2.id;

    // This is a composite layer. Recurse.
    if (v2.layers) {
        v3.layers = v2.layers.map(function(layer) {
            return convertLayer(memo, layer, buckets, styles, constants);
        });
        return v3;
    // This layer's bucket has not been established yet. Do so.
    } else if (v2.bucket && !memo[v2.bucket]) {
        memo[v2.bucket] = v2.id;

        var bucket = buckets[v2.bucket];
        if (!bucket) throw new Error('bucket not found for layer ' + v2.id);

        // Migrate bucket.filter.
        if (bucket.filter) for (k in bucket.filter) {
            if (k === 'source') {
                v3.source = bucket.filter[k];
            } else if (k === 'layer') {
                v3['source-layer'] = bucket.filter[k];
            } else if (k === 'feature_type') {
                v3.filter = v3.filter || {};
                v3.filter.$type = bucket.filter[k];
            } else {
                v3.filter = v3.filter || {};
                v3.filter[k] = bucket.filter[k];
            }
        }
        // Migrate bucket properties.
        for (k in bucket) {
            if (k === 'filter') continue;
            val = bucket[k];
            v3.render = v3.render || {};

            // specific migrations.
            if (k === 'point-size') {
                k = 'icon-size';
                v3.render[k] = Array.isArray(val) ? val[0] : val;
                continue;
            }

            if (/^(fill|line|point|text|raster|composite)$/.test(k)) {
                v3.render.type = k === 'point' ? 'icon' : k;
                continue;
            }

            k = k.replace('point-','icon-');
            known = false;
            for (j = 0; j < ref.render.length; j++) {
                known = known || (!!ref[ref.render[j]][k]);
            }
            if (!known) {
                console.warn('Skipping unknown render property %s', k);
                continue;
            }

            v3.render[k] = val;
        }
    } else if (v2.bucket) {
        v3.ref = memo[v2.bucket];
    }

    for (var className in styles) {
        if (!styles[className][v2.id]) continue;
        var styleName = className === 'default' ? 'style' : 'style.' + className;
        for (k in styles[className][v2.id]) {
            val = styles[className][v2.id][k];
            val = typeof constants[val] !== 'undefined' ? '@' + val : val;
            v3[styleName] = v3[styleName] || {};

            // specific migrations.
            if (k === 'point-alignment') {
                k = 'icon-rotate-anchor';
                v3[styleName][k] = val !== 'screen' ? 'map' : 'viewport';
                continue;
            }

            k = k.replace('point-','icon-');
            known = false;
            for (j = 0; j < ref['class'].length; j++) {
                known = known || (!!ref[ref['class'][j]][k]);
            }
            if (!known) {
                console.warn('Skipping unknown class property %s', k);
                continue;
            }
            v3[styleName][k] = val;
        }
    }
    /*
    if (layer.filter) {
        layer.source = layer.filter.source;
        layer.layer = layer.filter.layer;
        delete layer.filter.source;
        delete layer.filter.layer;

        if (layer.filter.feature_type) {
            layer.geometry = layer.filter.feature_type;
            delete layer.filter.feature_type;
        }

        if (Object.keys(layer.filter).length === 0) {
            delete layer.filter;
        }
    }
    */
<commit_msg>More migration updates.<commit_after>'use strict';

var ref = require('../lib/reference')('v3');

module.exports = function upgrade(v2) {
    return converter(v2);
};

function converter(v2) {
    var v3 = {
        version: 3
    };

    if (v2.sprite) {
        v3.sprite = v2.sprite;
    }

    if (v2.constants) {
        v3.constants = {};
        for (var k in v2.constants) {
            v3.constants[(k.charAt(0) !== '@' ? '@' : '') + k] = v2.constants[k];
        }
    }

    if (v2.sources) {
        v3.sources = v2.sources;
    }

    var memo = {};

    v3.layers = v2.layers.map(function(layer) {
        return convertLayer(memo, layer, v2.buckets||{}, v2.styles||{}, v2.constants||{});
    });

    return v3;
}

/*
Converter.prototype.convertStyles = function(styles) {
    this.types = {};

    for (var style_name in styles) {
        var style = styles[style_name];
        for (var name in style) {

            var old_rules = style[name];
            var rules = {};
            for (var old_rule_name in old_rules) {
                var new_rule_name = old_rule_name.replace(/(^|-)point-/, '$1icon-');

                if (new_rule_name == 'icon-size') {
                    rules[new_rule_name] = old_rules[old_rule_name];
                    rules[new_rule_name] = Math.max(rules[new_rule_name][0], rules[new_rule_name][1]);
                } else if (new_rule_name != 'icon-antialias') {
                    rules[new_rule_name] = old_rules[old_rule_name];
                }
            }

            style[name] = rules;

            for (var rule_name in rules) {
                var match = rule_name.match(/(?:^|-)(icon|line|fill|text)-/);
                if (match) {
                    this.types[name] = match[1];
                    break;
                }
            }

            if (rules['fill-color'] && rules['line-color']) {
                rules['fill-stroke-color'] = rules['line-color'];
                delete rules['line-color'];
            }
        }
    }

    return styles;
};
*/

function convertLayer(memo, v2, buckets, styles, constants) {
    var j, k, val, known;
    var v3 = {};
    v3.id = v2.id;

    // This is a composite layer. Recurse.
    if (v2.layers) {
        v3.layers = v2.layers.map(function(layer) {
            return convertLayer(memo, layer, buckets, styles, constants);
        });
    // This layer's bucket has not been established yet. Do so.
    } else if (v2.bucket && !memo[v2.bucket]) {
        memo[v2.bucket] = v2.id;

        var bucket = buckets[v2.bucket];
        if (!bucket) throw new Error('bucket not found for layer ' + v2.id);

        // Migrate bucket.filter.
        if (bucket.filter) for (k in bucket.filter) {
            if (k === 'source') {
                v3.source = bucket.filter[k];
            } else if (k === 'layer') {
                v3['source-layer'] = bucket.filter[k];
            } else if (k === 'feature_type') {
                v3.filter = v3.filter || {};
                v3.filter.$type = bucket.filter[k];
            } else {
                v3.filter = v3.filter || {};
                v3.filter[k] = bucket.filter[k];
            }
        }
        // Migrate bucket properties.
        for (k in bucket) {
            if (k === 'filter') continue;
            val = bucket[k];
            v3.render = v3.render || {};

            // specific migrations.
            if (k === 'point-size') {
                k = 'icon-size';
                v3.render[k] = Array.isArray(val) ? val[0] : val;
                continue;
            }

            if (/^(fill|line|point|text|raster|composite)$/.test(k)) {
                v3.render.type = k === 'point' ? 'icon' : k;
                continue;
            }

            k = k.replace('point-','icon-');
            known = false;
            for (j = 0; j < ref.render.length; j++) {
                known = known || (!!ref[ref.render[j]][k]);
            }
            if (!known) {
                console.warn('Skipping unknown render property %s', k);
                continue;
            }

            v3.render[k] = val;
        }
    } else if (v2.bucket) {
        v3.ref = memo[v2.bucket];
    }

    for (var className in styles) {
        if (!styles[className][v2.id]) continue;
        var styleName = className === 'default' ? 'style' : 'style.' + className;
        for (k in styles[className][v2.id]) {
            val = styles[className][v2.id][k];
            val = typeof constants[val] !== 'undefined' ? '@' + val : val;
            v3[styleName] = v3[styleName] || {};

            // specific migrations.
            if (k === 'point-alignment') {
                k = 'icon-rotate-anchor';
                v3[styleName][k] = val !== 'screen' ? 'map' : 'viewport';
                continue;
            }
            if (k === 'stroke-color') {
                k = 'fill-outline-color';
                v3[styleName][k] = val;
                continue;
            }
            // composite styles
            if (k === 'opacity') {
                k = 'composite-opacity';
                v3.render = { type: 'composite' };
                v3[styleName][k] = val;
                continue;
            }
            if (k === 'transition-opacity') {
                k = 'transition-composite-opacity';
                v3.render = { type: 'composite' };
                v3[styleName][k] = val;
                continue;
            }

            k = k.replace('point-','icon-');
            known = false;
            for (j = 0; j < ref['class'].length; j++) {
                known = known || (!!ref[ref['class'][j]][k]);
            }
            if (!known) {
                console.warn('Skipping unknown class property %s', k);
                continue;
            }
            v3[styleName][k] = val;
        }
    }
    /*
    if (layer.filter) {
        layer.source = layer.filter.source;
        layer.layer = layer.filter.layer;
        delete layer.filter.source;
        delete layer.filter.layer;

        if (layer.filter.feature_type) {
            layer.geometry = layer.filter.feature_type;
            delete layer.filter.feature_type;
        }

        if (Object.keys(layer.filter).length === 0) {
            delete layer.filter;
        }
    }
    */

{'fraction_non_alphanumeric': 0.1542513167795335, 'fraction_numerical': 0.011871917063790653, 'mean_word_length': 1.4497235306164242, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1407251', 'n_tokens_mistral': 3747, 'n_tokens_neox': 3611, 'n_words': 752}