import React from "react";
import { ContinuousLegend, DiscreteColorLegend, } from "@emerson-eps/color-tables";
import { getLayersByType } from "../layers/utils/layerTools";
// Todo: Adapt it for other layers too
const ColorLegend = ({ visible, position, horizontal, layers, colorTables, }) => {
    var _a, _b, _c;
    const [legendProps, setLegendProps] = React.useState({
        title: "",
        name: "string",
        colorName: "string",
        discrete: false,
        metadata: { objects: {} },
        valueRange: [],
    });
    // layers will have entries of unique type only
    const wellsLayer = React.useMemo(() => { var _a; return (_a = getLayersByType(layers, "WellsLayer")) === null || _a === void 0 ? void 0 : _a[0]; }, [layers]);
    // Get color table for log curves.
    React.useEffect(() => {
        var _a, _b;
        if (!(wellsLayer === null || wellsLayer === void 0 ? void 0 : wellsLayer.isLoaded) || !wellsLayer.props.logData)
            return;
        const legend = wellsLayer.state.legend[0];
        setLegendProps({
            title: legend.title,
            name: (_a = wellsLayer === null || wellsLayer === void 0 ? void 0 : wellsLayer.props) === null || _a === void 0 ? void 0 : _a.logName,
            colorName: (_b = wellsLayer === null || wellsLayer === void 0 ? void 0 : wellsLayer.props) === null || _b === void 0 ? void 0 : _b.logColor,
            discrete: legend.discrete,
            metadata: legend.metadata,
            valueRange: legend.valueRange,
        });
    }, [
        wellsLayer === null || wellsLayer === void 0 ? void 0 : wellsLayer.isLoaded,
        (_a = wellsLayer === null || wellsLayer === void 0 ? void 0 : wellsLayer.props) === null || _a === void 0 ? void 0 : _a.logName,
        (_b = wellsLayer === null || wellsLayer === void 0 ? void 0 : wellsLayer.props) === null || _b === void 0 ? void 0 : _b.logColor,
    ]);
    const [showLegend, setShowLegend] = React.useState();
    React.useEffect(() => {
        // check log_curves from layer manager
        setShowLegend(visible && (wellsLayer === null || wellsLayer === void 0 ? void 0 : wellsLayer.props.visible) && (wellsLayer === null || wellsLayer === void 0 ? void 0 : wellsLayer.props.logCurves));
    }, [visible, wellsLayer === null || wellsLayer === void 0 ? void 0 : wellsLayer.props.visible, wellsLayer === null || wellsLayer === void 0 ? void 0 : wellsLayer.props.logCurves]);
    if (!showLegend)
        return null;
    return (React.createElement("div", null,
        legendProps.discrete && (React.createElement(DiscreteColorLegend, { discreteData: legendProps.metadata, dataObjectName: legendProps.title, position: position, colorName: legendProps.colorName, colorTables: colorTables, horizontal: horizontal })),
        ((_c = legendProps.valueRange) === null || _c === void 0 ? void 0 : _c.length) > 0 && legendProps && (React.createElement(ContinuousLegend, { min: legendProps.valueRange[0], max: legendProps.valueRange[1], dataObjectName: legendProps.title, position: position, colorName: legendProps.colorName, colorTables: colorTables, horizontal: horizontal }))));
};
ColorLegend.defaultProps = {
    visible: true,
    position: [5, 10],
    horizontal: false,
};
export default ColorLegend;
//# sourceMappingURL=ColorLegend.js.map