import { CompositeLayer } from "@deck.gl/core";
import { PolygonLayer } from "@deck.gl/layers";
import { COORDINATE_SYSTEM } from "@deck.gl/core";
import { layersDefaultProps } from "../layersDefaultProps";
import { rgbValues } from "@emerson-eps/color-tables/";
function getColorMapColors(colorMapName, colorTables) {
    const colors = [];
    for (let i = 0; i < 256; i++) {
        const value = i / 255.0;
        const rgb = rgbValues(value, colorMapName, colorTables);
        let color = [155, 255, 255];
        if (rgb != undefined) {
            if (Array.isArray(rgb)) {
                color = [rgb[0], rgb[1], rgb[2]];
            }
            else {
                color = [rgb.r, rgb.g, rgb.b];
            }
        }
        colors.push(color);
    }
    return colors;
}
export default class GridLayer extends CompositeLayer {
    initializeState() {
        this.setState({
            ti: 0, // timestep no
        });
        const updateTimeStepNo = () => {
            const ti_next = this.state.ti + 1;
            this.setState({
                ti: ti_next,
            });
        };
        // For now just cycle over the timesteps.
        setInterval(updateTimeStepNo, 500);
    }
    // For now, use `any` for the picking types because this function should
    // recieve PickInfo<FeatureCollection>, but it recieves PickInfo<Feature>.
    //eslint-disable-next-line
    getPickingInfo({ info }) {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        if (!info.object)
            return info;
        const feature = info.object;
        return Object.assign(Object.assign({}, info), { properties: [
                { name: "i", value: (_b = (_a = feature === null || feature === void 0 ? void 0 : feature.properties) === null || _a === void 0 ? void 0 : _a["i"]) !== null && _b !== void 0 ? _b : "NA" },
                { name: "j", value: (_d = (_c = feature === null || feature === void 0 ? void 0 : feature.properties) === null || _c === void 0 ? void 0 : _c["j"]) !== null && _d !== void 0 ? _d : "NA" },
                {
                    name: "depth:",
                    value: (_f = (_e = feature === null || feature === void 0 ? void 0 : feature.properties) === null || _e === void 0 ? void 0 : _e["depth"]) !== null && _f !== void 0 ? _f : "NA",
                },
                {
                    name: "value:",
                    value: (_g = feature === null || feature === void 0 ? void 0 : feature.properties) === null || _g === void 0 ? void 0 : _g["value"],
                    color: (_h = feature === null || feature === void 0 ? void 0 : feature.properties) === null || _h === void 0 ? void 0 : _h["color"],
                },
            ] });
    }
    renderLayers() {
        var _a, _b;
        const data = this.props.data;
        if (!data) {
            return [];
        }
        // Wrap around timestep if necessary.
        if (this.state.ti >= ((_b = (_a = data === null || data === void 0 ? void 0 : data[0]) === null || _a === void 0 ? void 0 : _a.vs.length) !== null && _b !== void 0 ? _b : 1)) {
            this.setState({
                ti: 0,
            });
        }
        const colors = getColorMapColors(this.props.colorMapName, this.context.userData.colorTables);
        const layer = new PolygonLayer(this.getSubLayerProps({
            data: makeLayerData(data, this.state.ti, colors, this.props.valueRange, this.props.colorMapRange),
            id: "grid-layer",
            getFillColor: (d) => d.properties.color,
            getLineColor: [0, 0, 0, 255],
            getLineWidth: 1,
            stroked: true,
            filled: true,
            lineWidthMinPixels: 1,
            visible: this.props.visible,
            coordinateSystem: COORDINATE_SYSTEM.CARTESIAN,
        }));
        return [layer];
    }
}
GridLayer.layerName = "GridLayer";
GridLayer.defaultProps = layersDefaultProps["GridLayer"];
//================= Local help functions. ==================
function makeLayerData(data, ti, colors, valueRange, colorMapRange) {
    const polygons = data.map(function (cell) {
        var _a, _b, _c, _d;
        const propertyValue = cell.vs[ti];
        const valueRangeMin = (_a = valueRange[0]) !== null && _a !== void 0 ? _a : 0.0;
        const valueRangeMax = (_b = valueRange[1]) !== null && _b !== void 0 ? _b : 1.0;
        // If specified color map will extend from colorMapRangeMin to colorMapRangeMax.
        // Otherwise it will extend from valueRangeMin to valueRangeMax.
        const colorMapRangeMin = (_c = colorMapRange === null || colorMapRange === void 0 ? void 0 : colorMapRange[0]) !== null && _c !== void 0 ? _c : valueRangeMin;
        const colorMapRangeMax = (_d = colorMapRange === null || colorMapRange === void 0 ? void 0 : colorMapRange[1]) !== null && _d !== void 0 ? _d : valueRangeMax;
        let x = propertyValue * (valueRangeMax - valueRangeMin) + valueRangeMin;
        x = (x - colorMapRangeMin) / (colorMapRangeMax - colorMapRangeMin);
        x = Math.max(0.0, x);
        x = Math.min(1.0, x);
        const color = colors[Math.floor(x * 255)];
        return {
            polygon: cell.cs,
            properties: {
                color,
                i: cell.i,
                j: cell.j,
                depth: cell.z,
                value: cell.vs[ti],
            },
        };
    });
    return polygons;
}
//# sourceMappingURL=gridLayer.js.map