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 });
    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;
            });
        };
        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;
            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");
        };
        return AtomElementBridge;
    }(BaseElementBridge));
    exports.AtomElementBridge = AtomElementBridge;
    var AtomBridge = /** @class */ (function () {
        function AtomBridge() {
        }
        AtomBridge.create = function (name) {
            return this.instance.create(name);
        };
        AtomBridge.instance = new AtomElementBridge();
        return AtomBridge;
    }());
    exports.AtomBridge = AtomBridge;
});
//# sourceMappingURL=AtomBridge.js.map