"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var dimensions_1 = require("@dojo/framework/core/middleware/dimensions");
var vdom_1 = require("@dojo/framework/core/vdom");
var popup_1 = require("../popup");
var fixedCss = require("./trigger-popup.m.css");
var theme_1 = require("../middleware/theme");
var icache_1 = require("@dojo/framework/core/middleware/icache");
var icache = icache_1.createICacheMiddleware();
var factory = vdom_1.create({ dimensions: dimensions_1.dimensions, icache: icache, theme: theme_1.default })
    .properties()
    .children();
exports.TriggerPopup = factory(function TriggerPopup(_a) {
    var properties = _a.properties, children = _a.children, _b = _a.middleware, dimensions = _b.dimensions, icache = _b.icache, theme = _b.theme;
    var _c = properties(), _d = _c.matchWidth, matchWidth = _d === void 0 ? true : _d, onOpen = _c.onOpen, classes = _c.classes, otherProperties = tslib_1.__rest(_c, ["matchWidth", "onOpen", "classes"]);
    var _e = dimensions.get('trigger'), triggerPosition = _e.position, triggerSize = _e.size;
    var triggerTop = triggerPosition.top + document.documentElement.scrollTop;
    var triggerBottom = triggerTop + triggerSize.height;
    var themedCss = theme.classes(fixedCss);
    var wrapperStyles = {
        width: matchWidth ? triggerSize.width + "px" : 'auto'
    };
    var _f = children()[0], trigger = _f.trigger, content = _f.content;
    var close = function () {
        var onClose = properties().onClose;
        icache.set('open', false);
        onClose && onClose();
    };
    return (vdom_1.tsx("virtual", { classes: [themedCss.root] },
        vdom_1.tsx("span", { key: "trigger", classes: fixedCss.trigger }, trigger(function () {
            var onOpen = properties().onOpen;
            icache.set('open', !icache.get('open'));
            onOpen && onOpen();
        })),
        vdom_1.tsx(popup_1.default, tslib_1.__assign({ key: "popup" }, otherProperties, { classes: classes, yTop: triggerBottom, yBottom: triggerTop, xLeft: triggerPosition.left, xRight: triggerPosition.right, onClose: close, open: icache.get('open') }), function (position) { return (vdom_1.tsx("div", { key: "trigger-wrapper", styles: wrapperStyles }, content(close, position))); })));
});
exports.default = exports.TriggerPopup;

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