import { CompositeLayer } from "@deck.gl/core";
import { isEqual } from "lodash";
import { createPropertyData, computeBoundingBox } from "../utils/layerTools";
import { PrivatePointsLayer } from "./privatePointsLayer";
const defaultProps = {
    "@@type": "PointsLayer",
    name: "PointsLayer",
    id: "points-layer",
    color: [125, 0, 0, 255],
    radiusUnits: "pixels",
    pointRadius: 5,
    pickable: true,
    visible: true,
    ZIncreasingDownwards: true,
    depthTest: true,
};
export default class PointsLayer extends CompositeLayer {
    constructor(props) {
        super(props);
    }
    renderLayers() {
        const layer = new PrivatePointsLayer(this.getSubLayerProps({
            pickable: this.props.pickable,
            billboard: true,
            data: this.state["dataAttributes"],
            _pathType: "open",
            getFillColor: () => this.props.color,
            getRadius: () => this.props.pointRadius,
            radiusUnits: this.props.radiusUnits,
            updateTriggers: {
                getFillColor: [this.props.color],
                getRadius: [this.props.pointRadius],
            },
            depthTest: this.props.depthTest,
            ZIncreasingDownwards: this.props.ZIncreasingDownwards,
        }));
        return [layer];
    }
    initializeState() {
        const dataAttributes = this.rebuildDataAttributes(true);
        this.setState({ dataAttributes });
    }
    updateState({ props, oldProps }) {
        const needs_reload = !isEqual(props.pointsData, oldProps.pointsData);
        if (needs_reload) {
            const dataAttributes = this.rebuildDataAttributes(false);
            this.setState({ dataAttributes });
        }
        if (props.ZIncreasingDownwards != oldProps.ZIncreasingDownwards) {
            this.updateBoundingBox(true);
        }
    }
    getPickingInfo({ info }) {
        var _a;
        if (!info.color) {
            return info;
        }
        const layer_properties = [];
        const zScale = this.props.modelMatrix ? this.props.modelMatrix[10] : 1;
        if (typeof ((_a = info.coordinate) === null || _a === void 0 ? void 0 : _a[2]) !== "undefined") {
            const depth = (this.props.ZIncreasingDownwards
                ? -info.coordinate[2]
                : info.coordinate[2]) / Math.max(0.001, zScale);
            layer_properties.push(createPropertyData("Depth", depth));
        }
        return Object.assign(Object.assign({}, info), { properties: layer_properties });
    }
    rebuildDataAttributes(reportBoundingBox) {
        const dataArray = this.loadData();
        if (!dataArray) {
            return null;
        }
        this.updateBoundingBox(reportBoundingBox);
        return {
            length: dataArray.length / 3,
            attributes: {
                getPosition: {
                    value: dataArray,
                    size: 3,
                },
            },
        };
    }
    loadData() {
        if (Array.isArray(this.props.pointsData)) {
            return new Float32Array(this.props.pointsData);
        }
        if (this.props.pointsData instanceof Float32Array) {
            return this.props.pointsData;
        }
        console.warn("pointsData is not array");
        return new Float32Array();
    }
    updateBoundingBox(reportBoundingBox) {
        if (this.state["dataAttributes"] &&
            typeof this.props.reportBoundingBox === "function" &&
            reportBoundingBox) {
            const boundingBox = computeBoundingBox(this.state["dataAttributes"], 
            // why not for polylines ?
            this.props.ZIncreasingDownwards);
            this.props.reportBoundingBox({ layerBoundingBox: boundingBox });
        }
    }
}
PointsLayer.layerName = "PointsLayer";
PointsLayer.defaultProps = defaultProps;
//# sourceMappingURL=pointsLayer.js.map