"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddManageLayersContainer = exports.zoomToLayerExtents = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var i18n_1 = require("../api/i18n");
var common_1 = require("../api/common");
var manage_layers_1 = require("../components/layer-manager/manage-layers");
var add_layer_1 = require("../components/layer-manager/add-layer");
var core_1 = require("@blueprintjs/core");
var hooks_1 = require("./hooks");
var Vector_1 = tslib_1.__importDefault(require("ol/layer/Vector"));
var Cluster_1 = tslib_1.__importDefault(require("ol/source/Cluster"));
var proj_1 = require("ol/proj");
var map_1 = require("../actions/map");
var runtime_1 = require("../api/runtime");
var context_1 = require("../components/map-providers/context");
function zoomToLayerExtents(layerName, viewer) {
    var _a;
    var layer = viewer.getLayerManager().getLayer(layerName);
    // If the layer has a WGS84 bbox, we'll use that
    var ll_bbox = layer === null || layer === void 0 ? void 0 : layer.get(common_1.LayerProperty.WGS84_BBOX);
    if (ll_bbox) {
        var zoomBounds = (0, proj_1.transformExtent)(ll_bbox, "EPSG:4326", viewer.getProjection());
        viewer.zoomToExtent(zoomBounds);
    }
    else if (layer instanceof Vector_1.default) {
        var source = layer.getSource();
        var bounds = void 0;
        if (source instanceof Cluster_1.default) {
            bounds = (_a = source.getSource()) === null || _a === void 0 ? void 0 : _a.getExtent();
        }
        else {
            bounds = source.getExtent();
        }
        var sp = source.getProjection();
        var dp = viewer.getProjection();
        if (sp && dp) {
            bounds = (0, proj_1.transformExtent)(bounds, sp, dp);
        }
        if (Number.isFinite(bounds[0]) &&
            Number.isFinite(bounds[1]) &&
            Number.isFinite(bounds[2]) &&
            Number.isFinite(bounds[3])) {
            viewer.zoomToExtent(bounds);
        }
        else {
            console.warn("Attempted to zoom to invalid bounds for layer: ".concat(layerName));
        }
    }
}
exports.zoomToLayerExtents = zoomToLayerExtents;
var AddManageLayersContainer = function () {
    var dispatch = (0, context_1.useReduxDispatch)();
    var locale = (0, hooks_1.useViewerLocale)();
    var activeMapName = (0, hooks_1.useActiveMapName)();
    var layers = (0, hooks_1.useActiveMapLayers)();
    var view = (0, hooks_1.useActiveMapView)();
    var getLayerIndex = function (layerName) {
        if (layers) {
            for (var i = 0; i < layers.length; i++) {
                if (layers[i].name == layerName) {
                    return i;
                }
            }
        }
        return -1;
    };
    var onLayerAdded = function (layer) {
        if (activeMapName) {
            dispatch((0, map_1.mapLayerAdded)(activeMapName, layer));
        }
    };
    var onAddLayerBusyWorker = function (name) {
        if (activeMapName) {
            dispatch((0, map_1.addMapLayerBusyWorker)(activeMapName, name));
        }
    };
    var onRemoveLayerBusyWorker = function (name) {
        if (activeMapName) {
            dispatch((0, map_1.removeMapLayerBusyWorker)(activeMapName, name));
        }
    };
    var removeHandler = function (layerName) {
        if (activeMapName) {
            dispatch((0, map_1.removeMapLayer)(activeMapName, layerName));
        }
    };
    var upHandler = function (layerName) {
        var newIndex = getLayerIndex(layerName);
        if (activeMapName && newIndex >= 0) {
            dispatch((0, map_1.setMapLayerIndex)(activeMapName, layerName, newIndex - 1));
        }
    };
    var downHandler = function (layerName) {
        var newIndex = getLayerIndex(layerName);
        if (layers && activeMapName && newIndex < layers.length - 1) {
            dispatch((0, map_1.setMapLayerIndex)(activeMapName, layerName, newIndex + 1));
        }
    };
    var zoomToBounds = function (layerName) {
        var viewer = (0, runtime_1.getViewer)();
        if (viewer) {
            zoomToLayerExtents(layerName, viewer);
        }
    };
    var setVisibility = function (layerName, visible) {
        if (activeMapName) {
            dispatch((0, map_1.setMapLayerVisibility)(activeMapName, layerName, visible));
        }
    };
    var setOpacity = function (layerName, value) {
        if (activeMapName) {
            dispatch((0, map_1.setMapLayerOpacity)(activeMapName, layerName, value));
        }
    };
    var setHeatmapBlur = function (layerName, value) {
        if (activeMapName) {
            dispatch((0, map_1.setHeatmapLayerBlur)(activeMapName, layerName, value));
        }
    };
    var setHeatmapRadius = function (layerName, value) {
        if (activeMapName) {
            dispatch((0, map_1.setHeatmapLayerRadius)(activeMapName, layerName, value));
        }
    };
    var updateVectorStyle = function (layerName, value, which) {
        if (activeMapName) {
            dispatch((0, map_1.setMapLayerVectorStyle)(activeMapName, layerName, value, which));
        }
    };
    if (layers) {
        return React.createElement("div", { style: { padding: 8 } },
            React.createElement(core_1.Tabs, { id: "tabs", renderActiveTabPanelOnly: true },
                React.createElement(core_1.Tab, { id: "add_layer", title: React.createElement("span", null,
                        React.createElement(core_1.Icon, { icon: "new-layer", iconSize: core_1.Icon.SIZE_STANDARD }),
                        " ",
                        (0, i18n_1.tr)("ADD_LAYER", locale)), panel: React.createElement(add_layer_1.AddLayer, { onLayerAdded: onLayerAdded, onAddLayerBusyWorker: onAddLayerBusyWorker, onRemoveLayerBusyWorker: onRemoveLayerBusyWorker, locale: locale }) }),
                React.createElement(core_1.Tab, { id: "manage_layers", title: React.createElement("span", null,
                        React.createElement(core_1.Icon, { icon: "layers", iconSize: core_1.Icon.SIZE_STANDARD }),
                        " ",
                        (0, i18n_1.tr)("MANAGE_LAYERS", locale)), panel: React.createElement(manage_layers_1.ManageLayers, { layers: layers, locale: locale, currentResolution: view === null || view === void 0 ? void 0 : view.resolution, onSetOpacity: setOpacity, onSetHeatmapBlur: setHeatmapBlur, onSetHeatmapRadius: setHeatmapRadius, onSetVisibility: setVisibility, onZoomToBounds: zoomToBounds, onMoveLayerDown: downHandler, onMoveLayerUp: upHandler, onRemoveLayer: removeHandler, onVectorStyleChanged: updateVectorStyle }) })));
    }
    else {
        return React.createElement(React.Fragment, null);
    }
};
exports.AddManageLayersContainer = AddManageLayersContainer;
//# sourceMappingURL=add-manage-layers.js.map