"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskPaneContainer = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var task_pane_1 = require("../components/task-pane");
var url_1 = require("../utils/url");
var i18n_1 = require("../api/i18n");
var core_1 = require("@blueprintjs/core");
var hooks_1 = require("./hooks");
var map_1 = require("../actions/map");
var taskpane_1 = require("../actions/taskpane");
var flyout_1 = require("../actions/flyout");
var context_1 = require("../components/map-providers/context");
var TaskPaneContainerInner = /** @class */ (function (_super) {
    tslib_1.__extends(TaskPaneContainerInner, _super);
    function TaskPaneContainerInner(props) {
        var _this = _super.call(this, props) || this;
        _this.onCloseFlyout = function (id) { var _a, _b; return (_b = (_a = _this.props).closeFlyout) === null || _b === void 0 ? void 0 : _b.call(_a, id); };
        _this.onOpenFlyout = function (id, metrics) { var _a, _b; return (_b = (_a = _this.props).openFlyout) === null || _b === void 0 ? void 0 : _b.call(_a, id, metrics); };
        _this.onUrlLoaded = function (url) {
            var _a = _this.props, navigationStack = _a.navigationStack, navIndex = _a.navIndex, pushUrl = _a.pushUrl;
            var currentUrl = navigationStack[navIndex];
            if (!(0, url_1.areUrlsSame)(currentUrl, url)) {
                pushUrl === null || pushUrl === void 0 ? void 0 : pushUrl(url);
            }
        };
        _this.canGoHome = function () {
            var _a, _b, _c, _d, _e;
            var _f = _this.props, initialUrl = _f.initialUrl, navigationStack = _f.navigationStack, navIndex = _f.navIndex, map = _f.map;
            if (initialUrl) { //An initial URL was set
                var initUrl = ((_a = map === null || map === void 0 ? void 0 : map.mapguide) === null || _a === void 0 ? void 0 : _a.runtimeMap) && initialUrl
                    ? (0, url_1.ensureParameters)(initialUrl, (_c = (_b = map === null || map === void 0 ? void 0 : map.mapguide) === null || _b === void 0 ? void 0 : _b.runtimeMap) === null || _c === void 0 ? void 0 : _c.Name, (_e = (_d = map === null || map === void 0 ? void 0 : map.mapguide) === null || _d === void 0 ? void 0 : _d.runtimeMap) === null || _e === void 0 ? void 0 : _e.SessionId, _this.getLocale())
                    : initialUrl;
                return navigationStack.length > 0 //We have a navigation stack
                    && !(0, url_1.areUrlsSame)(navigationStack[navIndex], initUrl); //The current URL is not initial.
            }
            return false;
        };
        _this.canGoBack = function () {
            var navIndex = _this.props.navIndex;
            return navIndex > 0;
        };
        _this.canGoForward = function () {
            var _a = _this.props, navigationStack = _a.navigationStack, navIndex = _a.navIndex;
            return navIndex < navigationStack.length - 1;
        };
        var locale = _this.getLocale();
        _this.homeAction = {
            bpIconName: "home",
            tooltip: (0, i18n_1.tr)("TT_GO_HOME", locale),
            enabled: _this.canGoHome,
            invoke: function () { var _a, _b; return (_b = (_a = _this.props).goHome) === null || _b === void 0 ? void 0 : _b.call(_a); }
        };
        _this.backAction = {
            bpIconName: "arrow-left",
            tooltip: (0, i18n_1.tr)("TT_GO_BACK", locale),
            enabled: _this.canGoBack,
            invoke: function () { var _a, _b; return (_b = (_a = _this.props).goBack) === null || _b === void 0 ? void 0 : _b.call(_a); }
        };
        _this.forwardAction = {
            bpIconName: "arrow-right",
            tooltip: (0, i18n_1.tr)("TT_GO_FORWARD", locale),
            enabled: _this.canGoForward,
            invoke: function () { var _a, _b; return (_b = (_a = _this.props).goForward) === null || _b === void 0 ? void 0 : _b.call(_a); }
        };
        return _this;
    }
    TaskPaneContainerInner.prototype.getLocale = function () {
        return this.props.locale;
    };
    TaskPaneContainerInner.prototype.render = function () {
        var _this = this;
        var _a, _b, _c, _d;
        var _e = this.props, navigationStack = _e.navigationStack, navIndex = _e.navIndex, hasTaskBar = _e.hasTaskBar, lastUrlPushed = _e.lastUrlPushed, map = _e.map, maxHeight = _e.maxHeight, flyouts = _e.flyouts, isResizing = _e.isResizing;
        if (navigationStack[navIndex]) {
            var flyoutStates = {};
            if (flyouts) {
                var ids = Object.keys(flyouts);
                for (var _i = 0, ids_1 = ids; _i < ids_1.length; _i++) {
                    var fid = ids_1[_i];
                    flyoutStates[fid] = !!flyouts[fid].open;
                }
            }
            return React.createElement("div", null,
                React.createElement(task_pane_1.TaskPane, { currentUrl: navigationStack[navIndex], showTaskBar: hasTaskBar, lastUrlPushed: lastUrlPushed, homeAction: this.homeAction, backAction: this.backAction, onOpenFlyout: this.onOpenFlyout, onCloseFlyout: this.onCloseFlyout, forwardAction: this.forwardAction, session: (_b = (_a = map === null || map === void 0 ? void 0 : map.mapguide) === null || _a === void 0 ? void 0 : _a.runtimeMap) === null || _b === void 0 ? void 0 : _b.SessionId, mapName: (_d = (_c = map === null || map === void 0 ? void 0 : map.mapguide) === null || _c === void 0 ? void 0 : _c.runtimeMap) === null || _d === void 0 ? void 0 : _d.Name, onUrlLoaded: this.onUrlLoaded, maxHeight: maxHeight, flyoutStates: flyoutStates, locale: this.getLocale() }),
                (function () {
                    if (isResizing == true) {
                        return React.createElement("div", { style: { position: "absolute", left: 0, right: 0, top: 0, bottom: 0, backgroundColor: task_pane_1.TASK_PANE_OVERLAY_BGCOLOR } },
                            React.createElement(core_1.NonIdealState, { icon: "arrows-horizontal", description: (0, i18n_1.tr)("TASK_PANE_RESIZING", _this.getLocale()) }));
                    }
                })());
        }
        return React.createElement("noscript", null);
    };
    return TaskPaneContainerInner;
}(React.Component));
var TaskPaneContainer = function (props) {
    var locale = (0, hooks_1.useViewerLocale)();
    var map = (0, hooks_1.useActiveMapBranch)();
    var flyouts = (0, hooks_1.useViewerFlyouts)();
    var initialUrl = (0, hooks_1.useTaskPaneInitialUrl)();
    var lastUrlPushed = (0, hooks_1.useTaskPaneLastUrlPushed)();
    var navIndex = (0, hooks_1.useTaskPaneNavigationIndex)();
    var navigationStack = (0, hooks_1.useTaskPaneNavigationStack)();
    var hasTaskBar = (0, hooks_1.useConfiguredCapabilities)().hasTaskBar;
    var dispatch = (0, context_1.useReduxDispatch)();
    var invokeCommandAction = function (cmd, parameters) { return dispatch((0, map_1.invokeCommand)(cmd, parameters)); };
    var goHomeAction = function () { return dispatch((0, taskpane_1.goHome)()); };
    var goForwardAction = function () { return dispatch((0, taskpane_1.goForward)()); };
    var goBackAction = function () { return dispatch((0, taskpane_1.goBack)()); };
    var pushUrlAction = function (url, silent) { return dispatch((0, taskpane_1.pushUrl)(url, silent)); };
    var openFlyoutAction = function (id, metrics) { return dispatch((0, flyout_1.openFlyout)(id, metrics)); };
    var closeFlyoutAction = function (id) { return dispatch((0, flyout_1.closeFlyout)(id)); };
    return React.createElement(TaskPaneContainerInner, tslib_1.__assign({ map: map, locale: locale, flyouts: flyouts, initialUrl: initialUrl, lastUrlPushed: lastUrlPushed, navIndex: navIndex, navigationStack: navigationStack, invokeCommand: invokeCommandAction, hasTaskBar: hasTaskBar, goHome: goHomeAction, goForward: goForwardAction, goBack: goBackAction, pushUrl: pushUrlAction, openFlyout: openFlyoutAction, closeFlyout: closeFlyoutAction }, props));
};
exports.TaskPaneContainer = TaskPaneContainer;
//# sourceMappingURL=task-pane.js.map