import { Matrix4 } from "math.gl";
import { cloneDeep } from "lodash";
import { layersDefaultProps } from "../layersDefaultProps";
// Creates property object which will be used to display layer property
// in the info card.
export function createPropertyData(name, value, color) {
    return {
        name: name,
        value: value,
        color: color,
    };
}
// Return a model matrix representing a rotation of "deg" degrees around the point x, y
export function getModelMatrix(deg, x, y) {
    const rad = deg * 0.017453;
    const IDENTITY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
    const m1 = new Matrix4(IDENTITY).translate([-x, -y, 0, 1]); // translate to origin
    const mRot = new Matrix4(IDENTITY).rotateZ(rad); // rotate
    const m2 = new Matrix4(IDENTITY).translate([x, y, 0, 1]); // translate back
    // Make  m2*mRot*m1
    mRot.multiplyRight(m1);
    const m2mRotm1 = m2.multiplyRight(mRot);
    return m2mRotm1;
}
// update layer object to include additional props
export function applyPropsOnLayers(layer_props, layers) {
    const result = cloneDeep(layers);
    result === null || result === void 0 ? void 0 : result.forEach((layer) => {
        const props = layer_props.find((l) => {
            if (layer["id"])
                return l["id"] === layer["id"];
            else
                return l["@@type"] === layer["@@type"];
        });
        if (props) {
            Object.entries(props).forEach(([prop, value]) => {
                if (layer[prop] == undefined)
                    layer[prop] = value;
            });
        }
        else {
            // if it's a user defined layer and its name and visibility are not specified
            // set layer id as its default name
            if (layer["name"] == undefined)
                layer["name"] = layer["id"];
            if (layer["visible"] == undefined)
                layer["visible"] = true;
        }
    });
    return result;
}
export function getLayersWithDefaultProps(layers) {
    return applyPropsOnLayers(Object.values(layersDefaultProps), layers);
}
export function getLayersInViewport(layers, layerIds) {
    if (layerIds && layerIds.length > 0) {
        const layers_in_view = layers.filter((layer) => layerIds.includes(layer["id"]));
        return layers_in_view;
    }
    else {
        return layers;
    }
}
export function getLayersByType(layers, type) {
    if (!layers)
        return [];
    return layers.filter((l) => l.constructor.name === type);
}
export function isDrawingEnabled(layer_manager) {
    var _a;
    const drawing_layer = (_a = layer_manager.getLayers({
        layerIds: ["drawing-layer"],
    })) === null || _a === void 0 ? void 0 : _a[0];
    return drawing_layer && drawing_layer.props.mode != "view";
}
//# sourceMappingURL=layerTools.js.map