"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var i18n_1 = require("@dojo/framework/core/middleware/i18n");
var icache_1 = require("@dojo/framework/core/middleware/icache");
var util_1 = require("@dojo/framework/core/util");
var vdom_1 = require("@dojo/framework/core/vdom");
var util_2 = require("../common/util");
var icon_1 = require("../icon");
var theme_1 = require("../middleware/theme");
var bodyScroll_1 = require("../middleware/bodyScroll");
var css = require("../theme/default/dialog.m.css");
var fixedCss = require("./styles/dialog.m.css");
var Dialog_1 = require("./nls/Dialog");
var global_event_1 = require("../global-event");
var inert_1 = require("@dojo/framework/core/middleware/inert");
var factory = vdom_1.create({
    theme: theme_1.default,
    i18n: i18n_1.default,
    icache: icache_1.createICacheMiddleware(),
    inert: inert_1.default,
    bodyScroll: bodyScroll_1.default
})
    .properties()
    .children();
exports.Dialog = factory(function Dialog(_a) {
    var _b = _a.middleware, theme = _b.theme, i18n = _b.i18n, icache = _b.icache, inert = _b.inert, bodyScroll = _b.bodyScroll, properties = _a.properties, children = _a.children;
    var themeCss = theme.classes(css);
    var _c = properties(), open = _c.open, _d = _c.aria, aria = _d === void 0 ? {} : _d, underlay = _c.underlay, _e = _c.role, role = _e === void 0 ? 'dialog' : _e, _f = _c.closeable, closeable = _f === void 0 ? true : _f, closeText = _c.closeText, classes = _c.classes, themeProp = _c.theme, variant = _c.variant;
    var _g = tslib_1.__read(children(), 1), _h = _g[0], title = _h.title, actions = _h.actions, content = _h.content;
    var modal = role === 'alertdialog' || properties().modal;
    inert.set('dialog', open, true);
    if (!closeText) {
        var messages = i18n.localize(Dialog_1.default).messages;
        closeText = messages.close + " " + (title || '');
    }
    var wasOpen = icache.getOrSet('wasOpen', false);
    var titleId = icache.getOrSet('titleId', util_1.uuid());
    var contentId = icache.getOrSet('contentId', util_1.uuid());
    var onOpen = function () {
        var onOpen = properties().onOpen;
        icache.set('callFocus', true);
        onOpen && onOpen();
    };
    var close = function () {
        var _a = properties(), _b = _a.closeable, closeable = _b === void 0 ? true : _b, onRequestClose = _a.onRequestClose;
        closeable && onRequestClose();
    };
    open && !wasOpen && onOpen();
    if (wasOpen !== open) {
        icache.set('wasOpen', open);
    }
    var callFocus = icache.getOrSet('callFocus', false);
    if (callFocus) {
        icache.set('callFocus', false);
    }
    var keyup = function (event) {
        event.stopPropagation();
        if (event.which === util_2.Keys.Escape) {
            close();
        }
    };
    bodyScroll(!open);
    return (vdom_1.tsx("body", null,
        vdom_1.tsx("div", { key: "dialog", classes: [theme.variant(), themeCss.root, open ? themeCss.open : null] }, open && (vdom_1.tsx("virtual", null,
            vdom_1.tsx(global_event_1.default, { key: "global", document: { keyup: keyup } }),
            vdom_1.tsx("div", { classes: [
                    underlay ? themeCss.underlayVisible : null,
                    fixedCss.underlay
                ], enterAnimation: themeCss.underlayEnter, exitAnimation: themeCss.underlayExit, key: "underlay", onclick: function (event) {
                    var role = properties().role;
                    var modal = role === 'alertdialog' ||
                        properties().modal;
                    event.stopPropagation();
                    !modal && close();
                } }),
            vdom_1.tsx("div", tslib_1.__assign({ "aria-labelledby": titleId, "aria-modal": modal ? 'true' : 'false', "aria-describedby": role === 'alertdialog' ? contentId : undefined }, util_2.formatAriaProperties(aria), { classes: [themeCss.main, fixedCss.main], enterAnimation: themeCss.enter, exitAnimation: themeCss.exit, key: "main", role: role, tabIndex: -1, focus: callFocus }),
                vdom_1.tsx("div", { classes: themeCss.title, key: "title", id: titleId },
                    vdom_1.tsx("div", null, title),
                    closeable && (vdom_1.tsx("button", { classes: themeCss.close, type: "button", onclick: function (event) {
                            event.stopPropagation();
                            close();
                        } },
                        closeText,
                        vdom_1.tsx("span", { classes: themeCss.closeIcon },
                            vdom_1.tsx(icon_1.default, { theme: themeProp, classes: classes, variant: variant, type: "closeIcon" }))))),
                vdom_1.tsx("div", { classes: themeCss.content, key: "content", id: contentId }, content),
                actions && (vdom_1.tsx("div", { classes: themeCss.actions, key: "actions" }, actions))))))));
});
exports.default = exports.Dialog;

/*# sourceMappingURL=index.js.map*/