import { SimpleMeshLayer } from "@deck.gl/mesh-layers";
import { COORDINATE_SYSTEM } from "@deck.gl/core";
import fsShader from "./map.fs.glsl";
const defaultProps = {
    data: [{ position: [0, 0], angle: 0, color: [255, 0, 0, 0] }],
    propertyValuesImageData: { value: null, type: "object", async: true },
    getPosition: (d) => d.position,
    getColor: (d) => d.color,
    getOrientation: (d) => [0, d.angle, 0],
    contours: [-1, -1],
    isReadoutDepth: false,
    isContoursDepth: true,
    coordinateSystem: COORDINATE_SYSTEM.CARTESIAN,
    material: {
        ambient: 0.35,
        diffuse: 0.6,
        shininess: 600,
        specularColor: [255, 255, 255],
    },
};
// This is a private layer used only by the composite MapLayer.
// It is an extension of SimpleMeshLayer but with modified fragment shader
// so that the texture pixel values can be used as lookup in  a supplied color map.
export default class PrivateMeshLayer extends SimpleMeshLayer {
    // Signature from the base class, eslint doesn't like the any type.
    // eslint-disable-next-line
    draw({ uniforms }) {
        var _a, _b;
        const contourReferencePoint = (_a = this.props.contours[0]) !== null && _a !== void 0 ? _a : -1.0;
        const contourInterval = (_b = this.props.contours[1]) !== null && _b !== void 0 ? _b : -1.0;
        const isReadoutDepth = this.props.isReadoutDepth;
        const isContoursDepth = this.props.isContoursDepth;
        super.draw({
            uniforms: Object.assign(Object.assign({}, uniforms), { contourReferencePoint,
                contourInterval,
                isReadoutDepth,
                isContoursDepth }),
        });
    }
    getShaders() {
        const parentShaders = super.getShaders();
        // Overwrite the default fragment shader with ours.
        parentShaders.fs = fsShader;
        return Object.assign(Object.assign({}, parentShaders), { 
            // Inject this into vertex shader. Vi want to export vertex world position to
            // fragment shader for making contour lines.
            inject: {
                "vs:#decl": `
                  out vec3 worldPos;
                `,
                "vs:#main-start": `
                   worldPos = positions;
                `,
            } });
    }
    decodePickingColor() {
        return 0;
    }
    // For now, use `any` for the picking types.
    //eslint-disable-next-line
    getPickingInfo({ info }) {
        if (!info.color) {
            return info;
        }
        let valueString = "";
        const isPropertyReadout = !this.props.isReadoutDepth; // Either map properties or map depths are encoded here.
        if (isPropertyReadout && this.props.propertyValuesImageData) {
            // PROPERTY READOUT.
            const data = this.props.propertyValuesImageData.data;
            const float_view = new Float32Array(data.buffer, 0, data.length / 4);
            const w = this.props.propertyValuesImageData.width;
            const h = this.props.propertyValuesImageData.height;
            const j = Math.min(Math.floor(w * (info.color[0] / 255.0)), w - 1);
            const i = Math.min(Math.floor(h * (info.color[1] / 255.0)), h - 1);
            const pixelNo = i * w + j;
            const value = float_view[pixelNo];
            valueString =
                "Property: " +
                    (!Number.isNaN(value) && value !== undefined
                        ? value.toFixed(1)
                        : " - ");
        }
        else {
            // DEPTH READOUT.
            const DECODER = {
                rScaler: 256 * 256,
                gScaler: 256,
                bScaler: 1,
                offset: 0,
            };
            // Note these colors are in the  0-255 range.
            const r = info.color[0] * DECODER.rScaler;
            const g = info.color[1] * DECODER.gScaler;
            const b = info.color[2] * DECODER.bScaler;
            const value = r + g + b;
            valueString = "Depth: " + value.toFixed(1);
        }
        return Object.assign(Object.assign({}, info), { propertyValue: valueString });
    }
}
PrivateMeshLayer.layerName = "TerrainMapLayer";
PrivateMeshLayer.defaultProps = defaultProps;
//# sourceMappingURL=PrivateMeshLayer.js.map