"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModalLauncher = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var modal_dialog_1 = require("../components/modal-dialog");
var component_1 = require("../api/registry/component");
var error_1 = require("../components/error");
var i18n_1 = require("../api/i18n");
var common_1 = require("../api/common");
var type_guards_1 = require("../utils/type-guards");
var never_1 = require("../utils/never");
var url_1 = require("../utils/url");
var hooks_1 = require("./hooks");
var modal_1 = require("../actions/modal");
var context_1 = require("../components/map-providers/context");
function getComponentId(diag) {
    if ((0, type_guards_1.isModalComponentDisplayOptions)(diag)) {
        return { name: diag.component, props: {} };
    }
    else if ((0, type_guards_1.isModalDisplayOptions)(diag)) {
        return (0, url_1.parseComponentUri)(diag.url);
    }
    else {
        (0, never_1.assertNever)(diag);
    }
}
var ModalLauncher = function (props) {
    var dispatch = (0, context_1.useReduxDispatch)();
    var hideModalAction = function (name) { return dispatch((0, modal_1.hideModal)(name)); };
    var onCloseModal = function (name) { return hideModalAction(name); };
    var modal = (0, context_1.useAppState)(function (state) { return state.modal; });
    var locale = (0, hooks_1.useViewerLocale)();
    var onModalChanged = function (name, args) {
        dispatch((0, modal_1.updateModal)(name, args));
    };
    if (!modal) {
        return React.createElement("noscript", null);
    }
    return React.createElement("div", null,
        Object.keys(modal).map(function (key) {
            var _a, _b;
            var diag = modal[key];
            var pos = (_a = diag.modal.position) !== null && _a !== void 0 ? _a : common_1.DEFAULT_MODAL_POSITION;
            var size = (_b = diag.modal.size) !== null && _b !== void 0 ? _b : common_1.DEFAULT_MODAL_SIZE;
            if ((0, type_guards_1.isModalComponentDisplayOptions)(diag) || ((0, type_guards_1.isModalDisplayOptions)(diag) && (0, url_1.isComponentUri)(diag.url))) {
                var componentId_1 = getComponentId(diag);
                if (componentId_1) {
                    var componentRenderer_1 = (0, component_1.getComponentFactory)(componentId_1.name);
                    return React.createElement(modal_dialog_1.RndModalDialog, { title: diag.modal.title, x: pos[0], y: pos[1], locale: locale, enableInteractionMask: true, width: size[0], height: size[1], disableYOverflow: !diag.modal.overflowYScroll, isOpen: true, key: key, onChange: function (args) { return onModalChanged(key, args); }, onClose: function () { return onCloseModal(key); } }, function (_a) {
                        if (componentRenderer_1) {
                            if ((0, type_guards_1.isModalComponentDisplayOptions)(diag))
                                return componentRenderer_1(diag.componentProps);
                            else
                                return componentRenderer_1(componentId_1.props);
                        }
                        else {
                            return React.createElement(error_1.Error, { error: (0, i18n_1.tr)("ERR_UNREGISTERED_COMPONENT", locale, { componentId: componentId_1 }) });
                        }
                    });
                }
                else {
                    return React.createElement(error_1.Error, { error: (0, i18n_1.tr)("ERR_NO_COMPONENT_ID", locale) });
                }
            }
            else if ((0, type_guards_1.isModalDisplayOptions)(diag)) {
                return React.createElement(modal_dialog_1.RndModalDialog, { title: diag.modal.title, isOpen: true, key: key, x: pos[0], y: pos[1], locale: locale, enableInteractionMask: false, width: size[0], height: size[1], onChange: function (args) { return onModalChanged(key, args); }, onClose: function () { return onCloseModal(key); } }, function (_a) {
                    var w = _a[0], h = _a[1];
                    return React.createElement("iframe", { frameBorder: 0, src: diag.url, width: w, height: h });
                });
            } /* else {
                assertNever(diag);
            }*/
        }),
        props.children);
};
exports.ModalLauncher = ModalLauncher;
//# sourceMappingURL=modal-launcher.js.map