import { CompositeLayer } from "@deck.gl/core";
import { isDrawingEnabled } from "../utils/layerTools";
import { SolidPolygonLayer } from "@deck.gl/layers";
import { layersDefaultProps } from "../layersDefaultProps";
export default class PieChartLayer extends CompositeLayer {
    onClick(info) {
        var _a, _b;
        // Make selection only when drawing is disabled
        if (isDrawingEnabled(this.context.layerManager)) {
            return false;
        }
        else {
            const pie_idx = (_a = info.object) === null || _a === void 0 ? void 0 : _a.properties.pieIndex;
            this.context.userData.setEditedData({
                selectedPie: (_b = this.props.data) === null || _b === void 0 ? void 0 : _b.pies[pie_idx],
            });
            return true;
        }
    }
    renderLayers() {
        const pieData = this.props.data;
        if (!(pieData === null || pieData === void 0 ? void 0 : pieData.pies)) {
            // this.props.data is a sum type, and since TS doesn't have
            // pattern matching, we must check it this way.
            return [];
        }
        const layer = new SolidPolygonLayer(this.getSubLayerProps({
            data: makePies(pieData),
            getFillColor: (d) => d.properties.color,
        }));
        return [layer];
    }
}
PieChartLayer.layerName = "PieChartLayer";
PieChartLayer.defaultProps = layersDefaultProps["PieChartLayer"];
//================= Local help functions. ==================
function makePies(data) {
    let polygons = [];
    let pie_index = 0;
    for (const pie of data.pies) {
        polygons = polygons.concat(makePie(pie, data.properties, pie_index++));
    }
    return polygons;
}
// return array of one pie's polygon's
function makePie(pie, properties, pieIndex) {
    var _a, _b;
    const dA = 10; // delta angle
    const x = pie.x;
    const y = pie.y;
    const R = pie.R;
    // Normalize
    let sum = 0;
    for (const frac of pie.fractions) {
        sum += frac.value;
    }
    const pie_polygons = [];
    let start_a = -90.0;
    for (let i = 0; i < pie.fractions.length; i++) {
        const frac = pie.fractions[i].value / sum;
        const end_a = start_a + frac * 360.0;
        const coordinates = [];
        coordinates.push([x, y]);
        for (let a = start_a; a < end_a; a += dA) {
            const rad = (a * (2.0 * Math.PI)) / 360.0;
            const xx = R * Math.cos(rad) + x;
            const yy = R * Math.sin(rad) + y;
            coordinates.push([xx, yy]);
        }
        const rad = (end_a * (2.0 * Math.PI)) / 360.0;
        const xx = R * Math.cos(rad) + x;
        const yy = R * Math.sin(rad) + y;
        coordinates.push([xx, yy]);
        coordinates.push([x, y]);
        const prop = properties[pie.fractions[i].idx];
        const col = (_a = prop === null || prop === void 0 ? void 0 : prop.color) !== null && _a !== void 0 ? _a : [255, 0, 255]; // magenta
        const label = (_b = prop === null || prop === void 0 ? void 0 : prop.label) !== null && _b !== void 0 ? _b : "no label";
        pie_polygons.push({
            polygon: coordinates,
            properties: {
                color: col,
                name: label + ": " + (frac * 100).toFixed(1) + "%",
                pieIndex: pieIndex,
            },
        });
        start_a = end_a;
    }
    return pie_polygons;
}
//# sourceMappingURL=pieChartLayer.js.map