/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { List$1 } from "./List$1";
import { String_$type, TypeRegistrar } from './type';
import { fromSpinal } from './componentUtil';
import { CssHelper } from './CssHelper';
import { Localization } from './Localization';
var WebComponentRenderer = /** @class */ /*@__PURE__*/ (function () {
    function WebComponentRenderer(container, document, useDefaultsSource, defaultsSource, portalManager) {
        if (portalManager === void 0) {
            portalManager = null;
        }
        this.container = container;
        this._useDefaultsSource = false;
        this._checkResized = null;
        this._resizeListener = null;
        this._resizeTicking = false;
        this._resizeTickId = -1;
        this._discStack = [];
        this._toUnbind = [];
        this._currentFontQuery = null;
        this._document = document;
        this._container = container;
        this._rootElement = container;
        this._rootWrapper = new WebComponentWrapper(this._rootElement, this);
        this._cssHelper = new WebComponentCssHelper(this, this._document);
        this._useDefaultsSource = useDefaultsSource;
        this._defaultsSource = defaultsSource;
        this._portalManager = portalManager;
    }
    WebComponentRenderer.prototype.addSizeWatcher = function (onResized) {
        var _this = this;
        var previousWidth = this._rootElement.offsetWidth;
        var previousHeight = this._rootElement.offsetHeight;
        var previousRatio = window.devicePixelRatio;
        if (this._checkResized == null) {
            this._checkResized = function () {
                var currWidth = _this._rootElement.offsetWidth;
                var currHeight = _this._rootElement.offsetHeight;
                var currRatio = window.devicePixelRatio;
                var changed = false;
                if (previousWidth != currWidth ||
                    previousHeight != currHeight ||
                    previousRatio != currRatio) {
                    changed = true;
                }
                previousWidth = currWidth;
                previousHeight = currHeight;
                previousRatio = currRatio;
                if (changed) {
                    onResized();
                }
            };
        }
        this._resizeListener = this.globalListen("window", "resize", function (ev) { return _this._checkResized(); });
        var self = this;
        this._resizeTicking = true;
        function resizeTick() {
            self._resizeTickId = -1;
            if (self._resizeTicking) {
                self._checkResized();
                self._resizeTickId = self.setTimeout(resizeTick, 100);
            }
        }
        this._resizeTickId = this.setTimeout(resizeTick, 100);
    };
    WebComponentRenderer.prototype.destroy = function () {
        for (var i = 0; i < this._toUnbind.length; i++) {
            this._toUnbind[i]();
        }
        this._toUnbind = [];
        this.removeSizeWatcher();
        this._rootWrapper.destroy();
        this._rootWrapper = null;
        this._rootElement = null;
    };
    WebComponentRenderer.prototype.removeSizeWatcher = function () {
        this._resizeTicking = false;
        if (this._resizeTickId != -1) {
            window.clearTimeout(this._resizeTickId);
            this._resizeTickId = -1;
        }
        if (this._resizeListener != null) {
            this._resizeListener();
            this._resizeListener = null;
        }
    };
    WebComponentRenderer.prototype.updateRoot = function (root) {
        this._rootWrapper = root;
        this._rootElement = root.getNativeElement();
    };
    WebComponentRenderer.prototype.withRenderer = function (act) {
        act(this);
    };
    WebComponentRenderer.prototype.supportsAnimation = function () {
        return true;
    };
    WebComponentRenderer.prototype.getRequestAnimationFrame = function () {
        var ret = function (act) {
            requestAnimationFrame(act);
        };
        return ret;
    };
    WebComponentRenderer.prototype.setTimeout = function (act, millisecondsDelay) {
        var val;
        val = window.setTimeout(act, millisecondsDelay);
        return val;
    };
    WebComponentRenderer.prototype.clearTimeout = function (timerId) {
        window.clearTimeout(timerId);
    };
    Object.defineProperty(WebComponentRenderer.prototype, "rootWrapper", {
        get: function () {
            return this._rootWrapper;
        },
        enumerable: false,
        configurable: true
    });
    WebComponentRenderer.prototype.querySelector = function (selector) {
        var _this = this;
        var wrapper = null;
        this.withRenderer(function (ren) {
            var ele = _this.rootWrapper.getNativeElement().querySelector(selector);
            if (ele === null) {
                return null;
            }
            wrapper = new WebComponentWrapper(ele, ren);
        });
        return wrapper;
    };
    WebComponentRenderer.prototype.getWrapper = function (ele) {
        if (!ele) {
            return null;
        }
        var wrapper = null;
        this.withRenderer(function (ren) {
            wrapper = new WebComponentWrapper(ele, ren);
        });
        return wrapper;
    };
    WebComponentRenderer.prototype.getResourceString = function (resourceId) {
        // check if the resource culture is available.  It is a combination of the bundle ID and culture ID
        var grouping = this._resourceBundleId + "-" + this._cultureId;
        var isRegistered = Localization.isRegistered(grouping);
        if (isRegistered) {
            return Localization.getString(grouping, resourceId);
        }
        else {
            return "";
        }
    };
    WebComponentRenderer.prototype.setResourceBundleId = function (bundle) {
        this._resourceBundleId = bundle;
    };
    WebComponentRenderer.prototype.setCultureId = function (culture) {
        this._cultureId = culture;
    };
    WebComponentRenderer.prototype.append = function (child) {
        this._rootWrapper.append(child);
        return this;
    };
    WebComponentRenderer.prototype.appendToBody = function (element) {
        if (this._document !== null && this._document.body !== null) {
            this._document.body.appendChild(element.getNativeElement());
        }
    };
    WebComponentRenderer.prototype.createElement = function (elementName) {
        var _this = this;
        var ele;
        this.withRenderer(function (ren) {
            ele = _this._document.createElement(elementName);
        });
        var wrap = new WebComponentWrapper(ele, this);
        return wrap;
    };
    WebComponentRenderer.prototype.createElementNS = function (elementName, ns) {
        var _this = this;
        var ele;
        this.withRenderer(function (ren) {
            ele = _this._document.createElementNS(ns, elementName);
        });
        var wrap = new WebComponentWrapper(ele, this);
        return wrap;
    };
    WebComponentRenderer.prototype.endCSSQuery = function () {
        this._currentFontQuery = null;
        if (!this.hasBody()) {
            return;
        }
        if (this._discStack.length > 0) {
            var toRemove = this._discStack.pop();
            toRemove.remove();
        }
    };
    WebComponentRenderer.prototype.expandTemplate = function (template, args) {
        throw new Error("not implemented");
    };
    WebComponentRenderer.prototype.get2DCanvasContext = function (canvas) {
        var context = null;
        this.withRenderer(function (ren) {
            var canv = (canvas.getNativeElement());
            context = canv.getContext("2d");
        });
        return context;
    };
    Object.defineProperty(WebComponentRenderer.prototype, "isComputedFontQuery", {
        get: function () {
            return this._currentFontQuery != null &&
                this.hasBody();
        },
        enumerable: false,
        configurable: true
    });
    WebComponentRenderer.prototype.getCssDefaultPropertyValue = function (className, propertyName) {
        if (this._useDefaultsSource && !this.isComputedFontQuery) {
            var c = this._defaultsSource[className];
            if (className == "" || className == null) {
                c = this._defaultsSource;
            }
            if (c === undefined) {
                return null;
            }
            var v = c[propertyName];
            if (v === undefined) {
                return null;
            }
            return v;
        }
        return this._cssHelper.getPropertyValue(this.getCurrentDiscovery(), className, propertyName);
    };
    WebComponentRenderer.prototype.getCssDefaultValuesForClassCollection = function (classPrefix, propertyNames) {
        if (this._useDefaultsSource) {
            var ret = [];
            for (var k in this._defaultsSource) {
                if (k.indexOf(classPrefix) === 0) {
                    var subRet = [];
                    ret.push(subRet);
                    for (var i = 0; i < propertyNames.length; i++) {
                        var v = this._defaultsSource[k][propertyNames[i]];
                        if (v === undefined) {
                            subRet.push(null);
                        }
                        else {
                            subRet.push(v);
                        }
                    }
                }
            }
            return ret;
        }
        return this._cssHelper.getValuesForClassCollection(this.getCurrentDiscovery(), classPrefix, propertyNames);
    };
    WebComponentRenderer.prototype.getDefaultFontHeight = function () {
        if (this._defaultsSource["default-font-height"] !== undefined) {
            return +this._defaultsSource["default-font-height"];
        }
        return 16;
    };
    WebComponentRenderer.prototype.getHeightForFontString = function (fontString, text, useOffsetHeight) {
        if (this._rootWrapper.getNativeElement() == null) {
            //TODO: some voodoo here.
            return 12;
        }
        this.startCSSQuery();
        var disc = this.getCurrentDiscovery();
        if (disc == null || !this.hasWindow) {
            return this.getDefaultFontHeight();
        }
        if (disc.getNativeElement().parentElement) {
            disc.getNativeElement().parentElement.removeChild(disc.getNativeElement());
        }
        disc.setStyleProperty("position", "absolute");
        disc.setStyleProperty("visibility", "hidden");
        disc.setStyleProperty("font", fontString);
        document.body.appendChild(disc.getNativeElement());
        var span;
        this.withRenderer(function (ren) {
            span = ren.createElement("span");
        });
        var spanWrap = new WebComponentWrapper(span, this);
        this.getCurrentDiscovery().append(spanWrap);
        spanWrap.setText(text);
        var height;
        if (useOffsetHeight) {
            var offHeight = spanWrap.getNativeElement().offsetHeight;
            height = offHeight;
        }
        else {
            height = spanWrap.getNativeElement().height();
        }
        if (height == 0) {
            //HACK: HACK! HACK! HACK!
            height = this.getDefaultFontHeight();
        }
        //console.log("font height: " + height);
        document.body.removeChild(disc.getNativeElement());
        this.endCSSQuery();
        return height;
    };
    WebComponentRenderer.prototype.getSubRenderer = function (root) {
        var elem = root;
        elem = elem.getNativeElement();
        return new WebComponentRenderer(elem, this._document, this._useDefaultsSource, this._defaultsSource, this._portalManager);
    };
    WebComponentRenderer.prototype.listen = function (element, eventName, handler) {
        var retVal = null;
        if (element == "window") {
            element = window;
        }
        if (element == "document") {
            element = document;
        }
        var $self = this;
        var ret = element.addEventListener(eventName, handler);
        var self = this;
        retVal = function () {
            var ind = self._toUnbind.indexOf(retVal);
            if (ind >= 0) {
                self._toUnbind.splice(ind, 1);
            }
            element.removeEventListener(eventName, handler);
            handler = null;
        };
        this._toUnbind.push(retVal);
        return retVal;
    };
    WebComponentRenderer.prototype.runInMainZone = function (action) {
        action();
    };
    WebComponentRenderer.prototype.globalListen = function (element, eventName, handler) {
        var retVal = null;
        //this._ngZone.runOutsideAngular(() => {
        var $self = this;
        var a = function (e) {
            var inner = {};
            //TODO: normalize here?
            inner.originalEvent = e;
            inner.altKey = e.altKey;
            inner.button = e.button;
            inner.ctrlKey = e.ctrlKey;
            inner.offsetX = e.offsetX;
            inner.offsetY = e.offsetY;
            inner.pageX = e.pageX;
            inner.pageY = e.pageY;
            if (e.touches && e.touches.length > 0) {
                inner.pageX = e.touches[0].pageX;
                inner.pageY = e.touches[0].pageY;
            }
            inner.shiftKey = e.shiftKey;
            inner.which = e.which;
            inner.preventDefault = function () {
                e.preventDefault();
            };
            inner.stopPropagation = function () {
                e.stopPropagation();
            };
            //console.log(e);
            handler(inner);
        };
        var ret = this.listen(element, eventName, a);
        var self = this;
        retVal = function () {
            var ind = self._toUnbind.indexOf(retVal);
            if (ind >= 0) {
                self._toUnbind.splice(ind, 1);
            }
            ret();
            a = null;
        };
        this._toUnbind.push(retVal);
        //});
        return retVal;
    };
    WebComponentRenderer.prototype.hasBody = function () {
        return this._document !== null && this._document.body !== null;
    };
    WebComponentRenderer.prototype.hasWindow = function () {
        //todo: hmmm
        return true;
    };
    WebComponentRenderer.prototype.getCurrentDiscovery = function () {
        if (this._discStack.length <= 0) {
            return null;
        }
        return this._discStack[this._discStack.length - 1];
    };
    WebComponentRenderer.prototype.setCssQueryFontString = function (fontString) {
        if (this._useDefaultsSource) {
            this._currentFontQuery = fontString;
        }
        if (!document.body.contains(this.getCurrentDiscovery().getNativeElement())) {
            this.getCurrentDiscovery().remove();
            this.appendToBody(this.getCurrentDiscovery());
        }
        this.getCurrentDiscovery().setStyleProperty("font", fontString);
    };
    WebComponentRenderer.prototype.startCSSQuery = function () {
        if (!this.hasBody()) {
            return;
        }
        var disc = this._cssHelper.getDiscoveryElement(this._rootElement);
        this._discStack.push(disc);
        this._rootWrapper.append(disc);
    };
    WebComponentRenderer.prototype.supportsDOMEvents = function () {
        //todo: hmmm.
        return true;
    };
    WebComponentRenderer.prototype.getPortal = function (hostElement, elementTag, portalCallback, isContentPortal) {
        if (this._portalManager) {
            this._portalManager.getPortal(hostElement, elementTag, portalCallback, isContentPortal);
        }
    };
    WebComponentRenderer.prototype.getExternal = function (internalComponent, styleSourceElement, optionalParent) {
        if (styleSourceElement === void 0) {
            styleSourceElement = null;
        }
        if (optionalParent === void 0) {
            optionalParent = null;
        }
        if (internalComponent.externalObject) {
            return internalComponent.externalObject;
        }
        var ext = null;
        if (!internalComponent.$type) {
            return null;
        }
        var name = internalComponent.$type.name;
        var externalName = "Igc" + name + "Component";
        if (!TypeRegistrar.isRegistered(externalName)) {
            return null;
        }
        ext = TypeRegistrar.create(externalName);
        if (ext) {
            ext._implementation = internalComponent;
            internalComponent.externalObject = ext;
        }
        if (styleSourceElement) {
            if (ext._styling) {
                ext._styling(styleSourceElement.getNativeElement(), ext, optionalParent);
            }
        }
        return ext;
    };
    return WebComponentRenderer;
}());
export { WebComponentRenderer };
var WebComponentWrapper = /** @class */ /*@__PURE__*/ (function () {
    function WebComponentWrapper(element, renderer) {
        this.renderer = renderer;
        this._toUnbind = [];
        this._attrCache = new Map();
        this._styleCache = new Map();
        this._innerText = undefined;
        if (element.getNativeElement) {
            element = element.getNativeElement();
        }
        this.ele = element;
        this._toUnbind = [];
    }
    WebComponentWrapper.prototype.destroy = function () {
        this.unlistenAll();
        this.ele = null;
    };
    WebComponentWrapper.prototype.withRenderer = function (act) {
        act(this.renderer);
    };
    WebComponentWrapper.prototype.addClass = function (className) {
        var _this = this;
        if (className == null || className.length == 0) {
            return this;
        }
        this.withRenderer(function (ren) {
            className.split(" ").forEach(function (item) { return _this.ele.classList.add(item); });
        });
        return this;
    };
    WebComponentWrapper.prototype.append = function (child) {
        var _this = this;
        this.withRenderer(function (ren) {
            _this.ele.appendChild(child.getNativeElement());
        });
        return this;
    };
    WebComponentWrapper.prototype.getAttribute = function (propertyName) {
        if (this._attrCache.has(propertyName)) {
            return this._attrCache.get(propertyName);
        }
        return null;
    };
    WebComponentWrapper.prototype.setAttribute = function (propertyName, value) {
        var _this = this;
        this._attrCache.set(propertyName, value);
        this.withRenderer(function (ren) {
            _this.ele.setAttribute(propertyName, value);
        });
        return this;
    };
    WebComponentWrapper.prototype.before = function (child) {
        if (this.ele.parentElement) {
            this.ele.parentElement.insertBefore(child.getNativeElement(), this.ele);
            // this.insertBefore(child.getNativeElement());
        }
        return this;
    };
    WebComponentWrapper.prototype.clone = function () {
        return new WebComponentWrapper(this.getNativeElement().cloneNode(true), this.renderer);
    };
    WebComponentWrapper.prototype.getStyleProperty = function (propertyName) {
        if (this.getNativeElement() !== null) {
            if (getComputedStyle !== undefined) {
                var computed = getComputedStyle(this.getNativeElement());
                //console.log("getting computed value for: " + propertyName + ": " + computed[propertyName]);
                return computed[propertyName];
            }
        }
        if (this._styleCache.has(propertyName)) {
            return this._styleCache.get(propertyName);
        }
        return null;
    };
    WebComponentWrapper.prototype.setStyleProperty = function (propertyName, value) {
        var _this = this;
        this._styleCache.set(propertyName, value);
        this.withRenderer(function (ren) {
            _this.ele.style[propertyName] = value;
        });
        return this;
    };
    WebComponentWrapper.prototype.findByClass = function (className) {
        if (className.substring(0, 1) == '.') {
            className = className.substring(1);
        }
        var ret = this.ele.getElementsByClassName(className);
        var retVal = [];
        retVal.length = ret.length;
        for (var i = 0; i < ret.length; i++) {
            retVal[i] = new WebComponentWrapper(ret[i], this.renderer);
        }
        return retVal;
    };
    WebComponentWrapper.prototype.focus = function (preventScroll) {
        if (this.getNativeElement() !== null && this.getNativeElement().focus !== undefined) {
            if (preventScroll) {
                this.getNativeElement().focus({ preventScroll: true });
            }
            else {
                this.getNativeElement().focus();
            }
        }
        return this;
    };
    WebComponentWrapper.prototype.getChildAt = function (i) {
        var child = this.ele.children[i];
        return new WebComponentWrapper(child, this.renderer);
    };
    WebComponentWrapper.prototype.getChildCount = function () {
        return this.ele.children.length;
    };
    WebComponentWrapper.prototype.getNativeElement = function () {
        var _this = this;
        var nativeElement = null;
        this.withRenderer(function (ren) {
            if (_this.ele == null) {
                nativeElement = null;
                return;
            }
            if (_this.ele.getNativeElement == undefined) {
                nativeElement = _this.ele;
                return nativeElement;
            }
            nativeElement = (_this.ele.getNativeElement());
        });
        return nativeElement;
    };
    WebComponentWrapper.prototype.height = function () {
        var ret = this.getStyleProperty("height");
        if (ret == null) {
            //todo: some voodoo here.
            return 500;
        }
        var val = parseFloat(ret.replace("px", ""));
        if (isNaN(val)) {
            return 0;
        }
        return val;
    };
    WebComponentWrapper.prototype.hide = function () {
        this.setStyleProperty("display", "none");
        return this;
    };
    WebComponentWrapper.prototype.listen = function (eventName, handler) {
        var retVal = null;
        //this.ngZone.runOutsideAngular(() => {
        var $self = this;
        var a = function (e) {
            var inner = {};
            //TODO: normalize here?
            inner.originalEvent = e;
            inner.altKey = e.altKey;
            inner.button = e.button;
            inner.ctrlKey = e.ctrlKey;
            inner.offsetX = e.offsetX;
            inner.offsetY = e.offsetY;
            inner.pageX = e.pageX;
            inner.pageY = e.pageY;
            if (e.touches && e.touches.length > 0) {
                inner.pageX = e.touches[0].pageX;
                inner.pageY = e.touches[0].pageY;
            }
            inner.shiftKey = e.shiftKey;
            inner.which = e.which;
            inner.preventDefault = function () {
                e.preventDefault();
            };
            inner.stopPropagation = function () {
                e.stopPropagation();
            };
            //console.log(e);
            handler(inner);
        };
        var ret = this.renderer.listen(this.ele, eventName, a);
        var self = this;
        retVal = function () {
            var ind = self._toUnbind.indexOf(retVal);
            if (ind >= 0) {
                self._toUnbind.splice(ind, 1);
            }
            ret();
            a = null;
        };
        this._toUnbind.push(retVal);
        //});
        return retVal;
    };
    WebComponentWrapper.prototype.getOffsetHelper = function (ele) {
        var clientRect = ele.getBoundingClientRect();
        return {
            top: clientRect.top + window.pageYOffset,
            left: clientRect.left + window.pageXOffset
        };
    };
    WebComponentWrapper.prototype.getOffset = function () {
        return this.getOffsetHelper(this.getNativeElement());
    };
    WebComponentWrapper.prototype.setOffset = function (x, y) {
        var par = this.getNativeElement().offsetParent || this.getNativeElement().parentElement;
        var parentOffset = this.getOffsetHelper(par);
        return this.setRawPosition(x - parentOffset.left, y - parentOffset.top);
        //return this;
    };
    WebComponentWrapper.prototype.outerHeight = function () {
        return this.getProperty("offsetHeight");
    };
    WebComponentWrapper.prototype.outerHeightWithMargin = function () {
        var height = this.outerHeight();
        height += parseInt(this.getStyleProperty("marginTop"));
        height += parseInt(this.getStyleProperty("marginBottom"));
        return height;
    };
    WebComponentWrapper.prototype.outerWidth = function () {
        return this.getProperty("offsetWidth");
    };
    WebComponentWrapper.prototype.outerWidthWithMargin = function () {
        var width = this.outerWidth();
        width += parseInt(this.getStyleProperty("marginLeft"));
        width += parseInt(this.getStyleProperty("marginRight"));
        return width;
    };
    WebComponentWrapper.prototype.getProperty = function (propertyName) {
        var native = this.getNativeElement();
        return native[propertyName];
    };
    WebComponentWrapper.prototype.setProperty = function (propertyName, value) {
        var _this = this;
        this.withRenderer(function (ren) {
            _this.ele[propertyName] = value;
        });
        return this;
    };
    WebComponentWrapper.prototype.remove = function () {
        var _this = this;
        this.withRenderer(function (ren) {
            var ele = _this.getNativeElement();
            if (ele.parentElement != null) {
                _this.ele.parentElement.removeChild(ele);
            }
            //ren.detachView([this.getNativeElement()]);
        });
        return this;
    };
    WebComponentWrapper.prototype.removeChild = function (child) {
        var _this = this;
        this.withRenderer(function (ren) {
            _this.ele.removeChild(child.getNativeElement());
            //ren.detachView([child.getNativeElement()]);
        });
        return this;
    };
    WebComponentWrapper.prototype.removeChildren = function () {
        for (var i = this.getChildCount() - 1; i >= 0; i--) {
            this.removeChild(this.getChildAt(i));
        }
        return this;
    };
    WebComponentWrapper.prototype.removeClass = function (className) {
        var _this = this;
        if (className == null || className.length == 0) {
            return this;
        }
        this.withRenderer(function (ren) {
            className.split(" ").forEach(function (item) { return _this.ele.classList.remove(item); });
        });
        return this;
    };
    WebComponentWrapper.prototype.setRawStyleProperty = function (propertyName, value) {
        //this.withRenderer((ren) => {
        this.ele.style[propertyName] = value;
        //})
        return this;
    };
    WebComponentWrapper.prototype.setRawPosition = function (x, y) {
        this.ele.style.left = x + "px";
        this.ele.style.top = y + "px";
        return this;
    };
    WebComponentWrapper.prototype.setRawXPosition = function (x) {
        this.ele.style.left = x + "px";
        return this;
    };
    WebComponentWrapper.prototype.setRawYPosition = function (y) {
        this.ele.style.top = y + "px";
        return this;
    };
    WebComponentWrapper.prototype.setRawSize = function (width, height) {
        this.ele.style.width = width + "px";
        this.ele.style.height = height + "px";
        return this;
    };
    WebComponentWrapper.prototype.show = function () {
        this.setStyleProperty("display", "");
        return this;
    };
    WebComponentWrapper.prototype.getText = function () {
        if (this._innerText != undefined) {
            return this._innerText;
        }
        return this.getNativeElement().innerText;
    };
    WebComponentWrapper.prototype.setText = function (value) {
        var _this = this;
        this._innerText = value;
        this.withRenderer(function (ren) {
            //workaround warning:
            _this.getNativeElement().innerText = value;
            //ren.setText(this.ele, value);
        });
        return this;
    };
    WebComponentWrapper.prototype.setRawText = function (value) {
        this._innerText = value;
        this.ele.textContent = value;
        return this;
    };
    WebComponentWrapper.prototype.unlistenAll = function () {
        var toUnbind = [];
        for (var i = 0; i < this._toUnbind.length; i++) {
            toUnbind.push(this._toUnbind[i]);
        }
        for (var i = 0; i < toUnbind.length; i++) {
            toUnbind[i]();
        }
        this._toUnbind.length = 0;
        return this;
    };
    WebComponentWrapper.prototype.width = function () {
        var ret = this.getStyleProperty("width");
        if (ret == null) {
            //todo: some voodoo here.
            return 500;
        }
        var val = parseFloat(ret.replace("px", ""));
        if (isNaN(val)) {
            return 0;
        }
        return val;
    };
    WebComponentWrapper.prototype.parent = function () {
        return this.ele == null || this.ele.parentElement == null ? null :
            new WebComponentWrapper(this.ele.parentElement, this.renderer);
    };
    WebComponentWrapper.prototype.querySelectorAll = function (selector) {
        var elements = this.ele.querySelectorAll(selector);
        var result = [];
        for (var ii = 0; ii < elements.length; ii++) {
            result.push(new WebComponentWrapper(elements[ii], this.renderer));
        }
        return result;
    };
    return WebComponentWrapper;
}());
export { WebComponentWrapper };
var WebComponentCssHelper = /** @class */ /*@__PURE__*/ (function () {
    function WebComponentCssHelper(renderer, document) {
        this.renderer = renderer;
        this.document = document;
    }
    WebComponentCssHelper.prototype.getDiscoveryElement = function (container) {
        //console.log(container);
        var ele = this.renderer.createElement("fakediscoveryelement");
        var wrapper = new WebComponentWrapper(ele, this.renderer);
        wrapper.setStyleProperty("box-sizing", "content-box");
        return wrapper;
    };
    WebComponentCssHelper.prototype.getPropertyValue = function (discoveryElement, className, propertyName) {
        //console.log("fetching: " + className + ", prop: " + propertyName);
        var ret = CssHelper.getPropertyValue1(discoveryElement, className, propertyName);
        //console.log("for: " + className + ", prop: " + propertyName + " value: " + ret);
        return ret;
    };
    WebComponentCssHelper.prototype.getValuesForClassCollection = function (discoveryElement, classPrefix, propertyNames) {
        var prop = new List$1(String_$type, 0);
        for (var i = 0; i < propertyNames.length; i++) {
            prop.add(propertyNames[i]);
        }
        var ret = CssHelper.getValuesForClassCollection(discoveryElement, classPrefix, prop);
        var retVal = new Array(ret.count);
        for (var i1 = 0; i1 < ret.count; i1++) {
            retVal[i1] = new Array(ret._inner[i1].count);
            for (var j = 0; j < ret._inner[i1].count; j++) {
                retVal[i1][j] = ret._inner[i1]._inner[j];
            }
        }
        return retVal;
    };
    return WebComponentCssHelper;
}());
var PortalManager = /** @class */ /*@__PURE__*/ (function () {
    function PortalManager(id) {
        this._portals = [];
        this._portalsMap = new Map();
        this._id = null;
        this._currId = 0;
        this._id = id;
    }
    PortalManager.prototype.onChildrenChanged = function (children) {
        if (this._portals && this._portals.length > 0) {
            //children = React.Children.toArray(this.props.children);
            for (var i = 0; i < this._portals.length; i++) {
                var p = this._portals[i];
                if (p.isContentPortal) {
                    p.componentRef.onChildrenChanged(children);
                }
            }
        }
    };
    PortalManager.prototype.getId = function () {
        return this._id + "__" + (this._currId++).toString();
    };
    PortalManager.prototype.getPortal = function (hostElement, elementTag, portalCallback, isContentPortal) {
        var portal = new WebComponentDomPortal(this, hostElement, elementTag, portalCallback, this.getId(), isContentPortal);
        this._portals.push(portal);
        this._portalsMap.set(portal._portalId, portal);
        portalCallback(portal);
    };
    PortalManager.prototype._destroy = function (portal) {
        var ind = this._portals.indexOf(portal);
        this._portalsMap.delete(portal._portalId);
        this._portals.splice(ind, 1);
    };
    return PortalManager;
}());
export { PortalManager };
var WebComponentDomPortal = /** @class */ /*@__PURE__*/ (function () {
    function WebComponentDomPortal(portalManager, hostElement, elementTag, portalCallback, portalId, isContentPortal) {
        this.onChildrenChanged = null;
        this._componentReference = null;
        this.portalContainer = hostElement;
        this._elementTag = elementTag;
        this._portalCallback = portalCallback;
        this._portalId = portalId;
        this.isContentPortal = isContentPortal;
        this._owner = portalManager;
        this.componentRef = this.createElementFromTag(this._elementTag, this._portalId);
        hostElement.getNativeElement().appendChild(this.componentRef);
    }
    WebComponentDomPortal.prototype.destroy = function () {
        this._owner._destroy(this);
    };
    WebComponentDomPortal.prototype._onComponentProvided = function (ele) {
        var oldValue = this.componentRef;
        this.componentRef = ele;
        if (oldValue !== this.componentRef) {
            this._portalCallback(this);
        }
    };
    WebComponentDomPortal.prototype.toPascal = function (name) {
        var val = fromSpinal(name);
        return val.substr(0, 1).toUpperCase() + val.substr(1);
    };
    WebComponentDomPortal.prototype.createElementFromTag = function (elementTag, portalId) {
        if (elementTag.indexOf("-") >= 0) {
            elementTag = this.toPascal(elementTag);
        }
        var name = "Igc" + elementTag + "Component";
        var ele = TypeRegistrar.create(name);
        this.componentRef = ele;
        return this.componentRef;
    };
    return WebComponentDomPortal;
}());
