import * as tslib_1 from "tslib";
import { fromSize, ensureEnum, brushToString, stringToBrush, ensureBool, initializePropertiesFromCss, NamePatcher, toSpinal, enumToString, getAllPropertyNames, fromSpinal } from "igniteui-webcomponents-core";
import { FontInfo } from "igniteui-webcomponents-core";
import { WebComponentRenderer, PortalManager } from "igniteui-webcomponents-core";
import { CollectionAdapter } from "igniteui-webcomponents-core";
import { NotifyCollectionChangedAction } from "igniteui-webcomponents-core";
import { RegisterElementHelper } from "igniteui-webcomponents-core";
import { PropertyEditor } from "./PropertyEditor";
import { BaseControlTheme_$type } from "igniteui-webcomponents-core";
import { ControlDisplayDensity_$type } from "igniteui-webcomponents-core";
import { IgcHTMLElement } from "igniteui-webcomponents-core";
import { IgcPropertyEditorPropertyDescriptionCollection } from './igc-property-editor-property-description-collection';
import { PropertyEditorPropertyDescriptionCollection as PropertyEditorPropertyDescriptionCollection_internal } from './PropertyEditorPropertyDescriptionCollection';
import { SyncableObservableCollection$1 } from "igniteui-webcomponents-core";
import { PropertyEditorPropertyDescription } from './PropertyEditorPropertyDescription';
import { IgcPropertyEditorPropertyDescriptionComponent } from './igc-property-editor-property-description-component';
import { InputGroupDisplayType_$type } from "igniteui-webcomponents-inputs";
var IgcPropertyEditorComponent = /** @class */ /*@__PURE__*/ (function (_super) {
    tslib_1.__extends(IgcPropertyEditorComponent, _super);
    function IgcPropertyEditorComponent() {
        var _this = _super.call(this) || this;
        _this._implementation = null;
        _this.contentProperties = [];
        /**
        * The properties actually present in the editor. Do not directly modify this array.
        * This array's contents can be modified by using the properties property or providing child content.
        */
        _this.actualProperties = [];
        _this._properties = null;
        _this._propertiesAdapter = null;
        _this._disconnected = false;
        _this._settingAttributes = false;
        _this._attached = false;
        _this._queuedSetAttributes = [];
        _this._updatingFromAttribute = false;
        _this._actualDataSource = null;
        _this.__p = null;
        _this._hasUserValues = new Set();
        _this._stylingContainer = null;
        _this._stylingParent = null;
        _this._inStyling = false;
        if (_this._styling) {
            NamePatcher.ensureStylablePatched(Object.getPrototypeOf(_this));
        }
        _this._implementation = _this.createImplementation();
        _this._implementation.externalObject = _this;
        _this._portalManager = new PortalManager("propertyEditorContent");
        _this._webComponentRenderer = new WebComponentRenderer(_this, document, true, {}, _this._portalManager);
        _this._webComponentWrapper = _this._webComponentRenderer.createElement("div");
        _this._webComponentRenderer.updateRoot(_this._webComponentWrapper);
        _this._webComponentWrapper.setStyleProperty("display", "block");
        _this._webComponentWrapper.setStyleProperty("width", "100%");
        _this._webComponentWrapper.setStyleProperty("height", "100%");
        _this._propertiesAdapter = new CollectionAdapter(_this.contentProperties, _this.i.properties, _this.actualProperties, function (c) { return c.i; }, function (i) {
            if (_this._webComponentRenderer && _this._webComponentRenderer.rootWrapper.getNativeElement().parentElement) {
                i._styling(_this, _this, _this);
            }
        }, function (i) { });
        _this.i.provideContainer(_this._webComponentRenderer);
        _this.i.notifySizeChanged();
        var mut = new MutationObserver(function (list) {
            var e_1, _b;
            try {
                for (var list_1 = tslib_1.__values(list), list_1_1 = list_1.next(); !list_1_1.done; list_1_1 = list_1.next()) {
                    var mutation = list_1_1.value;
                    if (mutation.type == 'childList') {
                        _this.updateContentProperties();
                    }
                }
            }
            catch (e_1_1) {
                e_1 = { error: e_1_1 };
            }
            finally {
                try {
                    if (list_1_1 && !list_1_1.done && (_b = list_1.return))
                        _b.call(list_1);
                }
                finally {
                    if (e_1)
                        throw e_1.error;
                }
            }
        });
        mut.observe(_this, {
            childList: true
        });
        _this._webComponentRenderer.addSizeWatcher(function () {
            _this.i.notifySizeChanged();
        });
        return _this;
    }
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "height", {
        get: function () {
            return this._height;
        },
        set: function (value) {
            this._height = value;
            this.style.height = value;
            this.i.notifySizeChanged();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "width", {
        get: function () {
            return this._width;
        },
        set: function (value) {
            this._width = value;
            this.style.width = value;
            this.i.notifySizeChanged();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "properties", {
        /**
         * A collection of manually added properties for the editor.
        */
        get: function () {
            var _this = this;
            if (this._properties === null) {
                var coll = new IgcPropertyEditorPropertyDescriptionCollection();
                var inner = coll._innerColl;
                inner.addListener(function (sender, e) {
                    switch (e.action) {
                        case NotifyCollectionChangedAction.Add:
                            _this._propertiesAdapter.insertManualItem(e.newStartingIndex, e.newItems.item(0));
                            break;
                        case NotifyCollectionChangedAction.Remove:
                            _this._propertiesAdapter.removeManualItemAt(e.oldStartingIndex);
                            break;
                        case NotifyCollectionChangedAction.Replace:
                            _this._propertiesAdapter.removeManualItemAt(e.oldStartingIndex);
                            _this._propertiesAdapter.insertManualItem(e.newStartingIndex, e.newItems.item(0));
                            break;
                        case NotifyCollectionChangedAction.Reset:
                            _this._propertiesAdapter.clearManualItems();
                            break;
                    }
                });
                this._properties = coll;
            }
            return this._properties;
        },
        enumerable: true,
        configurable: true
    });
    // supports angular themes or custom properties set in CSS
    IgcPropertyEditorComponent.prototype.updateStyle = function () {
        this._styling(this, this);
    };
    IgcPropertyEditorComponent.prototype.destroy = function () {
        this._implementation.destroy();
        this._webComponentRenderer.destroy();
    };
    IgcPropertyEditorComponent.prototype.updateContentProperties = function () {
        this.contentProperties.length = 0;
        for (var i = 0; i < this.children.length; i++) {
            if (this.children[i] instanceof IgcPropertyEditorPropertyDescriptionComponent) {
                this.contentProperties.push(this.children[i]);
            }
        }
        if (this._propertiesAdapter != null) {
            this._propertiesAdapter.notifyContentChanged();
        }
    };
    IgcPropertyEditorComponent.prototype.createImplementation = function () {
        return new PropertyEditor();
    };
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "i", {
        get: function () {
            return this._implementation;
        },
        enumerable: true,
        configurable: true
    });
    IgcPropertyEditorComponent.prototype.disconnectedCallback = function () {
        this._disconnected = true;
    };
    IgcPropertyEditorComponent.prototype.connectedCallback = function () {
        if (this._disconnected) {
            this._disconnected = false;
            return;
        }
        this.classList.add("ig-property-editor");
        this.classList.add("igc-property-editor");
        var rootWrapper = this._webComponentRenderer.rootWrapper;
        var rootElement = rootWrapper.getNativeElement();
        this.appendChild(rootElement);
        this._attached = true;
        this.style.display = "block";
        this.style.height = this._height;
        this.style.width = this._width;
        this._flushQueuedAttributes();
        // supports themes or custom properties set in CSS
        this._styling(this, this);
        for (var i = 0; i < this.actualProperties.length; i++) {
            var s = this.actualProperties[i];
            s._styling(this, this, this);
        }
        this.updateContentProperties();
        this.afterContentInit();
    };
    IgcPropertyEditorComponent.prototype.afterContentInit = function () {
        this.i.notifySizeChanged();
    };
    IgcPropertyEditorComponent.prototype._enqueueSetAttribute = function (attrName, attrValue) {
        this._queuedSetAttributes.push({ attrName: attrName, attrValue: attrValue });
    };
    IgcPropertyEditorComponent.prototype._flushQueuedAttributes = function () {
        this._settingAttributes = true;
        for (var i = 0; i < this._queuedSetAttributes.length; i++) {
            this.setAttribute(this._queuedSetAttributes[i].attrName, this._queuedSetAttributes[i].attrValue);
        }
        this._settingAttributes = false;
        this._queuedSetAttributes.length = 0;
    };
    IgcPropertyEditorComponent.prototype._a = function (attrName, attrValue) {
        if (this._updatingFromAttribute) {
            return;
        }
        if (attrValue) {
            attrValue = attrValue.toString();
        }
        this._settingAttributes = true;
        attrName = toSpinal(attrName);
        if (this._attached) {
            this.setAttribute(attrName, attrValue);
        }
        else {
            this._enqueueSetAttribute(attrName, attrValue);
        }
        this._settingAttributes = false;
    };
    Object.defineProperty(IgcPropertyEditorComponent, "observedAttributes", {
        get: function () {
            if (IgcPropertyEditorComponent._observedAttributesIgcPropertyEditorComponent == null) {
                var names = getAllPropertyNames(IgcPropertyEditorComponent);
                for (var i = 0; i < names.length; i++) {
                    names[i] = toSpinal(names[i]);
                }
                IgcPropertyEditorComponent._observedAttributesIgcPropertyEditorComponent = names;
            }
            return IgcPropertyEditorComponent._observedAttributesIgcPropertyEditorComponent;
        },
        enumerable: true,
        configurable: true
    });
    IgcPropertyEditorComponent.prototype.attributeChangedCallback = function (name, oldValue, newValue) {
        if (this._settingAttributes) {
            return;
        }
        var setName = fromSpinal(name);
        this._updatingFromAttribute = true;
        this[setName] = newValue;
        this._updatingFromAttribute = false;
    };
    IgcPropertyEditorComponent.register = function () {
        if (!IgcPropertyEditorComponent._isElementRegistered) {
            IgcPropertyEditorComponent._isElementRegistered = true;
            RegisterElementHelper.registerElement(IgcPropertyEditorComponent.htmlTagName, IgcPropertyEditorComponent);
        }
    };
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "actualDataSource", {
        /**
         * Gets the actaul data or data source instance to which to bind the grid.
        */
        get: function () {
            if (this._actualDataSource === null) {
                var coll = new IgcPropertyEditorPropertyDescriptionCollection();
                var innerColl = this.i.actualProperties;
                if (!innerColl) {
                    innerColl = new PropertyEditorPropertyDescriptionCollection_internal();
                }
                this._actualDataSource = coll._fromInner(innerColl);
                this.i.actualProperties = innerColl;
            }
            return this._actualDataSource;
        },
        set: function (v) {
            if (this._actualDataSource !== null) {
                this._actualDataSource._setSyncTarget(null);
                this._actualDataSource = null;
            }
            var coll = new IgcPropertyEditorPropertyDescriptionCollection();
            this._actualDataSource = coll._fromOuter(v);
            var syncColl = new SyncableObservableCollection$1(PropertyEditorPropertyDescription.$type);
            var innerColl = this.i.actualProperties;
            if (!innerColl) {
                innerColl = new PropertyEditorPropertyDescriptionCollection_internal();
            }
            syncColl._inner = innerColl;
            syncColl.clear();
            this._actualDataSource._setSyncTarget(syncColl);
            this.i.actualProperties = innerColl;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "filterPlaceholderText", {
        /**
         * Gets or Sets the property name that contains the values.
        */
        get: function () {
            return this.i.b2;
        },
        set: function (v) {
            this.i.b2 = v;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "searchInputType", {
        /**
         * Gets or Sets the property name that contains the values.
        */
        get: function () {
            return this.i.aa;
        },
        set: function (v) {
            this.i.aa = ensureEnum(InputGroupDisplayType_$type, v);
            this._a("searchInputType", enumToString(InputGroupDisplayType_$type, this.i.aa));
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "rowHeight", {
        /**
         * Gets or Sets the property name that contains the values.
        */
        get: function () {
            return this.i.bl;
        },
        set: function (v) {
            this.i.bl = +v;
            this._a("rowHeight", this.i.bl);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "cellTextStyle", {
        /**
         * Gets or Sets the property name that contains the values.
        */
        get: function () {
            if (this.i.ad == null) {
                return null;
            }
            return this.i.ad.fontString;
        },
        set: function (v) {
            var fi = new FontInfo();
            fi.fontString = v;
            this.i.ad = fi;
            this._a("cellTextStyle", this.i.ad != null ? this.i.ad.fontString : "");
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "baseTheme", {
        /**
         * Gets or Sets the property name that contains the values.
        */
        get: function () {
            return this.i.u;
        },
        set: function (v) {
            this.i.u = ensureEnum(BaseControlTheme_$type, v);
            this._a("baseTheme", enumToString(BaseControlTheme_$type, this.i.u));
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "density", {
        /**
         * Gets or Sets the property name that contains the values.
        */
        get: function () {
            return this.i.w;
        },
        set: function (v) {
            this.i.w = ensureEnum(ControlDisplayDensity_$type, v);
            this._a("density", enumToString(ControlDisplayDensity_$type, this.i.w));
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "actualContext", {
        get: function () {
            return this.i.j;
        },
        set: function (v) {
            this.i.j = v;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "descriptionContext", {
        get: function () {
            return this.i.k;
        },
        set: function (v) {
            this.i.k = v;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "componentRenderer", {
        get: function () {
            return this.i.g;
        },
        set: function (v) {
            this.i.g = v;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "target", {
        get: function () {
            return this.i.bn;
        },
        set: function (v) {
            this.i.bn = v;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "descriptionType", {
        get: function () {
            return this.i.b0;
        },
        set: function (v) {
            this.i.b0 = v;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "isHorizontal", {
        get: function () {
            return this.i.am;
        },
        set: function (v) {
            this.i.am = ensureBool(v);
            this._a("isHorizontal", this.i.am);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "isWrappingEnabled", {
        get: function () {
            return this.i.an;
        },
        set: function (v) {
            this.i.an = ensureBool(v);
            this._a("isWrappingEnabled", this.i.an);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "backgroundColor", {
        /**
         * Gets or sets the color to use for the background of the component.
        */
        get: function () {
            return brushToString(this.i.dx);
        },
        set: function (v) {
            this.i.dx = stringToBrush(v);
            this._a("backgroundColor", brushToString(this.i.dx));
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "textColor", {
        /**
         * Gets or sets the color to use for the text of the component.
        */
        get: function () {
            return brushToString(this.i.d1);
        },
        set: function (v) {
            this.i.d1 = stringToBrush(v);
            this._a("textColor", brushToString(this.i.d1));
        },
        enumerable: true,
        configurable: true
    });
    IgcPropertyEditorComponent.prototype.findByName = function (name) {
        if (this.actualDataSource != null && this.actualDataSource.findByName && this.actualDataSource.findByName(name)) {
            return this.actualDataSource.findByName(name);
        }
        if (this.properties != null && this.properties.findByName && this.properties.findByName(name)) {
            return this.properties.findByName(name);
        }
        return null;
    };
    Object.defineProperty(IgcPropertyEditorComponent.prototype, "hasUserValues", {
        get: function () {
            return this._hasUserValues;
        },
        enumerable: true,
        configurable: true
    });
    IgcPropertyEditorComponent.prototype.__m = function (propertyName) {
        if (!this._inStyling) {
            this._hasUserValues.add(propertyName);
        }
    };
    IgcPropertyEditorComponent.prototype._styling = function (container, component, parent) {
        if (this._inStyling) {
            return;
        }
        this._inStyling = true;
        this._stylingContainer = container;
        this._stylingParent = component;
        var genericPrefix = "";
        var typeName = this.i.$type.name;
        if (typeName.indexOf("Xam") === 0) {
            typeName = typeName.substring(3);
        }
        genericPrefix = toSpinal("PropertyEditorComponent");
        var additionalPrefixes = [];
        var prefix = toSpinal(typeName);
        additionalPrefixes.push(prefix + "-");
        var b = this.i.$type.baseType;
        while (b && b.name != "Object" &&
            b.name != "Base" &&
            b.name != "Control" &&
            b.Name != "DependencyObject" &&
            b.Name != "FrameworkElement") {
            typeName = b.name;
            if (typeName.indexOf("Xam") === 0) {
                typeName = typeName.substring(3);
            }
            var basePrefix = toSpinal(typeName);
            additionalPrefixes.push(basePrefix + "-");
            b = b.baseType;
        }
        if (parent) {
            var parentTypeName = parent.i.$type.name;
            if (parentTypeName.indexOf("Xam") === 0) {
                parentTypeName = parentTypeName.substring(3);
            }
            var parentPrefix = toSpinal(parentTypeName);
            additionalPrefixes.push(parentPrefix + "-" + genericPrefix + "-");
            additionalPrefixes.push(parentPrefix + "-" + prefix + "-");
        }
        initializePropertiesFromCss(container, this, genericPrefix + "-", this.hasUserValues, false, additionalPrefixes);
        if (this._otherStyling) {
            this._otherStyling(container, component, parent);
        }
        this._inStyling = false;
    };
    IgcPropertyEditorComponent.prototype.getDesiredSize = function () {
        var iv = this.i.d2();
        return fromSize(iv);
    };
    IgcPropertyEditorComponent.prototype.notifySetItem = function (index, oldItem, newItem) {
        this.i.dh(index, oldItem, newItem);
    };
    /**
     * Manually notifies the checkboxlist's grid that the data it has bound to has been cleared and needs to be re-examined.
     * This should not be called if the data that the grid is bound to is already observable.
    
    */
    IgcPropertyEditorComponent.prototype.notifyClearItems = function () {
        this.i.de();
    };
    IgcPropertyEditorComponent.prototype.notifyInsertItem = function (index, newItem) {
        this.i.df(index, newItem);
    };
    IgcPropertyEditorComponent.prototype.notifyRemoveItem = function (index, oldItem) {
        this.i.dg(index, oldItem);
    };
    IgcPropertyEditorComponent._observedAttributesIgcPropertyEditorComponent = null;
    IgcPropertyEditorComponent.htmlTagName = "igc-property-editor";
    IgcPropertyEditorComponent._isElementRegistered = false;
    return IgcPropertyEditorComponent;
}(IgcHTMLElement));
export { IgcPropertyEditorComponent };
//# sourceMappingURL=igc-property-editor-component.js.map
