"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useTemplateSelectionVisible = exports.useTemplateTaskPaneVisible = exports.useTemplateLegendVisible = exports.useConfiguredCapabilities = exports.useConfiguredUndoLastPointKey = exports.useConfiguredCancelDigitizationKey = exports.useConfiguredLoadIndicatorColor = exports.useConfiguredLoadIndicatorPositioning = exports.useConfiguredManualFeatureTooltips = exports.useViewerSelectCanDragPan = exports.useViewerFeatureTooltipsEnabled = exports.useViewerPointSelectionBuffer = exports.useViewerActiveFeatureSelectionColor = exports.useViewerSelectionColor = exports.useViewerSelectionImageFormat = exports.useViewerImageFormat = exports.useViewerIsStateless = exports.useIsContextMenuOpen = exports.useConfiguredAgentKind = exports.useConfiguredAgentUri = exports.useConfiguredCoordinateFormat = exports.useConfiguredCoordinateDecimals = exports.useConfiguredCoordinateProjection = exports.useViewerViewRotationEnabled = exports.useViewerActiveTool = exports.useViewerViewRotation = exports.useViewerBusyCount = exports.useAvailableMaps = exports.useActiveMapInitialView = exports.useActiveMapClientSelectionSet = exports.useCustomAppSettings = exports.useActiveMapSelectionSet = exports.useActiveMapBranch = exports.getActiveMapBranch = exports.useInitErrorOptions = exports.useInitErrorStack = exports.useInitError = exports.useInitWarnings = exports.useViewerFlyouts = exports.useCurrentMouseCoordinates = exports.useActiveMapExternalBaseLayers = exports.useViewerSizeUnits = exports.useActiveMapHeight = exports.useActiveMapWidth = exports.useActiveMapView = exports.useViewerLocale = exports.useActiveMapLayers = exports.useActiveMapInitialExternalLayers = exports.useActiveMapName = exports.usePrevious = void 0;
exports.useReducedToolbarAppState = exports.useLastDispatchedAction = exports.useTaskPaneNavigationStack = exports.useTaskPaneLastUrlPushed = exports.useTaskPaneNavigationIndex = exports.useTaskPaneInitialUrl = exports.useTemplateCustomData = exports.useTemplateInitialTaskPaneWidth = exports.useTemplateInitialInfoPaneWidth = void 0;
var tslib_1 = require("tslib");
var common_1 = require("../api/common");
var constants_1 = require("../constants");
var react_1 = require("react");
var array_1 = require("../utils/array");
var command_1 = require("../api/registry/command");
var context_1 = require("../components/map-providers/context");
// From: https://usehooks.com/usePrevious/
function usePrevious(value) {
    // The ref object is a generic container whose current property is mutable ...
    // ... and can hold any value, similar to an instance property on a class
    var ref = (0, react_1.useRef)();
    // Store current value in ref
    (0, react_1.useEffect)(function () {
        ref.current = value;
    }, [value]); // Only re-run if value changes
    // Return previous value (happens before update in useEffect above)
    return ref.current;
}
exports.usePrevious = usePrevious;
function useActiveMapName() {
    return (0, context_1.useAppState)(function (state) { return state.config.activeMapName; });
}
exports.useActiveMapName = useActiveMapName;
function useActiveMapInitialExternalLayers() {
    return (0, context_1.useAppState)(function (state) {
        if (state.config.activeMapName) {
            return state.mapState[state.config.activeMapName].initialExternalLayers;
        }
        return [];
    });
}
exports.useActiveMapInitialExternalLayers = useActiveMapInitialExternalLayers;
function sameHeatmapSettings(left, right) {
    if (left && right) {
        return left.blur == right.blur
            && left.radius == right.radius;
    }
    return false;
}
function useActiveMapLayers() {
    return (0, context_1.useAppState)(function (state) {
        if (state.config.activeMapName) {
            return state.mapState[state.config.activeMapName].layers;
        }
        return undefined;
    }, function (left, right) { return !(0, array_1.areArraysDifferent)(left, right, function (l, r) {
        return l.name == r.name
            && l.opacity == r.opacity
            && l.visible == r.visible
            && l.vectorStyle == r.vectorStyle
            && l.cluster == r.cluster
            && sameHeatmapSettings(l.heatmap, r.heatmap);
    }); });
}
exports.useActiveMapLayers = useActiveMapLayers;
function useViewerLocale() {
    return (0, context_1.useAppState)(function (state) { return state.config.locale; });
}
exports.useViewerLocale = useViewerLocale;
function useActiveMapView() {
    return (0, context_1.useAppState)(function (state) {
        return (0, common_1.getCurrentView)(state);
    });
}
exports.useActiveMapView = useActiveMapView;
function useActiveMapWidth() {
    return (0, context_1.useAppState)(function (state) { var _a, _b; return (_b = (_a = state.viewer) === null || _a === void 0 ? void 0 : _a.size) === null || _b === void 0 ? void 0 : _b[0]; });
}
exports.useActiveMapWidth = useActiveMapWidth;
function useActiveMapHeight() {
    return (0, context_1.useAppState)(function (state) { var _a, _b; return (_b = (_a = state.viewer) === null || _a === void 0 ? void 0 : _a.size) === null || _b === void 0 ? void 0 : _b[1]; });
}
exports.useActiveMapHeight = useActiveMapHeight;
function useViewerSizeUnits() {
    return (0, context_1.useAppState)(function (state) { return state.config.viewSizeUnits; });
}
exports.useViewerSizeUnits = useViewerSizeUnits;
/**
 * Gets the current set of available external base layers
 *
 * @returns The current set of available external base layers
 * @since 0.14 New required includeNonVisual parameter
 * @since 0.14.9 Removed includeNonVisual parameter. Let the hook caller determine themselves if non-visual base layers should be omitted
 */
function useActiveMapExternalBaseLayers() {
    return (0, context_1.useAppState)(function (state) { return (0, common_1.getExternalBaseLayers)(state); });
}
exports.useActiveMapExternalBaseLayers = useActiveMapExternalBaseLayers;
function useCurrentMouseCoordinates() {
    return (0, context_1.useAppState)(function (state) { return state.mouse.coords; });
}
exports.useCurrentMouseCoordinates = useCurrentMouseCoordinates;
function useViewerFlyouts() {
    return (0, context_1.useAppState)(function (state) { return state.toolbar.flyouts; });
}
exports.useViewerFlyouts = useViewerFlyouts;
function useInitWarnings() {
    return (0, context_1.useAppState)(function (state) { return state.initError.warnings; });
}
exports.useInitWarnings = useInitWarnings;
function useInitError() {
    return (0, context_1.useAppState)(function (state) { return state.initError.error; });
}
exports.useInitError = useInitError;
function useInitErrorStack() {
    return (0, context_1.useAppState)(function (state) { return state.initError.includeStack; });
}
exports.useInitErrorStack = useInitErrorStack;
function useInitErrorOptions() {
    return (0, context_1.useAppState)(function (state) { return state.initError.options; });
}
exports.useInitErrorOptions = useInitErrorOptions;
function getActiveMapBranch(state) {
    if (state.config.activeMapName) {
        return state.mapState[state.config.activeMapName];
    }
    return undefined;
}
exports.getActiveMapBranch = getActiveMapBranch;
function useActiveMapBranch() {
    return (0, context_1.useAppState)(function (state) { return getActiveMapBranch(state); });
}
exports.useActiveMapBranch = useActiveMapBranch;
function useActiveMapSelectionSet() {
    return (0, context_1.useAppState)(function (state) { var _a; return (_a = (0, common_1.getSelectionSet)(state)) !== null && _a !== void 0 ? _a : null; });
}
exports.useActiveMapSelectionSet = useActiveMapSelectionSet;
/**
 * Gets custom app settings
 *
 * @returns
 * @since 0.14
 */
function useCustomAppSettings() {
    return (0, context_1.useAppState)(function (state) { return state.config.appSettings; });
}
exports.useCustomAppSettings = useCustomAppSettings;
/**
 * Gets the client-side selection set
 * @since 0.14
 */
function useActiveMapClientSelectionSet() {
    return (0, context_1.useAppState)(function (state) { var _a; return (_a = getActiveMapBranch(state)) === null || _a === void 0 ? void 0 : _a.clientSelection; });
}
exports.useActiveMapClientSelectionSet = useActiveMapClientSelectionSet;
function useActiveMapInitialView() {
    return (0, context_1.useAppState)(function (state) { var _a; return (_a = getActiveMapBranch(state)) === null || _a === void 0 ? void 0 : _a.initialView; });
}
exports.useActiveMapInitialView = useActiveMapInitialView;
function useAvailableMaps() {
    return (0, context_1.useAppState)(function (state) { return state.config.availableMaps; });
}
exports.useAvailableMaps = useAvailableMaps;
function useViewerBusyCount() {
    return (0, context_1.useAppState)(function (state) { return state.viewer.busyCount; });
}
exports.useViewerBusyCount = useViewerBusyCount;
function useViewerViewRotation() {
    return (0, context_1.useAppState)(function (state) { return state.config.viewRotation; });
}
exports.useViewerViewRotation = useViewerViewRotation;
function useViewerActiveTool() {
    return (0, context_1.useAppState)(function (state) { return state.viewer.tool; });
}
exports.useViewerActiveTool = useViewerActiveTool;
function useViewerViewRotationEnabled() {
    return (0, context_1.useAppState)(function (state) { return state.config.viewRotationEnabled; });
}
exports.useViewerViewRotationEnabled = useViewerViewRotationEnabled;
function useConfiguredCoordinateProjection() {
    return (0, context_1.useAppState)(function (state) { return state.config.coordinates.projection; });
}
exports.useConfiguredCoordinateProjection = useConfiguredCoordinateProjection;
function useConfiguredCoordinateDecimals() {
    return (0, context_1.useAppState)(function (state) { return state.config.coordinates.decimals; });
}
exports.useConfiguredCoordinateDecimals = useConfiguredCoordinateDecimals;
function useConfiguredCoordinateFormat() {
    return (0, context_1.useAppState)(function (state) { return state.config.coordinates.format; });
}
exports.useConfiguredCoordinateFormat = useConfiguredCoordinateFormat;
function useConfiguredAgentUri() {
    return (0, context_1.useAppState)(function (state) { return state.config.agentUri; });
}
exports.useConfiguredAgentUri = useConfiguredAgentUri;
function useConfiguredAgentKind() {
    return (0, context_1.useAppState)(function (state) { return state.config.agentKind; });
}
exports.useConfiguredAgentKind = useConfiguredAgentKind;
function useIsContextMenuOpen() {
    return (0, context_1.useAppState)(function (state) {
        var _a, _b, _c;
        var isContextMenuOpen = false;
        if (((_c = (_b = (_a = state === null || state === void 0 ? void 0 : state.toolbar) === null || _a === void 0 ? void 0 : _a.flyouts) === null || _b === void 0 ? void 0 : _b[constants_1.WEBLAYOUT_CONTEXTMENU]) === null || _c === void 0 ? void 0 : _c.open) === true) {
            isContextMenuOpen = true;
        }
        return isContextMenuOpen;
    });
}
exports.useIsContextMenuOpen = useIsContextMenuOpen;
/**
 * Gets whether the viewer is in stateless mode
 * @returns true if the viewer is in stateless mode
 * @since 0.14
 */
function useViewerIsStateless() {
    return (0, context_1.useAppState)(function (state) { return state.config.viewer.isStateless; });
}
exports.useViewerIsStateless = useViewerIsStateless;
function useViewerImageFormat() {
    return (0, context_1.useAppState)(function (state) { return state.config.viewer.imageFormat; });
}
exports.useViewerImageFormat = useViewerImageFormat;
function useViewerSelectionImageFormat() {
    return (0, context_1.useAppState)(function (state) { return state.config.viewer.selectionImageFormat; });
}
exports.useViewerSelectionImageFormat = useViewerSelectionImageFormat;
function useViewerSelectionColor() {
    return (0, context_1.useAppState)(function (state) { return state.config.viewer.selectionColor; });
}
exports.useViewerSelectionColor = useViewerSelectionColor;
function useViewerActiveFeatureSelectionColor() {
    return (0, context_1.useAppState)(function (state) { return state.config.viewer.activeSelectedFeatureColor; });
}
exports.useViewerActiveFeatureSelectionColor = useViewerActiveFeatureSelectionColor;
function useViewerPointSelectionBuffer() {
    return (0, context_1.useAppState)(function (state) { return state.config.viewer.pointSelectionBuffer; });
}
exports.useViewerPointSelectionBuffer = useViewerPointSelectionBuffer;
function useViewerFeatureTooltipsEnabled() {
    return (0, context_1.useAppState)(function (state) { return state.viewer.featureTooltipsEnabled; });
}
exports.useViewerFeatureTooltipsEnabled = useViewerFeatureTooltipsEnabled;
/**
 * @since 0.14.2
 */
function useViewerSelectCanDragPan() {
    return (0, context_1.useAppState)(function (state) { return state.config.selectCanDragPan; });
}
exports.useViewerSelectCanDragPan = useViewerSelectCanDragPan;
function useConfiguredManualFeatureTooltips() {
    return (0, context_1.useAppState)(function (state) { return state.config.manualFeatureTooltips; });
}
exports.useConfiguredManualFeatureTooltips = useConfiguredManualFeatureTooltips;
function useConfiguredLoadIndicatorPositioning() {
    return (0, context_1.useAppState)(function (state) { return state.config.viewer.loadIndicatorPositioning; });
}
exports.useConfiguredLoadIndicatorPositioning = useConfiguredLoadIndicatorPositioning;
function useConfiguredLoadIndicatorColor() {
    return (0, context_1.useAppState)(function (state) { return state.config.viewer.loadIndicatorColor; });
}
exports.useConfiguredLoadIndicatorColor = useConfiguredLoadIndicatorColor;
function useConfiguredCancelDigitizationKey() {
    return (0, context_1.useAppState)(function (state) { return state.config.cancelDigitizationKey; });
}
exports.useConfiguredCancelDigitizationKey = useConfiguredCancelDigitizationKey;
function useConfiguredUndoLastPointKey() {
    return (0, context_1.useAppState)(function (state) { return state.config.undoLastPointKey; });
}
exports.useConfiguredUndoLastPointKey = useConfiguredUndoLastPointKey;
function useConfiguredCapabilities() {
    return (0, context_1.useAppState)(function (state) { return state.config.capabilities; });
}
exports.useConfiguredCapabilities = useConfiguredCapabilities;
function useTemplateLegendVisible() {
    return (0, context_1.useAppState)(function (state) { return state.template.legendVisible; });
}
exports.useTemplateLegendVisible = useTemplateLegendVisible;
function useTemplateTaskPaneVisible() {
    return (0, context_1.useAppState)(function (state) { return state.template.taskPaneVisible; });
}
exports.useTemplateTaskPaneVisible = useTemplateTaskPaneVisible;
function useTemplateSelectionVisible() {
    return (0, context_1.useAppState)(function (state) { return state.template.selectionPanelVisible; });
}
exports.useTemplateSelectionVisible = useTemplateSelectionVisible;
function useTemplateInitialInfoPaneWidth() {
    return (0, context_1.useAppState)(function (state) { return state.template.initialInfoPaneWidth; });
}
exports.useTemplateInitialInfoPaneWidth = useTemplateInitialInfoPaneWidth;
function useTemplateInitialTaskPaneWidth() {
    return (0, context_1.useAppState)(function (state) { return state.template.initialTaskPaneWidth; });
}
exports.useTemplateInitialTaskPaneWidth = useTemplateInitialTaskPaneWidth;
/**
 *
 * @param key
 * @returns
 * @since 0.14.8
 */
function useTemplateCustomData(key) {
    return (0, context_1.useAppState)(function (state) { return state.template.templateData[key]; });
}
exports.useTemplateCustomData = useTemplateCustomData;
function useTaskPaneInitialUrl() {
    return (0, context_1.useAppState)(function (state) { return state.taskpane.initialUrl; });
}
exports.useTaskPaneInitialUrl = useTaskPaneInitialUrl;
function useTaskPaneNavigationIndex() {
    return (0, context_1.useAppState)(function (state) { return state.taskpane.navIndex; });
}
exports.useTaskPaneNavigationIndex = useTaskPaneNavigationIndex;
function useTaskPaneLastUrlPushed() {
    return (0, context_1.useAppState)(function (state) { return state.taskpane.lastUrlPushed; });
}
exports.useTaskPaneLastUrlPushed = useTaskPaneLastUrlPushed;
function useTaskPaneNavigationStack() {
    return (0, context_1.useAppState)(function (state) { return state.taskpane.navigation; });
}
exports.useTaskPaneNavigationStack = useTaskPaneNavigationStack;
function useLastDispatchedAction() {
    return (0, context_1.useAppState)(function (state) { return state.lastaction; });
}
exports.useLastDispatchedAction = useLastDispatchedAction;
function useReducedToolbarAppState() {
    var selection = useActiveMapSelectionSet();
    var clientSelection = useActiveMapClientSelectionSet();
    var tbState = (0, context_1.useAppState)(function (state) { return (0, command_1.reduceAppToToolbarState)(state); }, function (left, right) {
        return (left === null || left === void 0 ? void 0 : left.busyWorkerCount) == (right === null || right === void 0 ? void 0 : right.busyWorkerCount)
            && (left === null || left === void 0 ? void 0 : left.hasNextView) == (right === null || right === void 0 ? void 0 : right.hasNextView)
            && (left === null || left === void 0 ? void 0 : left.hasPreviousView) == (right === null || right === void 0 ? void 0 : right.hasPreviousView)
            && (left === null || left === void 0 ? void 0 : left.featureTooltipsEnabled) == (right === null || right === void 0 ? void 0 : right.featureTooltipsEnabled)
            && (left === null || left === void 0 ? void 0 : left.activeTool) == (right === null || right === void 0 ? void 0 : right.activeTool);
    });
    //Overwrite whatever selection flags from the reduceAppToToolbarState result
    var hasClientSelection = false;
    var hasSelection = false;
    if (selection) {
        if (selection.FeatureSet && selection.FeatureSet.Layer.length > 0) {
            hasSelection = true;
        }
        if (selection.SelectedFeatures && selection.SelectedFeatures.SelectedLayer.length > 0) {
            hasSelection = true;
        }
    }
    if (clientSelection && clientSelection.layers.length > 0) {
        hasClientSelection = true;
    }
    return tslib_1.__assign(tslib_1.__assign({}, tbState), { hasClientSelection: hasClientSelection, hasSelection: hasSelection });
}
exports.useReducedToolbarAppState = useReducedToolbarAppState;
//# sourceMappingURL=hooks.js.map