var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
(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", "../../core/AtomBinder", "../../core/StringHelper", "../../core/WebImage", "../../di/TypeKey"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var AtomBinder_1 = require("../../core/AtomBinder");
    var StringHelper_1 = require("../../core/StringHelper");
    var WebImage_1 = require("../../core/WebImage");
    var TypeKey_1 = require("../../di/TypeKey");
    var emptyPrototype = Object.getPrototypeOf({});
    var AtomStyle = /** @class */ (function () {
        function AtomStyle(styleSheet, parent, name) {
            this.styleSheet = styleSheet;
            this.parent = parent;
            this.name = name;
            this.defaults = {};
            this.isBuilt = false;
            this.init();
        }
        AtomStyle.prototype.getDefaultStyle = function (forKey) {
            return this.defaults[TypeKey_1.TypeKey.get(forKey)];
        };
        AtomStyle.prototype.createNamedStyle = function (c, name) {
            var style = this[name] = new (c)(this.styleSheet, this, this.name + "-" + name);
            style.build();
            return style;
        };
        AtomStyle.prototype.createStyle = function (tc, c, name) {
            this.defaults = this.defaults || {};
            var newStyle = new (c)(this.styleSheet, this, this.name + "-" + name);
            var key = TypeKey_1.TypeKey.get(tc);
            this.defaults[key] = newStyle;
            newStyle.build();
            return this[name] = newStyle;
        };
        AtomStyle.prototype.getBaseProperty = function (tc, name) {
            var c = tc;
            do {
                c = Object.getPrototypeOf(c);
                if (!c) {
                    throw new Error("No property descriptor found for " + name);
                }
                var pd = Object.getOwnPropertyDescriptor(c.prototype, name);
                if (!pd) {
                    continue;
                }
                return pd.get.apply(this);
            } while (true);
        };
        AtomStyle.prototype.toStyle = function (pairs) {
            pairs = pairs || {};
            var self = this;
            for (var key in self) {
                if (/^(isBuilt|constructor|name|parent|styleSheet|defaults|theme|styleElement)$/.test(key)) {
                    continue;
                }
                if (/^\_/.test(key)) {
                    continue;
                }
                var element = self[key];
                // if it is nested style
                if (element instanceof AtomStyle) {
                    pairs = element.toStyle(pairs);
                    continue;
                }
                // if it is class
                var c = element;
                if (c && typeof c === "object") {
                    if (emptyPrototype === Object.getPrototypeOf(c)) {
                        pairs = this.createStyleText(key, pairs, c);
                    }
                    continue;
                }
            }
            return pairs;
        };
        AtomStyle.prototype.toFullName = function (n) {
            return this.name + "-" + StringHelper_1.StringHelper.fromCamelToHyphen(n);
        };
        AtomStyle.prototype.build = function () {
            if (this.isBuilt) {
                return;
            }
            this.isBuilt = true;
            this.styleSheet.pushUpdate();
            var self = this;
            var _loop_1 = function (key) {
                if (/^(isBuilt|constructor|name|parent|styleSheet|defaults|theme|styleElement)$/.test(key)) {
                    return "continue";
                }
                if (/^\_\$\_/.test(key)) {
                    return "continue";
                }
                var element = self[key];
                if (element instanceof AtomStyle) {
                    element.build();
                    return "continue";
                }
                var c = element;
                if (c && typeof c === "object") {
                    if (emptyPrototype === Object.getPrototypeOf(c)) {
                        var pv_1 = AtomBinder_1.AtomBinder.getPropertyDescriptor(this_1, key);
                        if (!pv_1.get) {
                            return "continue";
                        }
                        var fullName_1 = this_1.toFullName(key);
                        var descriptor = {
                            get: function () {
                                return __assign(__assign({}, pv_1.get.apply(this)), { className: fullName_1, toString: function () { return fullName_1; } });
                            }, configurable: true, enumerable: true
                        };
                        Object.defineProperty(this_1, key, descriptor);
                    }
                }
            };
            var this_1 = this;
            for (var key in self) {
                _loop_1(key);
            }
            this.isBuilt = true;
        };
        AtomStyle.prototype.init = function () {
            // empty...
        };
        AtomStyle.prototype.registerExternalStyleSheet = function (s) {
            var wa = this.styleSheet.app;
            wa.installStyleSheet(s);
        };
        AtomStyle.prototype.createStyleText = function (name, pairs, styles) {
            var styleList = [];
            for (var key in styles) {
                if (styles.hasOwnProperty(key)) {
                    var element = styles[key];
                    if (element === undefined || element === null || key === "className") {
                        continue;
                    }
                    var keyName = StringHelper_1.StringHelper.fromCamelToHyphen(key);
                    if (key === "subclasses") {
                        for (var subclassKey in element) {
                            if (element.hasOwnProperty(subclassKey)) {
                                var ve = element[subclassKey];
                                pairs = this.createStyleText("" + name + subclassKey, pairs, ve);
                            }
                        }
                    }
                    else {
                        if (element instanceof WebImage_1.default) {
                            styleList.push(keyName + ": url(" + element + ")");
                        }
                        else {
                            styleList.push(keyName + ": " + element);
                        }
                    }
                }
            }
            var cname = StringHelper_1.StringHelper.fromCamelToHyphen(name);
            var styleClassName = this.name + "-" + cname;
            pairs[styleClassName] = "{ " + styleList.join(";\r\n") + " }";
            // styles.className = styleClassName;
            // styles.toString = () => styleClassName;
            return pairs;
        };
        return AtomStyle;
    }());
    exports.AtomStyle = AtomStyle;
});
//# sourceMappingURL=AtomStyle.js.map