import { TypeDescriptionMetadata } from './TypeDescriptionMetadata';
import { TypeDescriptionWellKnownType } from './TypeDescriptionWellKnownType';
import { IgCollection } from './IgCollection';
import { TypeRegistrar } from './type';
import { IgcComponentRendererContainerComponent } from './igc-component-renderer-container-component';
var ComponentRendererAdapter = /** @class */ /*@__PURE__*/ (function () {
    function ComponentRendererAdapter() {
        this._platform = "Igc";
    }
    ComponentRendererAdapter.prototype.createObject = function (type, container, context) {
        var meta = context.getMetadata(type, "__qualifiedNameTS");
        if (meta) {
            type = meta.specificExternalType;
        }
        var typeName = "Igc" + type + "Component";
        if (TypeRegistrar.isRegistered(typeName)) {
            return TypeRegistrar.create(typeName);
        }
        var shortTypeName = "Igc" + type;
        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.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 = 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.setPropertyValue = function (target, propertyName, propertyMetadata, value, oldValue, sourceRef) {
        if (this._platform == "Igx" &&
            IgcComponentRendererContainerComponent.isEvent &&
            IgcComponentRendererContainerComponent.isEvent(target[propertyName])) {
            if (target["_" + propertyName + "Subscription"]) {
                target["_" + propertyName + "Subscription"]();
                target["_" + propertyName + "Subscription"] = null;
            }
            var fun = function (ev) {
                value(ev.sender, ev.args);
            };
            var sub = target[propertyName].subscribe(fun);
            target["_" + propertyName + "Subscription"] = sub;
            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 (oldValue && oldValue._implementation) {
            value.___parent = null;
        }
        if (value && value._implementation) {
            value.___parent = target;
        }
        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 = IgcComponentRendererContainerComponent.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 = IgcComponentRendererContainerComponent.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.clearCollection = function (target, propertyName, metadata) {
        var 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 = 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 = 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 = 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 = "Igc" + type + "Component";
        var meta = context.getMetadata(type, "__qualifiedNameTS");
        if (meta) {
            typeName = "Igc" + meta.specificExternalType + "Component";
        }
        if (TypeRegistrar.isRegistered(typeName)) {
            var t = TypeRegistrar.get(typeName);
            if (this._platform === "Igc") {
                var crc_3 = IgcComponentRendererContainerComponent.fromElement(container);
                crc_3.replaceRootItem(t, continueActions);
                return;
            }
            var crc = container;
            crc.replaceRootItem(t, continueActions);
        }
        else {
            if (this._platform === "Igc") {
                var crc_4 = IgcComponentRendererContainerComponent.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 };
