var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
(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", "./AtomStyle"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var AtomStyle_1 = require("./AtomStyle");
    var AtomStyleSheet = /** @class */ (function (_super) {
        __extends(AtomStyleSheet, _super);
        function AtomStyleSheet(app, prefix) {
            var _this = _super.call(this, null, null, prefix) || this;
            _this.app = app;
            _this.lastUpdateId = 0;
            _this.isAttaching = false;
            _this.styleSheet = _this;
            _this.pushUpdate(0);
            return _this;
        }
        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;
            }
            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 pairs = this.toStyle({});
            var textContent = this.flatten(pairs);
            if (this.styleElement) {
                if (this.styleElement.textContent === textContent) {
                    this.isAttaching = false;
                    return;
                }
            }
            var ss = document.createElement("style");
            ss.textContent = textContent;
            if (this.styleElement) {
                this.styleElement.remove();
            }
            document.head.appendChild(ss);
            this.styleElement = ss;
            this.isAttaching = false;
        };
        AtomStyleSheet.prototype.build = function () {
            // do nothing..
        };
        AtomStyleSheet.prototype.flatten = function (pairs) {
            var sl = [];
            for (var key in pairs) {
                if (pairs.hasOwnProperty(key)) {
                    var element = pairs[key];
                    sl.push("." + key + " " + element + " ");
                }
            }
            return sl.join("\r\n");
        };
        return AtomStyleSheet;
    }(AtomStyle_1.AtomStyle));
    exports.AtomStyleSheet = AtomStyleSheet;
});
//# sourceMappingURL=AtomStyleSheet.js.map