"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplicationViewModel = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var ReactDOM = tslib_1.__importStar(require("react-dom"));
var app_1 = require("../containers/app");
var configure_store_1 = require("../store/configure-store");
var config_1 = require("../reducers/config");
var command_1 = require("../api/registry/command");
var subscriber_1 = require("../containers/subscriber");
var context_1 = require("../components/map-providers/context");
var init_mapguide_1 = require("../actions/init-mapguide");
/**
 * This is the entry point to the Application component
 *
 * In the browser globals context, this is accessible via MapGuide.Application
 */
var ApplicationViewModel = /** @class */ (function () {
    /**
     * @hidden
     */
    function ApplicationViewModel() {
    }
    /**
     * Returns any extra initial state to include as part of initializing the redux store
     *
     * Overridable by sub-classes that want to include extra initial state
     *
     * @virtual
     * @protected
     * @returns {*}
     * @memberof ApplicationViewModel
     */
    ApplicationViewModel.prototype.getExtraInitialState = function () { return {}; };
    /**
     * Returns any extra reducers to include as part of initializing the redux store
     *
     * Overridable by sub-classes that want to include custom reducers
     *
     * @virtual
     * @protected
     * @returns {*}
     * @memberof ApplicationViewModel
     */
    ApplicationViewModel.prototype.getExtraReducers = function () { return {}; };
    /**
     * Mounts the map viewer application at the specified DOM element with the
     * given component props.
     *
     * For the viewer templates already provided, this method is already called
     * for you in the template's HTML. If you are creating your own viewer template, be
     * sure to call this method must on the template's HTML.
     *
     * @param {Element} node
     * @param {IAppProps & IApplicationMountOptions} props
     *
     * @memberof ApplicationViewModel
     */
    ApplicationViewModel.prototype.mount = function (node, props) {
        var _a, _b, _c, _d;
        var subs = (_a = props.subscribers) !== null && _a !== void 0 ? _a : [];
        var agentConf = {
            agentUri: (_b = props.mapguide) === null || _b === void 0 ? void 0 : _b.agentUri,
            agentKind: (_d = (_c = props.mapguide) === null || _c === void 0 ? void 0 : _c.agentKind) !== null && _d !== void 0 ? _d : "mapagent"
        };
        var initState = tslib_1.__assign({ config: tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, config_1.CONFIG_INITIAL_STATE), agentConf), (props.initialConfig || {})) }, this.getExtraInitialState());
        var extraReducers = this.getExtraReducers();
        this._store = (0, configure_store_1.configureStore)(initState, extraReducers);
        var initCommand;
        if (props.initCommandFactory)
            initCommand = props.initCommandFactory(this._store.dispatch);
        else
            initCommand = new init_mapguide_1.DefaultViewerInitCommand(this._store.dispatch);
        ReactDOM.render(React.createElement(context_1.ReduxProvider, { store: this._store },
            React.createElement(app_1.App, tslib_1.__assign({}, props, { initCommand: initCommand })),
            subs.map(function (s, i) { return React.createElement(subscriber_1.Subscriber, tslib_1.__assign({ key: "subscriber-".concat(i, "-").concat(s.name) }, s)); })), node);
    };
    /**
     * Dispatches the given action
     *
     * @param {(ViewerAction | ReduxThunkedAction)} action
     * @memberof ApplicationViewModel
     * @remarks Usage outside of the react component context should be used sparingly. In particular
     * you should avoid trying to call this method multiple times in succession. You should call this
     * method once in response to a DOM element event (eg. A button click)
     * @alpha
     */
    ApplicationViewModel.prototype.dispatch = function (action) {
        this._store.dispatch(action);
    };
    /**
     * Gets the command registered by the specific name
     *
     * @param {string} commandName
     * @returns {(ICommand | undefined)}
     * @memberof ApplicationViewModel
     */
    ApplicationViewModel.prototype.getCommand = function (commandName) {
        return (0, command_1.getCommand)(commandName);
    };
    /**
     * Returns the current application state. This state is read-only and should not be modified.
     *
     * @returns {Readonly<IApplicationState>}
     * @memberof ApplicationViewModel
     */
    ApplicationViewModel.prototype.getState = function () {
        return this._store.getState();
    };
    return ApplicationViewModel;
}());
exports.ApplicationViewModel = ApplicationViewModel;
//# sourceMappingURL=application.js.map