"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initMapGuideCommands = void 0;
var tslib_1 = require("tslib");
var command_1 = require("./registry/command");
var assets_1 = require("../constants/assets");
var map_1 = require("../actions/map");
var url_1 = require("../utils/url");
var common_1 = require("./common");
var runtime_1 = require("./runtime");
var i18n_1 = require("./i18n");
var default_commands_1 = require("./default-commands");
var viewer_shim_1 = require("../containers/viewer-shim");
var olExtent = tslib_1.__importStar(require("ol/extent"));
var composite_selection_1 = require("./composite-selection");
function initMapGuideCommands() {
    //Feature Tooltips
    (0, command_1.registerCommand)(command_1.DefaultCommands.MapTip, {
        iconClass: assets_1.SPRITE_MAPTIP,
        selected: function (state) {
            return state.featureTooltipsEnabled === true;
        },
        enabled: function (state) { return !state.stateless; },
        invoke: function (dispatch, getState) {
            var enabled = getState().viewer.featureTooltipsEnabled;
            return dispatch((0, map_1.setFeatureTooltipsEnabled)(!enabled));
        }
    });
    //Quick Plot
    (0, command_1.registerCommand)(command_1.DefaultCommands.QuickPlot, {
        iconClass: assets_1.SPRITE_PRINT,
        selected: function () { return false; },
        enabled: function (state) { return !state.stateless; },
        invoke: function (dispatch, getState, _viewer, parameters) {
            var config = getState().config;
            var url = "component://QuickPlot";
            var cmdDef = (0, default_commands_1.buildTargetedCommand)(config, parameters);
            (0, command_1.openUrlInTarget)(command_1.DefaultCommands.QuickPlot, cmdDef, config.capabilities.hasTaskPane, dispatch, url);
        }
    });
    //Viewer Options
    (0, command_1.registerCommand)(command_1.DefaultCommands.ViewerOptions, {
        iconClass: assets_1.SPRITE_OPTIONS,
        selected: function () { return false; },
        enabled: function () { return true; },
        invoke: function (dispatch, getState, _viewer, parameters) {
            var config = getState().config;
            var url = "component://ViewerOptions";
            var cmdDef = (0, default_commands_1.buildTargetedCommand)(config, parameters);
            (0, command_1.openUrlInTarget)(command_1.DefaultCommands.ViewerOptions, cmdDef, config.capabilities.hasTaskPane, dispatch, url, (0, i18n_1.tr)("VIEWER_OPTIONS", config.locale));
        }
    });
    //Select Radius
    (0, command_1.registerCommand)(command_1.DefaultCommands.SelectRadius, {
        iconClass: assets_1.SPRITE_SELECT_RADIUS,
        selected: function () { return false; },
        enabled: function (state) { return !state.stateless; },
        invoke: function (_dispatch, _getState, viewer, parameters) {
            if (viewer) {
                var selMethod_1 = parameters.SelectionType || "INTERSECTS";
                viewer.digitizeCircle(function (circle) {
                    var _a;
                    var fact = viewer.getOLFactory();
                    var geom = fact.createGeomPolygonFromCircle(circle);
                    (_a = viewer.mapguideSupport()) === null || _a === void 0 ? void 0 : _a.selectByGeometry(geom, selMethod_1);
                });
            }
        }
    });
    //Select Polygon
    (0, command_1.registerCommand)(command_1.DefaultCommands.SelectPolygon, {
        iconClass: assets_1.SPRITE_SELECT_POLYGON,
        selected: function () { return false; },
        enabled: function (state) { return !state.stateless; },
        invoke: function (_dispatch, _getState, viewer, parameters) {
            if (viewer) {
                var selMethod_2 = parameters.SelectionType || "INTERSECTS";
                viewer.digitizePolygon(function (geom) {
                    var _a;
                    (_a = viewer.mapguideSupport()) === null || _a === void 0 ? void 0 : _a.selectByGeometry(geom, selMethod_2);
                });
            }
        }
    });
    //Clear Selection
    (0, command_1.registerCommand)(command_1.DefaultCommands.ClearSelection, {
        iconClass: assets_1.SPRITE_SELECT_CLEAR,
        selected: function () { return false; },
        enabled: function (state) { return command_1.CommandConditions.hasSelection(state) || command_1.CommandConditions.hasClientSelection(state); },
        invoke: function (dispatch, getState, viewer) {
            var _a;
            var st = getState();
            if (st.config.activeMapName) {
                dispatch((0, map_1.clearClientSelection)(st.config.activeMapName));
            }
            (_a = viewer === null || viewer === void 0 ? void 0 : viewer.mapguideSupport()) === null || _a === void 0 ? void 0 : _a.clearSelection();
        }
    });
    //Zoom to Selection
    (0, command_1.registerCommand)(command_1.DefaultCommands.ZoomToSelection, {
        iconClass: assets_1.SPRITE_ICON_ZOOMSELECT,
        selected: function () { return false; },
        enabled: function (state) { return command_1.CommandConditions.hasSelection(state) || command_1.CommandConditions.hasClientSelection(state); },
        invoke: function (dispatch, getState, viewer) {
            if (viewer) {
                var fact = viewer.getOLFactory();
                var st = getState();
                var selection = (0, common_1.getSelectionSet)(st);
                var cs = void 0;
                if (st.config.activeMapName) {
                    cs = st.mapState[st.config.activeMapName].clientSelection;
                }
                var compSel = new composite_selection_1.CompositeSelection(selection === null || selection === void 0 ? void 0 : selection.SelectedFeatures, cs);
                var bounds = compSel.getBounds();
                if (bounds) {
                    var view = viewer.getViewForExtent(bounds);
                    dispatch((0, map_1.setCurrentView)(view));
                }
            }
        }
    });
    //Buffer
    (0, command_1.registerCommand)(command_1.DefaultCommands.Buffer, {
        iconClass: assets_1.SPRITE_BUFFER,
        selected: function () { return false; },
        enabled: function (state) { return !state.stateless && command_1.CommandConditions.hasSelection(state); },
        invoke: function (dispatch, getState, _viewer, parameters) {
            var state = getState();
            var map = (0, common_1.getRuntimeMap)(state);
            var config = state.config;
            if (map) {
                var url = (0, url_1.ensureParameters)("".concat((0, runtime_1.getFusionRoot)(), "/widgets/BufferPanel/BufferPanel.php"), map.Name, map.SessionId, config.locale, false);
                url += "&popup=false&us=0";
                var cmdDef = (0, default_commands_1.buildTargetedCommand)(config, parameters);
                (0, command_1.openUrlInTarget)(command_1.DefaultCommands.Buffer, cmdDef, config.capabilities.hasTaskPane, dispatch, url);
            }
        }
    });
    //Select Within
    (0, command_1.registerCommand)(command_1.DefaultCommands.SelectWithin, {
        iconClass: assets_1.SPRITE_SELECT_FEATURES,
        selected: function () { return false; },
        enabled: function (state, parameters) { return !state.stateless && !command_1.CommandConditions.disabledIfEmptySelection(state, parameters); },
        invoke: function (dispatch, getState, _viewer, parameters) {
            var state = getState();
            var map = (0, common_1.getRuntimeMap)(state);
            var config = state.config;
            if (map) {
                var url = (0, url_1.ensureParameters)("".concat((0, runtime_1.getFusionRoot)(), "/widgets/SelectWithin/SelectWithinPanel.php"), map.Name, map.SessionId, config.locale, false);
                url += "&popup=false";
                var cmdDef = (0, default_commands_1.buildTargetedCommand)(config, parameters);
                (0, command_1.openUrlInTarget)(command_1.DefaultCommands.SelectWithin, cmdDef, config.capabilities.hasTaskPane, dispatch, url);
            }
        }
    });
    //Redline
    (0, command_1.registerCommand)(command_1.DefaultCommands.Redline, {
        iconClass: assets_1.SPRITE_REDLINE,
        selected: function () { return false; },
        enabled: function (state) { return !state.stateless && command_1.CommandConditions.isNotBusy(state); },
        invoke: function (dispatch, getState, viewer, parameters) {
            var state = getState();
            var map = (0, common_1.getRuntimeMap)(state);
            var config = state.config;
            if (map) {
                var bUseAdvancedStylization = true;
                var defaultDataStoreFormat = null;
                var defaultRedlineGeometryType = 0;
                var bCreateOnStartup = false;
                if (parameters.AutogenerateLayerNames)
                    if (parameters.StylizationType)
                        bUseAdvancedStylization = (parameters.StylizationType == "advanced");
                if (parameters.DataStoreFormat && parameters.RedlineGeometryFormat) {
                    if (parameters.DataStoreFormat == "SDF" ||
                        parameters.DataStoreFormat == "SHP" ||
                        parameters.DataStoreFormat == "SQLite") {
                        var geomTypes = parseInt(parameters.RedlineGeometryFormat);
                        if (parameters.DataStoreFormat == "SHP") {
                            //Only accept if geometry type is singular
                            if (geomTypes == 1 || geomTypes == 2 || geomTypes == 4) {
                                defaultDataStoreFormat = parameters.DataStoreFormat;
                                defaultRedlineGeometryType = geomTypes;
                                if (parameters.AutoCreateOnStartup)
                                    bCreateOnStartup = (parameters.AutoCreateOnStartup == "true");
                            }
                        }
                        else {
                            defaultDataStoreFormat = parameters.DataStoreFormat;
                            defaultRedlineGeometryType = geomTypes;
                            if (parameters.AutoCreateOnStartup)
                                bCreateOnStartup = (parameters.AutoCreateOnStartup == "true");
                        }
                    }
                }
                (0, viewer_shim_1.enableRedlineMessagePrompt)(parameters.UseMapMessage == "true");
                var url = (0, url_1.ensureParameters)("".concat((0, runtime_1.getFusionRoot)(), "/widgets/Redline/markupmain.php"), map.Name, map.SessionId, config.locale, true);
                url += "&POPUP=false";
                if (defaultDataStoreFormat != null && defaultRedlineGeometryType > 0) {
                    url += "&REDLINEFORMAT=".concat(defaultDataStoreFormat);
                    url += "&REDLINEGEOMTYPE=".concat(defaultRedlineGeometryType);
                    url += "&AUTOCREATE=".concat(bCreateOnStartup ? "1" : "0");
                }
                url += "&REDLINESTYLIZATION=".concat(bUseAdvancedStylization ? "ADVANCED" : "BASIC");
                var cmdDef = (0, default_commands_1.buildTargetedCommand)(config, parameters);
                (0, command_1.openUrlInTarget)(command_1.DefaultCommands.Redline, cmdDef, config.capabilities.hasTaskPane, dispatch, url);
            }
        }
    });
    //Feature Info
    (0, command_1.registerCommand)(command_1.DefaultCommands.FeatureInfo, {
        iconClass: assets_1.SPRITE_FEATURE_INFO,
        selected: function () { return false; },
        enabled: function (state) { return !state.stateless && command_1.CommandConditions.isNotBusy(state); },
        invoke: function (dispatch, getState, _viewer, parameters) {
            var state = getState();
            var map = (0, common_1.getRuntimeMap)(state);
            var config = state.config;
            if (map) {
                var url = (0, url_1.ensureParameters)("".concat((0, runtime_1.getFusionRoot)(), "/widgets/FeatureInfo/featureinfomain.php"), map.Name, map.SessionId, config.locale, true);
                var cmdDef = (0, default_commands_1.buildTargetedCommand)(config, parameters);
                (0, command_1.openUrlInTarget)(command_1.DefaultCommands.FeatureInfo, cmdDef, config.capabilities.hasTaskPane, dispatch, url);
            }
        }
    });
    //Query
    (0, command_1.registerCommand)(command_1.DefaultCommands.Query, {
        iconClass: assets_1.SPRITE_QUERY,
        selected: function () { return false; },
        enabled: function (state) { return !state.stateless && command_1.CommandConditions.isNotBusy(state); },
        invoke: function (dispatch, getState, _viewer, parameters) {
            var state = getState();
            var map = (0, common_1.getRuntimeMap)(state);
            var config = state.config;
            if (map) {
                var url = (0, url_1.ensureParameters)("".concat((0, runtime_1.getFusionRoot)(), "/widgets/Query/querymain.php"), map.Name, map.SessionId, config.locale, true);
                var cmdDef = (0, default_commands_1.buildTargetedCommand)(config, parameters);
                (0, command_1.openUrlInTarget)(command_1.DefaultCommands.Query, cmdDef, config.capabilities.hasTaskPane, dispatch, url);
            }
        }
    });
    //Theme
    (0, command_1.registerCommand)(command_1.DefaultCommands.Theme, {
        iconClass: assets_1.SPRITE_THEME,
        selected: function () { return false; },
        enabled: function (state) { return !state.stateless && command_1.CommandConditions.isNotBusy(state); },
        invoke: function (dispatch, getState, _viewer, parameters) {
            var state = getState();
            var map = (0, common_1.getRuntimeMap)(state);
            var config = state.config;
            if (map) {
                var url = (0, url_1.ensureParameters)("".concat((0, runtime_1.getFusionRoot)(), "/widgets/Theme/thememain.php"), map.Name, map.SessionId, config.locale, true);
                var cmdDef = (0, default_commands_1.buildTargetedCommand)(config, parameters);
                (0, command_1.openUrlInTarget)(command_1.DefaultCommands.Theme, cmdDef, config.capabilities.hasTaskPane, dispatch, url);
            }
        }
    });
    //Center Selection
    (0, command_1.registerCommand)(command_1.DefaultCommands.CenterSelection, {
        iconClass: assets_1.SPRITE_SELECT_CENTRE,
        selected: function () { return false; },
        enabled: function (state) { return !state.stateless && command_1.CommandConditions.hasSelection(state); },
        invoke: function (dispatch, getState, viewer) {
            var _a, _b;
            var state = getState();
            var mapName = state.config.activeMapName;
            if (mapName && viewer) {
                var mapState = state.mapState[mapName];
                var sf = (_b = (_a = mapState === null || mapState === void 0 ? void 0 : mapState.mapguide) === null || _a === void 0 ? void 0 : _a.selectionSet) === null || _b === void 0 ? void 0 : _b.SelectedFeatures;
                if (sf) {
                    var bbox = void 0;
                    for (var _i = 0, _c = sf.SelectedLayer; _i < _c.length; _i++) {
                        var layer = _c[_i];
                        for (var _d = 0, _e = layer.Feature.filter(function (f) { return f.Bounds != null; }); _d < _e.length; _d++) {
                            var f = _e[_d];
                            var b = f.Bounds.split(" ").map(function (s) { return parseFloat(s); });
                            if (!bbox) {
                                bbox = b;
                            }
                            else {
                                bbox = olExtent.extend(bbox, b);
                            }
                        }
                    }
                    if (bbox) {
                        var view = viewer.getViewForExtent(bbox);
                        dispatch((0, map_1.setCurrentView)(view));
                    }
                }
            }
        }
    });
}
exports.initMapGuideCommands = initMapGuideCommands;
//# sourceMappingURL=mapguide-commands.js.map