(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "../../di/TypeKey"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.AtomStyleSheet = void 0;
    var TypeKey_1 = require("../../di/TypeKey");
    var AtomStyleSheet = /** @class */ (function () {
        function AtomStyleSheet(app, name) {
            this.app = app;
            this.name = name;
            this.styles = {};
            this.lastUpdateId = 0;
            this.isAttaching = false;
            this.pushUpdate(0);
        }
        AtomStyleSheet.prototype.getNamedStyle = function (c) {
            var name = TypeKey_1.TypeKey.get(c);
            return this.createNamedStyle(c, name);
        };
        AtomStyleSheet.prototype.createNamedStyle = function (c, name, updateTimeout) {
            var style = this.styles[name] = new (c)(this, this.name + "-" + name);
            style.build();
            this.pushUpdate(updateTimeout);
            return style;
        };
        AtomStyleSheet.prototype.onPropertyChanging = function (name, newValue, oldValue) {
            this.pushUpdate();
        };
        AtomStyleSheet.prototype.pushUpdate = function (delay) {
            var _this = this;
            if (delay === void 0) { delay = 1; }
            if (this.isAttaching) {
                return;
            }
            // special case for Xamarin Forms
            if (delay === 0) {
                this.attach();
                return;
            }
            if (this.lastUpdateId) {
                clearTimeout(this.lastUpdateId);
            }
            this.lastUpdateId = setTimeout(function () {
                _this.attach();
            }, delay);
        };
        AtomStyleSheet.prototype.dispose = function () {
            if (this.styleElement) {
                this.styleElement.remove();
            }
        };
        AtomStyleSheet.prototype.attach = function () {
            this.isAttaching = true;
            var text = [];
            for (var key in this.styles) {
                if (this.styles.hasOwnProperty(key)) {
                    var element = this.styles[key];
                    text.push(element.toString());
                }
            }
            var textContent = text.join("\n");
            this.app.updateDefaultStyle(textContent);
            this.isAttaching = false;
        };
        return AtomStyleSheet;
    }());
    exports.AtomStyleSheet = AtomStyleSheet;
});
//# sourceMappingURL=AtomStyleSheet.js.map