'use strict';

module.exports = createTile;

function createTile(features, z2, tx, ty, tolerance, extent) {
    var tile = {
        features: [],
        numPoints: 0,
        numSimplified: 0,
        numFeatures: 0,
        source: null
    };
    for (var i = 0; i < features.length; i++) {
        tile.numFeatures++;
        addFeature(tile, features[i], z2, tx, ty, tolerance, extent);
    }
    return tile;
}

function addFeature(tile, feature, z2, tx, ty, tolerance, extent) {

    var geom = feature.geometry,
        type = feature.type,
        transformed = [],
        sqTolerance = tolerance * tolerance,
        i, j, ring, p;

    if (type === 1) {
        for (i = 0; i < geom.length; i++) {
            transformed.push(transformPoint(geom[i], z2, tx, ty, extent));
            tile.numPoints++;
            tile.numSimplified++;
        }

    } else {

        // simplify and transform projected coordinates for tile geometry
        for (i = 0; i < geom.length; i++) {
            ring = geom[i];
            for (j = 0; j < ring.length; j++) {
                p = ring[j];
                // keep points with significance > tolerance and points introduced by clipping
                if (p[2] === -1 || p[2] > sqTolerance) {
                    transformed.push(transformPoint(p, z2, tx, ty, extent));
                    tile.numSimplified++;
                }
                tile.numPoints++;
            }
        }
    }

    tile.features.push({
        geometry: transformed,
        type: type,
        tags: feature.tags || null
    });
}

function transformPoint(p, z2, tx, ty, extent) {
    var x = Math.round(extent * (p[0] * z2 - tx)),
        y = Math.round(extent * (p[1] * z2 - ty));
    return [x, y];
}

@@ -1014,16 +1014,56 @@
 eom%5Bi%5D;%0A
+%0A            var transformedRing = %5B%5D;%0A%0A
         
@@ -1306,32 +1306,36 @@
      transformed
+Ring
 .push(transformP
@@ -1463,32 +1463,80 @@
 +;%0A            %7D
+%0A%0A            transformed.push(transformedRing);
 %0A        %7D%0A    %7D

fix poly rings in simplified tiles