(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;
    const TypeKey_1 = require("../../di/TypeKey");
    class AtomStyleSheet {
        constructor(app, name) {
            this.app = app;
            this.name = name;
            this.styles = {};
            this.lastUpdateId = 0;
            this.isAttaching = false;
            this.pushUpdate(0);
        }
        getNamedStyle(c) {
            const name = TypeKey_1.TypeKey.get(c);
            return this.createNamedStyle(c, name);
        }
        createNamedStyle(c, name, updateTimeout) {
            const style = this.styles[name] = new (c)(this, `${this.name}-${name}`);
            style.build();
            this.pushUpdate(updateTimeout);
            return style;
        }
        onPropertyChanging(name, newValue, oldValue) {
            this.pushUpdate();
        }
        pushUpdate(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(() => {
                this.attach();
            }, delay);
        }
        dispose() {
            if (this.styleElement) {
                this.styleElement.remove();
            }
        }
        attach() {
            this.isAttaching = true;
            const text = [];
            for (const key in this.styles) {
                if (this.styles.hasOwnProperty(key)) {
                    const element = this.styles[key];
                    text.push(element.toString());
                }
            }
            const textContent = text.join("\n");
            this.app.updateDefaultStyle(textContent);
            this.isAttaching = false;
        }
    }
    exports.AtomStyleSheet = AtomStyleSheet;
});
//# sourceMappingURL=AtomStyleSheet.js.map