import { __extends, __read, __spreadArray } from "tslib";
import { TypeDescriptionMetadata } from './TypeDescriptionMetadata';
import { TypeDescriptionWellKnownType } from './TypeDescriptionWellKnownType';
import { IgCollection } from './IgCollection';
import { TypeRegistrar } from './type';
import { Description } from './Description';
import { IgxComponentRendererContainerComponent } from './igx-component-renderer-container-component';
var ComponentRendererAdapter = /** @class */ /*@__PURE__*/ (function () {
    function ComponentRendererAdapter() {
        this.isBlazorRenderer = false;
        this._platform = "Igx";
    }
    ComponentRendererAdapter.prototype.createObject = function (type, container, context) {
        var isPlain = context.getMetadata(type, "__isPlainObject");
        if (isPlain) {
            return {};
        }
        var origName = type;
        var meta = context.getMetadata(origName, "__qualifiedNameTS");
        if (meta) {
            type = meta.specificExternalType;
        }
        var meta = context.getMetadata(origName, "__qualifiedNameBlazorTS");
        if (meta && this.isBlazorRenderer) {
            type = meta.specificExternalType;
        }
        var tagName = null;
        var meta = context.getMetadata(origName, "__tagNameWC");
        if (meta && this._platform === "Igc") {
            tagName = meta.specificExternalType;
        }
        if (tagName) {
            if (this._platform === "Igc") {
                var crc = IgxComponentRendererContainerComponent.fromElement(container);
                return crc.createObject(tagName);
            }
        }
        var mustManageInMarkup = false;
        var meta = context.getMetadata(origName, "__manageCollectionInMarkup");
        if (meta) {
            mustManageInMarkup = true;
        }
        var meta = context.getMetadata(origName, "__manageItemInMarkup");
        if (meta) {
            mustManageInMarkup = true;
        }
        var preferUnsuffixed = false;
        var meta = context.getMetadata(origName, "__preferUnsuffixed");
        if (meta) {
            preferUnsuffixed = true;
        }
        if (mustManageInMarkup && this._platform == "Igx") {
            var crc = container;
            return crc.createObject(origName, container, context);
        }
        var typeName = "Igx" + type + "Component";
        var shortTypeName = "Igx" + type;
        if (preferUnsuffixed) {
            if (TypeRegistrar.isRegistered(shortTypeName)) {
                return TypeRegistrar.create(shortTypeName);
            }
        }
        if (TypeRegistrar.isRegistered(typeName)) {
            return TypeRegistrar.create(typeName);
        }
        if (TypeRegistrar.isRegistered(shortTypeName)) {
            return TypeRegistrar.create(shortTypeName);
        }
        if (TypeRegistrar.isRegistered(type)) {
            return TypeRegistrar.create(type);
        }
    };
    //createRootObject(type: string, container: any, context: TypeDescriptionContext, continueActions: (resumeRequired: boolean) => void) {
    //    let typeName = "Igr" + type;
    //    if (TypeRegistrar.isRegistered(typeName)) {
    //        let t = TypeRegistrar.get(typeName);
    //        let crc = container as IgrComponentRendererContainer;
    //        crc.createRootObject(t, continueActions);
    //    }
    //}
    ComponentRendererAdapter.prototype.createColorCollection = function (colors) {
        return colors;
    };
    ComponentRendererAdapter.prototype.createBrushCollection = function (brushes) {
        return brushes;
    };
    ComponentRendererAdapter.prototype.createDoubleCollection = function (lengths) {
        return lengths;
    };
    ComponentRendererAdapter.prototype.coerceToEnum = function (type, context, value) {
        return TypeDescriptionMetadata.camelize(value);
    };
    ComponentRendererAdapter.prototype.onUIThread = function (container, action) {
        action();
    };
    ComponentRendererAdapter.prototype.setOrUpdateCollectionOnTarget = function (container, propertyName, propertyMetadata, context, target, value) {
        var coll;
        if (this.mustManageInMarkup(target, propertyName, propertyMetadata, true)) {
            coll = this.getMarkupCollection(target, propertyName, propertyMetadata, true);
        }
        else {
            coll = this.getPropertyValue(target, propertyName);
        }
        if (coll instanceof IgCollection) {
            for (var i = 0; i < coll.count; i++) {
                if (coll.item(i) && coll.item(i)._implementation) {
                    coll.item(i).___parent = null;
                }
            }
            coll.clear();
            var newArr = value;
            for (var i = 0; i < newArr.length; i++) {
                if (newArr[i] && newArr[i]._implementation) {
                    newArr[i].___parent = target;
                }
            }
            for (var i = 0; i < newArr.length; i++) {
                coll.add(newArr[i]);
            }
        }
        else if (Array.isArray(coll) || coll == null || coll == undefined) {
            if (coll) {
                for (var i = 0; i < coll.length; i++) {
                    if (coll[i] && coll[i]._implementation) {
                        coll[i].___parent = null;
                    }
                }
            }
            var newArr = value;
            for (var i = 0; i < newArr.length; i++) {
                if (newArr[i] && newArr[i]._implementation) {
                    newArr[i].___parent = target;
                }
            }
            this.setPropertyValue(target, propertyName, propertyMetadata, value, coll, null);
        }
        else {
            if (coll.clear !== undefined) {
                if (coll.count && coll.item) {
                    for (var i = 0; i < coll.count; i++) {
                        if (coll.item(i) && coll.item(i)._implementation) {
                            coll.item(i).___parent = null;
                        }
                    }
                }
                coll.clear();
            }
            if (coll.add !== undefined) {
                var newArr = value;
                for (var i = 0; i < newArr.length; i++) {
                    if (newArr[i] && newArr[i]._implementation) {
                        newArr[i].___parent = target;
                    }
                }
                for (var i = 0; i < newArr.length; i++) {
                    coll.add(newArr[i]);
                }
            }
        }
    };
    ComponentRendererAdapter.prototype.onPendingRef = function (target, propertyName, propertyMetadata, sourceRef) {
    };
    ComponentRendererAdapter.prototype.ensureExternalObject = function (target, propertyMetadata) {
        if (target && target._implementation) {
            // we are already an external type.
            return target;
        }
        var owningType = propertyMetadata.owningType;
        var context = propertyMetadata.owningContext;
        var typeName = "Igx" + owningType + "Component";
        var meta = context.getMetadata(owningType, "__qualifiedNameTS");
        if (meta) {
            typeName = "Igx" + meta.specificExternalType + "Component";
        }
        if (TypeRegistrar.isRegistered(typeName)) {
            return TypeRegistrar.create(typeName);
        }
        typeName = "Igx" + owningType;
        if (meta) {
            typeName = "Igx" + meta.specificExternalType;
        }
        if (TypeRegistrar.isRegistered(typeName)) {
            return TypeRegistrar.create(typeName);
        }
        return null;
    };
    ComponentRendererAdapter.prototype.setPropertyValue = function (target, propertyName, propertyMetadata, value, oldValue, sourceRef) {
        if (this._platform == "Igx" &&
            IgxComponentRendererContainerComponent.isEvent &&
            IgxComponentRendererContainerComponent.isEvent(target[propertyName])) {
            if (target["_" + propertyName + "Subscription"]) {
                target["_" + propertyName + "Subscription"]();
                target["_" + propertyName + "Subscription"] = null;
            }
            if (value != null) {
                var fun = function (ev) {
                    if (ev.sender && ev.args) {
                        value(ev.sender, ev.args);
                    }
                    else {
                        value(ev);
                    }
                };
                var sub = target[propertyName].subscribe(fun);
                target["_" + propertyName + "Subscription"] = sub;
            }
            return;
        }
        var fromScript = value && value.___fromScript;
        if (this._platform == "Igc" &&
            target.addEventListener) {
            if (fromScript && this.isBlazorRenderer) {
                if (!target.externalObject) {
                    var ext = this.ensureExternalObject(target, propertyMetadata);
                    if (!ext) {
                        if (value) {
                            target.addEventListener(propertyName, value);
                        }
                        else {
                            target.removeEventListener(propertyName, oldValue);
                        }
                        return;
                    }
                    target.externalObject = ext;
                    if (target.externalObject.i && !target.externalObject.i.nativeElement) {
                        target.externalObject.i.nativeElement = target;
                    }
                }
                var extObj = target.externalObject;
                //extObj[propertyName] = value;
                extObj[propertyName + "Script"] = value.___fromScriptId;
            }
            if (propertyMetadata &&
                propertyMetadata.knownType == TypeDescriptionWellKnownType.EventRef &&
                propertyMetadata.isCustomEvent) {
                if (this.isBlazorRenderer && !fromScript) {
                    if (!target.externalObject) {
                        var ext = this.ensureExternalObject(target, propertyMetadata);
                        if (!ext) {
                            if (value) {
                                target.addEventListener(propertyName, value);
                            }
                            else {
                                target.removeEventListener(propertyName, oldValue);
                            }
                            return;
                        }
                        target.externalObject = ext;
                        if (target.externalObject.i && !target.externalObject.i.nativeElement) {
                            target.externalObject.i.nativeElement = target;
                        }
                    }
                    var extObj = target.externalObject;
                    extObj[propertyName] = value;
                }
                else {
                    if (value) {
                        target.addEventListener(propertyName, value);
                    }
                    else {
                        target.removeEventListener(propertyName, oldValue);
                    }
                }
                return;
            }
        }
        if (this._platform == "Igc" &&
            target.emitEvent &&
            target.addEventListener) {
            if (propertyMetadata &&
                propertyMetadata.knownType == TypeDescriptionWellKnownType.EventRef) {
                //TODO: this needs to come from the metadata.
                if (propertyName.endsWith("Ocurred")) {
                    propertyName = propertyName.replace("Ocurred", "");
                }
                if (propertyName.toLowerCase() == "selectionchanged") {
                    propertyName = "selection";
                }
                var eventId = "igc" + TypeDescriptionMetadata.toPascal(propertyName);
                if (value) {
                    target.addEventListener(eventId, value);
                }
                else {
                    target.removeEventListener(eventId, oldValue);
                }
                return;
            }
        }
        if (this._platform == "Igc" &&
            target.addEventListener &&
            this.isBlazorRenderer &&
            propertyMetadata &&
            propertyMetadata.knownType == TypeDescriptionWellKnownType.TemplateRef) {
            var fromScript = value && value.___fromScript;
            if (!fromScript) {
                if (!target.externalObject) {
                    var ext = this.ensureExternalObject(target, propertyMetadata);
                    if (!ext) {
                        target[propertyName] = value;
                        return;
                    }
                    target.externalObject = ext;
                    if (target.externalObject.i && !target.externalObject.i.nativeElement) {
                        target.externalObject.i.nativeElement = target;
                    }
                }
                var extObj = target.externalObject;
                extObj[propertyName] = value;
                return;
            }
        }
        if (oldValue && oldValue._implementation) {
            value.___parent = null;
        }
        if (value && value._implementation) {
            value.___parent = target;
        }
        if (propertyMetadata != null &&
            !(target instanceof Description) &&
            this.mustManageInMarkup(target, propertyName, propertyMetadata, false)) {
            var coll = this.getMarkupCollection(target, propertyName, propertyMetadata, false);
            if (value) {
                coll.add(value);
            }
            else {
                coll.clear();
            }
            return;
        }
        target[propertyName] = value;
    };
    ComponentRendererAdapter.prototype.getPropertyValue = function (target, propertyName) {
        return target[propertyName];
    };
    ComponentRendererAdapter.prototype.clearContainer = function (container, context, continueActions) {
        if (this._platform === "Igc") {
            var crc_1 = IgxComponentRendererContainerComponent.fromElement(container);
            crc_1.clearContainer(continueActions);
            return;
        }
        var crc = container;
        crc.clearContainer(continueActions);
    };
    ComponentRendererAdapter.prototype.getRootObject = function (container) {
        if (this._platform === "Igc") {
            var crc_2 = IgxComponentRendererContainerComponent.fromElement(container);
            return crc_2.getRootObject();
        }
        var crc = container;
        return crc.getRootObject();
    };
    ComponentRendererAdapter.prototype.forPropertyValueItem = function (target, propertyName, forItem) {
        var coll = this.getPropertyValue(target, propertyName);
        if (coll instanceof IgCollection) {
            for (var i = 0; i < coll.count; i++) {
                forItem(coll.item(i));
            }
        }
        else if (Array.isArray(coll)) {
            for (var i = 0; i < coll.length; i++) {
                forItem(coll[i]);
            }
        }
        else {
            if (coll.clear !== undefined) {
                for (var i = 0; i < coll.count; i++) {
                    forItem(coll.item(i));
                }
            }
        }
    };
    ComponentRendererAdapter.prototype.getMarkupTypeMatcher = function (target, propertyName, metadata, isCollection) {
        if (this._platform === "Igc" || this._platform === "Igx") {
            if ((isCollection && metadata.knownType !== TypeDescriptionWellKnownType.Collection) ||
                (!isCollection && metadata.knownType !== TypeDescriptionWellKnownType.ExportedType)) {
                return null;
            }
            if (isCollection) {
                var type_1 = metadata.collectionElementType;
                var origName = type_1;
                var meta = metadata.owningContext.getMetadata(type_1, "__qualifiedNameTS");
                if (meta) {
                    type_1 = meta.specificExternalType;
                }
                var tagName = null;
                meta = metadata.owningContext.getMetadata(origName, "__tagNameWC");
                if (meta && this._platform === "Igc") {
                    tagName = meta.specificExternalType;
                    if (tagName) {
                        if (window && window.customElements && window.customElements.get) {
                            var tags_1 = [];
                            var tt = window.customElements.get(tagName);
                            if (tt) {
                                tags_1.push(tt);
                            }
                            var subMeta = metadata.owningContext.getMetadata(origName, "__tagNameWCSubstitutable");
                            if (subMeta) {
                                var subs = subMeta.specificExternalType;
                                var subParts = subs.split(',');
                                for (var j = 0; j < subParts.length; j++) {
                                    var subPart = subParts[j];
                                    var subT = window.customElements.get(subPart);
                                    if (subT) {
                                        tags_1.push(subT);
                                    }
                                }
                            }
                            return function (o) {
                                if (tags_1.length == 0) {
                                    return false;
                                }
                                for (var i = 0; i < tags_1.length; i++) {
                                    if (o instanceof tags_1[i]) {
                                        return true;
                                    }
                                }
                                return false;
                            };
                        }
                    }
                }
                var typeName = "Igx" + type_1 + "Component";
                if (TypeRegistrar.isRegistered(typeName)) {
                    var t_1 = TypeRegistrar.get(typeName);
                    return function (o) { return o instanceof t_1; };
                }
                var shortTypeName = "Igx" + type_1;
                if (TypeRegistrar.isRegistered(shortTypeName)) {
                    var t_2 = TypeRegistrar.get(shortTypeName);
                    return function (o) { return o instanceof t_2; };
                }
                if (TypeRegistrar.isRegistered(type_1)) {
                    var t_3 = TypeRegistrar.get(type_1);
                    return function (o) { return o instanceof t_3; };
                }
            }
            else {
                var type = metadata.specificExternalType;
                var origName = type;
                var meta = metadata.owningContext.getMetadata(type, "__qualifiedNameTS");
                if (meta) {
                    type = meta.specificExternalType;
                }
                var tagName = null;
                meta = metadata.owningContext.getMetadata(origName, "__tagNameWC");
                if (meta && this._platform === "Igc") {
                    tagName = meta.specificExternalType;
                    if (tagName) {
                        if (window && window.customElements && window.customElements.get) {
                            var tags_2 = [];
                            var tt = window.customElements.get(tagName);
                            if (tt) {
                                tags_2.push(tt);
                            }
                            var subMeta = metadata.owningContext.getMetadata(origName, "__tagNameWCSubstitutable");
                            if (subMeta) {
                                var subs = subMeta.specificExternalType;
                                var subParts = subs.split(',');
                                for (var j = 0; j < subParts.length; j++) {
                                    var subPart = subParts[j];
                                    var subT = window.customElements.get(subPart);
                                    if (subT) {
                                        tags_2.push(subT);
                                    }
                                }
                            }
                            return function (o) {
                                if (tags_2.length == 0) {
                                    return false;
                                }
                                for (var i = 0; i < tags_2.length; i++) {
                                    if (o instanceof tags_2[i]) {
                                        return true;
                                    }
                                }
                                return false;
                            };
                        }
                    }
                }
                var typeName = "Igx" + type + "Component";
                if (TypeRegistrar.isRegistered(typeName)) {
                    var t_4 = TypeRegistrar.get(typeName);
                    return function (o) { return o instanceof t_4; };
                }
                var shortTypeName = "Igx" + type;
                if (TypeRegistrar.isRegistered(shortTypeName)) {
                    var t_5 = TypeRegistrar.get(shortTypeName);
                    return function (o) { return o instanceof t_5; };
                }
                if (TypeRegistrar.isRegistered(type)) {
                    var t_6 = TypeRegistrar.get(type);
                    return function (o) { return o instanceof t_6; };
                }
            }
        }
        return null;
    };
    ComponentRendererAdapter.prototype.mustManageInMarkup = function (target, propertyName, metadata, isCollection) {
        if (this._platform === "Igc" || this._platform === "Igx") {
            if ((isCollection && metadata.knownType !== TypeDescriptionWellKnownType.Collection) ||
                (!isCollection && metadata.knownType !== TypeDescriptionWellKnownType.ExportedType)) {
                return false;
            }
            if (isCollection) {
                var ele = metadata.collectionElementType;
                var needsMarkup = metadata.owningContext.getMetadata(ele, "__manageCollectionInMarkup");
                if (needsMarkup) {
                    return true;
                }
            }
            else {
                var ele = metadata.specificExternalType;
                var needsMarkup = metadata.owningContext.getMetadata(ele, "__manageItemInMarkup");
                if (needsMarkup) {
                    return true;
                }
            }
        }
        return false;
    };
    ComponentRendererAdapter.prototype.getMarkupCollection = function (target, propertyName, metadata, isCollection) {
        if (this._platform == "Igx") {
            return new ngMarkupCollection(target, this._platform, this.getMarkupTypeMatcher(target, propertyName, metadata, isCollection), metadata);
        }
        else {
            return new HtmlMarkupCollection(target, this._platform, this.getMarkupTypeMatcher(target, propertyName, metadata, isCollection));
        }
    };
    ComponentRendererAdapter.prototype.clearCollection = function (target, propertyName, metadata) {
        var coll;
        if (this.mustManageInMarkup(target, propertyName, metadata, true)) {
            coll = this.getMarkupCollection(target, propertyName, metadata, true);
        }
        else {
            coll = this.getPropertyValue(target, propertyName);
        }
        if (coll instanceof IgCollection) {
            for (var i = 0; i < coll.count; i++) {
                if (coll.item(i) && coll.item(i)._implementation) {
                    coll.item(i).___parent = null;
                }
            }
            coll.clear();
        }
        else if (Array.isArray(coll)) {
            for (var i = 0; i < coll.length; i++) {
                if (coll[i] && coll[i]._implementation) {
                    coll[i].___parent = null;
                }
            }
            this.setPropertyValue(target, propertyName, metadata, [], coll, null);
        }
        else {
            if (coll.clear !== undefined) {
                for (var i = 0; i < coll.count; i++) {
                    if (coll.item(i) && coll.item(i)._implementation) {
                        coll.item(i).___parent = null;
                    }
                }
                coll.clear();
            }
        }
    };
    ComponentRendererAdapter.prototype.addItemToCollection = function (propertyName, propertyMetadata, target, newIndex, item) {
        var coll;
        if (this.mustManageInMarkup(target, propertyName, propertyMetadata, true)) {
            coll = this.getMarkupCollection(target, propertyName, propertyMetadata, true);
        }
        else {
            coll = this.getPropertyValue(target, propertyName);
        }
        if (item && item._implementation) {
            item.___parent = target;
        }
        if (coll instanceof IgCollection) {
            coll.insert(newIndex, item);
        }
        else if (Array.isArray(coll)) {
            var newArr = [];
            for (var i = 0; i < coll.length; i++) {
                newArr[i] = coll[i];
            }
            newArr.splice(newIndex, 0, item);
            this.setPropertyValue(target, propertyName, propertyMetadata, newArr, coll, null);
        }
        else {
            if (coll.insert !== undefined) {
                coll.insert(newIndex, item);
            }
        }
    };
    ComponentRendererAdapter.prototype.resetPropertyOnTarget = function (container, propertyName, propertyMetadata, target) {
        //TODO: anything we can do here? store default?
    };
    ComponentRendererAdapter.prototype.replaceItemInCollection = function (propertyName, propertyMetadata, target, newIndex, item) {
        var coll;
        if (this.mustManageInMarkup(target, propertyName, propertyMetadata, true)) {
            coll = this.getMarkupCollection(target, propertyName, propertyMetadata, true);
        }
        else {
            coll = this.getPropertyValue(target, propertyName);
        }
        if (item && item._implementation) {
            item.___parent = target;
        }
        if (coll instanceof IgCollection) {
            var oldValue = coll.item(newIndex);
            if (oldValue && oldValue._implementation) {
                oldValue.___parent = null;
            }
            coll.item(newIndex, item);
        }
        else if (Array.isArray(coll)) {
            var newArr = [];
            for (var i = 0; i < coll.length; i++) {
                newArr[i] = coll[i];
            }
            var oldValue = newArr[newIndex];
            if (oldValue && oldValue._implementation) {
                oldValue.___parent = null;
            }
            newArr[newIndex] = item;
            this.setPropertyValue(target, propertyName, propertyMetadata, newArr, coll, null);
        }
        else {
            if (coll.item !== undefined) {
                var oldValue = coll.item(newIndex);
                if (oldValue && oldValue._implementation) {
                    oldValue.___parent = null;
                }
                coll.item(newIndex, item);
            }
        }
    };
    ComponentRendererAdapter.prototype.removeItemFromCollection = function (propertyName, propertyMetadata, target, oldIndex) {
        var coll;
        if (this.mustManageInMarkup(target, propertyName, propertyMetadata, true)) {
            coll = this.getMarkupCollection(target, propertyName, propertyMetadata, true);
        }
        else {
            coll = this.getPropertyValue(target, propertyName);
        }
        if (coll instanceof IgCollection) {
            var oldValue = coll.item(oldIndex);
            if (oldValue && oldValue._implementation) {
                oldValue.___parent = null;
            }
            coll.removeAt(oldIndex);
        }
        else if (Array.isArray(coll)) {
            var newArr = [];
            for (var i = 0; i < coll.length; i++) {
                newArr[i] = coll[i];
            }
            var oldValue = newArr[oldIndex];
            if (oldValue && oldValue._implementation) {
                oldValue.___parent = null;
            }
            newArr.splice(oldIndex, 1);
            this.setPropertyValue(target, propertyName, propertyMetadata, newArr, coll, null);
        }
        else {
            if (coll.removeAt !== undefined) {
                var oldValue = coll.item(oldIndex);
                if (oldValue && oldValue._implementation) {
                    oldValue.___parent = null;
                }
                coll.removeAt(oldIndex);
            }
        }
    };
    ComponentRendererAdapter.prototype.replaceRootItem = function (container, type, context, continueActions) {
        var typeName = "Igx" + type + "Component";
        var deferAttach = false;
        var meta = context.getMetadata(type, "__qualifiedNameTS");
        if (meta) {
            typeName = "Igx" + meta.specificExternalType + "Component";
        }
        meta = context.getMetadata(type, "__deferAttachWC");
        if (meta && this._platform === "Igc") {
            deferAttach = true;
        }
        var tagName = null;
        var meta = context.getMetadata(type, "__tagNameWC");
        if (meta && this._platform === "Igc") {
            tagName = meta.specificExternalType;
        }
        if (tagName) {
            if (this._platform === "Igc") {
                var crc = IgxComponentRendererContainerComponent.fromElement(container);
                crc.replaceRootItem(tagName, deferAttach, continueActions);
                return;
            }
        }
        if (TypeRegistrar.isRegistered(typeName)) {
            var t = TypeRegistrar.get(typeName);
            if (this._platform === "Igc") {
                var crc_3 = IgxComponentRendererContainerComponent.fromElement(container);
                crc_3.replaceRootItem(t, deferAttach, continueActions);
                return;
            }
            var crc = container;
            crc.replaceRootItem(t, deferAttach, continueActions);
        }
        else {
            if (this._platform === "Igc") {
                var crc_4 = IgxComponentRendererContainerComponent.fromElement(container);
                crc_4.clearContainer(continueActions);
                return;
            }
            var crc = container;
            crc.clearContainer(continueActions);
        }
    };
    ComponentRendererAdapter.prototype.removeRootItem = function (container, context, continueActions) {
        this.clearContainer(container, context, continueActions);
    };
    ComponentRendererAdapter.prototype.flushChanges = function (container) {
    };
    return ComponentRendererAdapter;
}());
export { ComponentRendererAdapter };
var MarkupCollection = /** @class */ /*@__PURE__*/ (function () {
    function MarkupCollection(target, platform) {
    }
    return MarkupCollection;
}());
//TODO: this could be more efficient if it tracked the child list changes and maintained a current view rather than repolling
var ngMarkupCollection = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(ngMarkupCollection, _super);
    function ngMarkupCollection(target, platform, typeMatcher, metadata) {
        var _this = _super.call(this, target, platform) || this;
        _this._target = target;
        if (metadata.owningContext.hasQueryListName(metadata.owningType, metadata.propertyName)) {
            var queryListName = (metadata.owningContext.getQueryListName(metadata.owningType, metadata.propertyName));
            _this._queryListName = queryListName;
        }
        _this._componentRef = target.___owningRef;
        _this._viewContainerRef = target.___viewContainerRef;
        _this._propertyName = metadata.propertyName;
        if (!_this._target["___listContent" + metadata.propertyName]) {
            _this._target["___listContent" + metadata.propertyName] = [];
        }
        return _this;
    }
    ngMarkupCollection.prototype.getItems = function () {
        return this._target["___listContent" + this._propertyName];
    };
    Object.defineProperty(ngMarkupCollection.prototype, "count", {
        get: function () {
            return this.getItems().length;
        },
        enumerable: false,
        configurable: true
    });
    ngMarkupCollection.prototype.clear = function () {
        var items = this.getItems();
        items.length = 0;
        this.notifyList();
    };
    ngMarkupCollection.prototype.item = function (index, value) {
        var items = this.getItems();
        items[index] = value;
        this.notifyList();
    };
    ngMarkupCollection.prototype.removeAt = function (index) {
        var items = this.getItems();
        items.splice(index, 1);
        this.notifyList();
    };
    ngMarkupCollection.prototype.insert = function (index, item) {
        var items = this.getItems();
        items.splice(index, 0, item);
        this.notifyList();
    };
    ngMarkupCollection.prototype.add = function (item) {
        var items = this.getItems();
        items.push(item);
        this.notifyList();
    };
    ngMarkupCollection.prototype.notifyList = function () {
        var items = this.getItems();
        var qList = this._target[this._queryListName];
        qList.reset(__spreadArray([], __read(items)));
        qList.notifyOnChanges();
    };
    return ngMarkupCollection;
}(MarkupCollection));
//TODO: this could be more efficient if it tracked the child list changes and maintained a current view rather than repolling
var HtmlMarkupCollection = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(HtmlMarkupCollection, _super);
    function HtmlMarkupCollection(target, platform, typeMatcher) {
        var _this = _super.call(this, target, platform) || this;
        _this._target = target;
        _this._typeMatcher = typeMatcher;
        return _this;
    }
    Object.defineProperty(HtmlMarkupCollection.prototype, "count", {
        get: function () {
            var _this = this;
            var items = Array.from(this._target.children).map(function (v, i) { return { item: v, index: i }; }).filter(function (i) { return _this._typeMatcher(i.item); });
            return items.length;
        },
        enumerable: false,
        configurable: true
    });
    HtmlMarkupCollection.prototype.clear = function () {
        var _this = this;
        var items = Array.from(this._target.children).map(function (v, i) { return { item: v, index: i }; }).filter(function (i) { return _this._typeMatcher(i.item); });
        for (var i = 0; i < items.length; i++) {
            items[i].item.remove();
        }
    };
    HtmlMarkupCollection.prototype.item = function (index, value) {
        var _this = this;
        var items = Array.from(this._target.children).map(function (v, i) { return { item: v, index: i }; }).filter(function (i) { return _this._typeMatcher(i.item); });
        if (value) {
            if (index > items.length - 1 || index < 0) {
                return value;
            }
            items[index].item.replaceWith(value);
            return value;
        }
        else {
            if (index > items.length - 1 || index < 0) {
                return null;
            }
            return items[index].item;
        }
    };
    HtmlMarkupCollection.prototype.removeAt = function (index) {
        var _this = this;
        var items = Array.from(this._target.children).map(function (v, i) { return { item: v, index: i }; }).filter(function (i) { return _this._typeMatcher(i.item); });
        if (index > items.length - 1 || index < 0) {
            return;
        }
        var removeItem = items[index];
        removeItem.item.remove();
    };
    HtmlMarkupCollection.prototype.insert = function (index, item) {
        var _this = this;
        var items = Array.from(this._target.children).map(function (v, i) { return { item: v, index: i }; }).filter(function (i) { return _this._typeMatcher(i.item); });
        if (items.length == 0) {
            this._target.append(item);
        }
        else {
            if (items.length - 1 < index) {
                var insertAfter2 = items[items.length - 1].item;
                insertAfter2.after(item);
            }
            var insertAfter = items[index].item;
            insertAfter.after(item);
        }
    };
    HtmlMarkupCollection.prototype.add = function (item) {
        var _this = this;
        var items = Array.from(this._target.children).map(function (v, i) { return { item: v, index: i }; }).filter(function (i) { return _this._typeMatcher(i.item); });
        if (items.length == 0) {
            this._target.append(item);
        }
        else {
            var insertAfter = items[items.length - 1].item;
            insertAfter.after(item);
        }
    };
    return HtmlMarkupCollection;
}(MarkupCollection));
