"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.configReducer = exports.CONFIG_INITIAL_STATE = void 0;
var tslib_1 = require("tslib");
var common_1 = require("../api/common");
var i18n_1 = require("../api/i18n");
var actions_1 = require("../constants/actions");
var logger_1 = require("../utils/logger");
var units_1 = require("../utils/units");
var type_guards_1 = require("../utils/type-guards");
exports.CONFIG_INITIAL_STATE = {
    agentUri: undefined,
    agentKind: "mapagent",
    locale: i18n_1.DEFAULT_LOCALE,
    activeMapName: undefined,
    availableMaps: undefined,
    viewRotation: 0,
    viewRotationEnabled: true,
    viewSizeUnits: common_1.UnitOfMeasure.Meters,
    manualFeatureTooltips: false,
    cancelDigitizationKey: common_1.KC_ESCAPE,
    undoLastPointKey: common_1.KC_U,
    selectCanDragPan: false,
    coordinates: {
        decimals: 6
    },
    viewer: {
        isStateless: false,
        imageFormat: "PNG",
        selectionImageFormat: "PNG8",
        selectionColor: "0x0000FFAA",
        activeSelectedFeatureColor: "0xFF0000AA",
        pointSelectionBuffer: 2,
        loadIndicatorPositioning: "top",
        loadIndicatorColor: "rgba(34, 153, 221, 0.9)",
    },
    capabilities: {
        hasTaskPane: false,
        hasTaskBar: false,
        hasStatusBar: false,
        hasNavigator: false,
        hasSelectionPanel: false,
        hasLegend: false,
        hasToolbar: false,
        hasViewSize: false
    }
};
function configReducer(state, action) {
    var _a, _b, _c;
    if (state === void 0) { state = exports.CONFIG_INITIAL_STATE; }
    switch (action.type) {
        case actions_1.ActionType.SET_LOCALE:
            {
                return tslib_1.__assign(tslib_1.__assign({}, state), { locale: action.payload });
            }
        case actions_1.ActionType.SET_APP_SETTING:
            {
                var settings = tslib_1.__assign({}, ((_a = state.appSettings) !== null && _a !== void 0 ? _a : {}));
                settings[action.payload.key] = action.payload.value;
                return tslib_1.__assign(tslib_1.__assign({}, state), { appSettings: settings });
            }
        case actions_1.ActionType.INIT_APP:
            {
                var payload = (_b = action.payload) !== null && _b !== void 0 ? _b : {};
                var maps = payload.maps;
                var availableMaps = [];
                var am = payload.activeMapName;
                var mapNames = Object.keys(maps);
                for (var _i = 0, mapNames_1 = mapNames; _i < mapNames_1.length; _i++) {
                    var mapName = mapNames_1[_i];
                    availableMaps.push({ name: maps[mapName].mapGroupId, value: mapName });
                }
                if (mapNames.indexOf(am) < 0) {
                    (0, logger_1.warn)("Invalid initial active map name: ".concat(am, ". Probably because we haven't properly implemented recovery of runtime maps on reload yet"));
                    am = mapNames[0];
                }
                var state1 = {
                    appSettings: payload.appSettings,
                    locale: payload.locale || i18n_1.DEFAULT_LOCALE,
                    capabilities: payload.capabilities,
                    activeMapName: am,
                    availableMaps: availableMaps
                };
                var newState = tslib_1.__assign(tslib_1.__assign({}, state), state1);
                if (payload.config != null && Object.keys(payload.config).length > 0) {
                    var coordConfig = tslib_1.__assign({}, state.coordinates);
                    var viewerConfig = tslib_1.__assign({}, state.viewer);
                    if (payload.config.isStateless != null) {
                        viewerConfig.isStateless = payload.config.isStateless;
                    }
                    if (payload.config.imageFormat != null) {
                        viewerConfig.imageFormat = payload.config.imageFormat;
                    }
                    if (payload.config.selectionImageFormat != null) {
                        viewerConfig.selectionImageFormat = payload.config.selectionImageFormat;
                    }
                    if (payload.config.selectionColor != null) {
                        viewerConfig.selectionColor = payload.config.selectionColor;
                    }
                    if (payload.config.pointSelectionBuffer != null) {
                        viewerConfig.pointSelectionBuffer = payload.config.pointSelectionBuffer;
                    }
                    if (payload.config.coordinateProjection != null) {
                        coordConfig.projection = payload.config.coordinateProjection;
                    }
                    if (payload.config.coordinateDecimals != null) {
                        coordConfig.decimals = payload.config.coordinateDecimals;
                    }
                    if (payload.config.coordinateDisplayFormat != null) {
                        coordConfig.format = payload.config.coordinateDisplayFormat;
                    }
                    var state2 = { viewer: viewerConfig, coordinates: coordConfig };
                    var mapKeys = Object.keys(payload.maps);
                    if (payload.config.viewSizeUnits != null) {
                        state2.viewSizeUnits = payload.config.viewSizeUnits;
                    }
                    else if (mapKeys.length == 1) {
                        var m = payload.maps[mapKeys[0]].map;
                        if (m) {
                            var arbCs = void 0;
                            if ((0, type_guards_1.isRuntimeMap)(m)) {
                                arbCs = (0, units_1.tryParseArbitraryCs)(m.CoordinateSystem.MentorCode);
                            }
                            else {
                                arbCs = (0, units_1.tryParseArbitraryCs)((_c = m === null || m === void 0 ? void 0 : m.meta) === null || _c === void 0 ? void 0 : _c.projection);
                            }
                            // If arbitrary, infer units from it
                            if (arbCs) {
                                state2.viewSizeUnits = arbCs.units;
                            }
                        }
                    }
                    return tslib_1.__assign(tslib_1.__assign({}, newState), state2);
                }
                else {
                    return newState;
                }
            }
        case actions_1.ActionType.MAP_ENABLE_SELECT_DRAGPAN:
            {
                return tslib_1.__assign(tslib_1.__assign({}, state), { selectCanDragPan: action.payload });
            }
        case actions_1.ActionType.MAP_SET_VIEW_ROTATION:
            {
                return tslib_1.__assign(tslib_1.__assign({}, state), { viewRotation: action.payload });
            }
        case actions_1.ActionType.MAP_SET_VIEW_ROTATION_ENABLED:
            {
                return tslib_1.__assign(tslib_1.__assign({}, state), { viewRotationEnabled: action.payload });
            }
        case actions_1.ActionType.MAP_SET_ACTIVE_MAP:
            {
                var data = action.payload;
                if (data) {
                    var state1 = {
                        activeMapName: data
                    };
                    return tslib_1.__assign(tslib_1.__assign({}, state), state1);
                }
            }
        case actions_1.ActionType.MAP_SET_VIEW_SIZE_UNITS:
            {
                //HACK: Huh? It's typed UnitOfMeasure on both ends, so where is the string coming from that causes
                //the RHS type to be string | UnitOfMeasure?
                var state1 = { viewSizeUnits: action.payload };
                return tslib_1.__assign(tslib_1.__assign({}, state), state1);
            }
        case actions_1.ActionType.MAP_SET_MANUAL_MAPTIP:
            {
                return tslib_1.__assign(tslib_1.__assign({}, state), { manualFeatureTooltips: action.payload });
            }
    }
    return state;
}
exports.configReducer = configReducer;
//# sourceMappingURL=config.js.map