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", "../web/core/AtomUI", "./AtomBinder"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.AtomBridge = exports.AtomElementBridge = exports.BaseElementBridge = void 0;
    var AtomUI_1 = require("../web/core/AtomUI");
    var AtomBinder_1 = require("./AtomBinder");
    var BaseElementBridge = /** @class */ (function () {
        function BaseElementBridge() {
        }
        BaseElementBridge.prototype.refreshInherited = function (target, name, fieldName) {
            var _this = this;
            AtomBinder_1.AtomBinder.refreshValue(target, name);
            if (!fieldName) {
                fieldName = "m" + name[0].toUpperCase() + name.substr(1);
            }
            this.visitDescendents(target.element, function (e, ac) {
                if (ac) {
                    if (ac[fieldName] === undefined) {
                        _this.refreshInherited(ac, name, fieldName);
                    }
                    return false;
                }
                return true;
            });
        };
        BaseElementBridge.prototype.createNode = function (target, node, 
        // tslint:disable-next-line: ban-types
        binder, 
        // tslint:disable-next-line: ban-types
        xNodeClass, 
        // tslint:disable-next-line: ban-types
        creator) {
            throw new Error("Method not implemented.");
        };
        return BaseElementBridge;
    }());
    exports.BaseElementBridge = BaseElementBridge;
    var AtomElementBridge = /** @class */ (function (_super) {
        __extends(AtomElementBridge, _super);
        function AtomElementBridge() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        AtomElementBridge.prototype.addEventHandler = function (element, name, handler, capture) {
            element.addEventListener(name, handler, capture);
            return {
                dispose: function () {
                    element.removeEventListener(name, handler, capture);
                }
            };
        };
        AtomElementBridge.prototype.atomParent = function (element, climbUp) {
            if (climbUp === void 0) { climbUp = true; }
            var eAny = element;
            if (eAny.atomControl) {
                return eAny.atomControl;
            }
            if (!climbUp) {
                return null;
            }
            if (!element.parentNode) {
                return null;
            }
            return this.atomParent(this.elementParent(element));
        };
        AtomElementBridge.prototype.elementParent = function (element) {
            var eAny = element;
            var lp = eAny._logicalParent;
            if (lp) {
                return lp;
            }
            return element.parentElement;
        };
        AtomElementBridge.prototype.templateParent = function (element) {
            if (!element) {
                return null;
            }
            var eAny = element;
            if (eAny._templateParent) {
                return this.atomParent(element);
            }
            var parent = this.elementParent(element);
            if (!parent) {
                return null;
            }
            return this.templateParent(parent);
        };
        AtomElementBridge.prototype.visitDescendents = function (element, action) {
            var en = new AtomUI_1.ChildEnumerator(element);
            while (en.next()) {
                var iterator = en.current;
                var eAny = iterator;
                var ac = eAny ? eAny.atomControl : undefined;
                if (!action(iterator, ac)) {
                    continue;
                }
                this.visitDescendents(iterator, action);
            }
        };
        AtomElementBridge.prototype.dispose = function (element) {
            var eAny = element;
            eAny.atomControl = undefined;
            eAny.innerHTML = "";
            delete eAny.atomControl;
        };
        AtomElementBridge.prototype.appendChild = function (parent, child) {
            parent.appendChild(child);
        };
        AtomElementBridge.prototype.setValue = function (element, name, value) {
            element[name] = value;
        };
        AtomElementBridge.prototype.getValue = function (element, name) {
            return element[name];
        };
        AtomElementBridge.prototype.watchProperty = function (element, name, events, f) {
            if (events.indexOf("change") === -1) {
                events.push("change");
            }
            var l = function (e) {
                var e1 = element;
                var v = e1.type === "checkbox" ? e1.checked : e1.value;
                f(v);
            };
            for (var _i = 0, events_1 = events; _i < events_1.length; _i++) {
                var iterator = events_1[_i];
                element.addEventListener(iterator, l, false);
            }
            return {
                dispose: function () {
                    for (var _i = 0, events_2 = events; _i < events_2.length; _i++) {
                        var iterator = events_2[_i];
                        element.removeEventListener(iterator, l, false);
                    }
                }
            };
        };
        AtomElementBridge.prototype.attachControl = function (element, control) {
            element.atomControl = control;
        };
        AtomElementBridge.prototype.create = function (type) {
            return document.createElement(type);
        };
        AtomElementBridge.prototype.loadContent = function (element, text) {
            throw new Error("Not supported");
        };
        AtomElementBridge.prototype.findChild = function (element, name) {
            throw new Error("Not supported");
        };
        AtomElementBridge.prototype.close = function (element, success, error) {
            throw new Error("Not supported");
        };
        AtomElementBridge.prototype.toTemplate = function (element, creator) {
            var templateNode = element;
            var name = templateNode.name;
            if (typeof name === "string") {
                element = (function (bx, n) { return /** @class */ (function (_super) {
                    __extends(class_1, _super);
                    function class_1() {
                        return _super !== null && _super.apply(this, arguments) || this;
                    }
                    class_1.prototype.create = function () {
                        this.render(n);
                    };
                    return class_1;
                }(bx)); })(creator, templateNode.children[0]);
            }
            else {
                element = (function (base, n) { return /** @class */ (function (_super) {
                    __extends(class_2, _super);
                    function class_2() {
                        return _super !== null && _super.apply(this, arguments) || this;
                    }
                    class_2.prototype.create = function () {
                        this.render(n);
                    };
                    return class_2;
                }(base)); })(name, templateNode.children[0]);
            }
            return element;
        };
        AtomElementBridge.prototype.createNode = function (target, node, 
        // tslint:disable-next-line: ban-types
        binder, 
        // tslint:disable-next-line: ban-types
        xNodeClass, 
        // tslint:disable-next-line: ban-types
        creator) {
            var parent = null;
            var app = target.app;
            var e = null;
            var nn = node.attributes ? node.attributes.for : undefined;
            if (typeof node.name === "string") {
                // it is simple node..
                e = document.createElement(node.name);
                parent = e;
                if (nn) {
                    delete node.attributes.for;
                }
            }
            else {
                if (nn) {
                    target = new node.name(app, document.createElement(nn));
                    delete node.attributes.for;
                }
                target = new node.name(app);
                e = target.element;
                parent = target;
                // target.append(child);
                // const firstChild = node.children ? node.children[0] : null;
                // if (firstChild) {
                // 	const n = this.createNode(child, firstChild, binder, xNodeClass, creator);
                // 	child.append(n.atomControl || n);
                // }
                // return child.element;
            }
            var a = node.attributes;
            if (a) {
                for (var key in a) {
                    if (a.hasOwnProperty(key)) {
                        var element = a[key];
                        if (element instanceof binder) {
                            if (/^event/.test(key)) {
                                var ev = key.substr(5);
                                if (ev.startsWith("-")) {
                                    ev = ev.split("-").map(function (s) { return s[0].toLowerCase() + s.substr(1); }).join("");
                                }
                                else {
                                    ev = ev[0].toLowerCase() + ev.substr(1);
                                }
                                element.setupFunction(ev, element, target, e);
                            }
                            else {
                                element.setupFunction(key, element, target, e);
                            }
                        }
                        else {
                            // this is template...
                            if (element instanceof xNodeClass) {
                                element = this.toTemplate(element, creator);
                            }
                            target.setLocalValue(e, key, element);
                        }
                    }
                }
            }
            var children = node.children;
            if (children) {
                for (var _i = 0, children_1 = children; _i < children_1.length; _i++) {
                    var iterator = children_1[_i];
                    if (typeof iterator === "string") {
                        e.appendChild(document.createTextNode(iterator));
                        continue;
                    }
                    var t = iterator.attributes ? iterator.attributes.template : null;
                    if (t) {
                        var tx = this.toTemplate(iterator, creator);
                        target[t] = tx;
                        continue;
                    }
                    if (typeof iterator.name === "string") {
                        e.appendChild(this.createNode(target, iterator, binder, xNodeClass, creator));
                        continue;
                    }
                    var child = this.createNode(target, iterator, binder, xNodeClass, creator);
                    if (parent.element && parent.element.atomControl === parent) {
                        parent.append(child.atomControl || child);
                    }
                    else {
                        parent.appendChild(child);
                    }
                }
            }
            return e;
        };
        return AtomElementBridge;
    }(BaseElementBridge));
    exports.AtomElementBridge = AtomElementBridge;
    var AtomBridge = /** @class */ (function () {
        function AtomBridge() {
        }
        AtomBridge.createNode = function (iterator, app) {
            if (typeof iterator.name !== "function") {
                return { element: AtomBridge.instance.create(iterator.name.toString(), iterator, app) };
            }
            var fx = iterator.attributes ? iterator.attributes.for : undefined;
            var c = new iterator.name(app, fx ? AtomBridge.instance.create(fx, iterator, app) : undefined);
            return { element: c.element, control: c };
        };
        AtomBridge.toTemplate = function (app, n, creator) {
            if (n.isTemplate) {
                var t = AtomBridge.toTemplate(app, n.children[0], creator);
                return AtomBridge.instance.create(n.name.toString(), t, app);
            }
            var bridge = AtomBridge.instance;
            var fx;
            var en;
            if (typeof n.name === "function") {
                fx = n.name;
                en = (n.attributes && n.attributes.for) ? n.attributes.for : undefined;
            }
            else {
                fx = bridge.controlFactory;
                en = n.name;
            }
            return /** @class */ (function (_super) {
                __extends(Template, _super);
                function Template(a, e1) {
                    var _this = _super.call(this, a || app, e1 || (en ? bridge.create(en, null, app) : undefined)) || this;
                    // tslint:disable-next-line: variable-name
                    _this._creator = fx;
                    return _this;
                }
                Template.prototype.create = function () {
                    _super.prototype.create.call(this);
                    this.render(n, null, creator);
                };
                return Template;
            }(fx));
        };
        AtomBridge.refreshInherited = function (target, name, fieldName) {
            if (AtomBridge.instance.refreshInherited) {
                AtomBridge.instance.refreshInherited(target, name, fieldName);
                return;
            }
            AtomBinder_1.AtomBinder.refreshValue(target, name);
            if (!fieldName) {
                fieldName = "m" + name[0].toUpperCase() + name.substr(1);
            }
            AtomBridge.instance.visitDescendents(target.element, function (e, ac) {
                if (ac) {
                    if (ac[fieldName] === undefined) {
                        AtomBridge.refreshInherited(ac, name, fieldName);
                    }
                    return false;
                }
                return true;
            });
        };
        return AtomBridge;
    }());
    exports.AtomBridge = AtomBridge;
    var globalNS = (typeof window !== "undefined" ? window : global);
    globalNS.AtomBridge = AtomBridge;
    if (typeof window !== "undefined") {
        AtomBridge.instance = new AtomElementBridge();
        AtomBridge.platform = "web";
    }
});
//# sourceMappingURL=AtomBridge.js.map