import { toRect, brushToString, stringToBrush, ensureBool, initializePropertiesFromCss, NamePatcher, toSpinal, getAllPropertyNames, fromSpinal } from "./componentUtil";
import { WebComponentRenderer, PortalManager } from "./web-component-renderer";
import { delegateCombine, delegateRemove } from "./type";
import { RegisterElementHelper } from "./module-manager";
import { Popup } from "./Popup";
import { IgcOnPopupEventArgs } from './igc-on-popup-event-args';
import { IgcOnClosedEventArgs } from './igc-on-closed-event-args';
import { IgcHTMLElement } from "./igc-html-element";
export let IgcPopupComponent = /*@__PURE__*/ (() => {
    class IgcPopupComponent extends IgcHTMLElement {
        constructor() {
            super();
            this._implementation = null;
            this._disconnected = false;
            this._settingAttributes = false;
            this._attached = false;
            this._queuedSetAttributes = [];
            this._updatingFromAttribute = false;
            this.__p = null;
            this._hasUserValues = new Set();
            this._stylingContainer = null;
            this._stylingParent = null;
            this._inStyling = false;
            this._onClosed = null;
            this._onClosed_wrapped = null;
            this._onPopup = null;
            this._onPopup_wrapped = null;
            if (this._styling) {
                NamePatcher.ensureStylablePatched(Object.getPrototypeOf(this));
            }
            this._implementation = this.createImplementation();
            this._portalManager = new PortalManager("popupContent");
            this._webComponentRenderer = new WebComponentRenderer(this, document, true, {}, this._portalManager);
            this._mainDiv = this._webComponentRenderer.createElement("div");
            this._webComponentRenderer.updateRoot(this._mainDiv);
            this.i.provideRenderer(this._webComponentRenderer);
            let mut = new MutationObserver((list) => {
                for (var mutation of list) {
                    if (mutation.type == 'childList') {
                        this._onChildrenChanged();
                    }
                }
            });
            mut.observe(this, {
                childList: true
            });
        }
        set height(value) {
            this._height = value;
            this.style.height = value;
        }
        get height() {
            return this._height;
        }
        set width(value) {
            this._width = value;
            this.style.width = value;
        }
        get width() {
            return this._width;
        }
        _onChildrenChanged() {
            var children = [];
            for (var i = 0; i < this.childNodes.length; i++) {
                var child = this.childNodes[i];
                if (child !== this._mainDiv.getNativeElement()) {
                    children.push(child);
                }
            }
            this._portalManager.onChildrenChanged(children);
        }
        // supports angular themes or custom properties set in CSS
        updateStyle() {
            this._styling(this, this);
        }
        destroy() {
            this._implementation.destroy();
            this._webComponentRenderer.destroy();
        }
        createImplementation() {
            return new Popup();
        }
        get i() {
            return this._implementation;
        }
        disconnectedCallback() {
            this._disconnected = true;
        }
        connectedCallback() {
            if (this._disconnected) {
                this._disconnected = false;
                return;
            }
            this.classList.add("ig-popup");
            this.classList.add("igr-popup");
            this.appendChild(this._mainDiv.getNativeElement());
            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);
            this.afterContentInit();
        }
        afterContentInit() {
        }
        _enqueueSetAttribute(attrName, attrValue) {
            this._queuedSetAttributes.push({ attrName: attrName, attrValue: attrValue });
        }
        _flushQueuedAttributes() {
            this._settingAttributes = true;
            for (let i = 0; i < this._queuedSetAttributes.length; i++) {
                this.setAttribute(this._queuedSetAttributes[i].attrName, this._queuedSetAttributes[i].attrValue);
            }
            this._settingAttributes = false;
            this._queuedSetAttributes.length = 0;
        }
        _a(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;
        }
        static get observedAttributes() {
            if (IgcPopupComponent._observedAttributesIgcPopupComponent == null) {
                let names = getAllPropertyNames(IgcPopupComponent);
                for (let i = 0; i < names.length; i++) {
                    names[i] = toSpinal(names[i]);
                }
                IgcPopupComponent._observedAttributesIgcPopupComponent = names;
            }
            return IgcPopupComponent._observedAttributesIgcPopupComponent;
        }
        attributeChangedCallback(name, oldValue, newValue) {
            if (this._settingAttributes) {
                return;
            }
            let setName = fromSpinal(name);
            this._updatingFromAttribute = true;
            this[setName] = newValue;
            this._updatingFromAttribute = false;
        }
        static register() {
            if (!IgcPopupComponent._isElementRegistered) {
                IgcPopupComponent._isElementRegistered = true;
                RegisterElementHelper.registerElement(IgcPopupComponent.htmlTagName, IgcPopupComponent);
            }
        }
        /**
     * Gets or sets the duration of the Popup and Closeup animations in milliseconds.
    */
        get animationDuration() {
            return this.i.y;
        }
        set animationDuration(v) {
            this.i.y = +v;
            this._a("animationDuration", this.i.y);
        }
        /**
         * Gets or sets whether the popup and closeup animations are animated.
        */
        get animationEnabled() {
            return this.i.q;
        }
        set animationEnabled(v) {
            this.i.q = ensureBool(v);
            this._a("animationEnabled", this.i.q);
        }
        /**
         * Gets or sets the elevation to use for the button regardless of type.
        */
        get elevation() {
            return this.i.aa;
        }
        set elevation(v) {
            this.i.aa = +v;
            this._a("elevation", this.i.aa);
        }
        /**
         * Gets or sets the elevation of the border shadow
        */
        get actualElevation() {
            return this.i.w;
        }
        set actualElevation(v) {
            this.i.w = +v;
            this._a("actualElevation", this.i.w);
        }
        /**
         * Gets / sets the isShown state of the panel.
        */
        get isShown() {
            return this.i.t;
        }
        /**
         * Gets or sets the color to use for the check mark when the checkbox is checked.
        */
        get actualUmbraShadowColor() {
            return brushToString(this.i.a7);
        }
        set actualUmbraShadowColor(v) {
            this.i.a7 = stringToBrush(v);
            this._a("actualUmbraShadowColor", brushToString(this.i.a7));
        }
        /**
         * Gets or sets the color to use for the check mark when the checkbox is checked.
        */
        get actualPenumbraShadowColor() {
            return brushToString(this.i.a6);
        }
        set actualPenumbraShadowColor(v) {
            this.i.a6 = stringToBrush(v);
            this._a("actualPenumbraShadowColor", brushToString(this.i.a6));
        }
        /**
         * Gets or sets the color to use for the check mark when the checkbox is checked.
        */
        get actualAmbientShadowColor() {
            return brushToString(this.i.a5);
        }
        set actualAmbientShadowColor(v) {
            this.i.a5 = stringToBrush(v);
            this._a("actualAmbientShadowColor", brushToString(this.i.a5));
        }
        /**
         * Gets or sets the background color of the popup.
        */
        get background() {
            return brushToString(this.i.a8);
        }
        set background(v) {
            this.i.a8 = stringToBrush(v);
            this._a("background", brushToString(this.i.a8));
        }
        findByName(name) {
            if (this.findEphemera) {
                if (name && name.indexOf("@@e:") == 0) {
                    return this.findEphemera(name);
                }
            }
            return null;
        }
        get hasUserValues() {
            return this._hasUserValues;
        }
        __m(propertyName) {
            if (!this._inStyling) {
                this._hasUserValues.add(propertyName);
            }
        }
        _styling(container, component, parent) {
            if (this._inStyling) {
                return;
            }
            this._inStyling = true;
            this._stylingContainer = container;
            this._stylingParent = component;
            let genericPrefix = "";
            let typeName = this.i.$type.name;
            if (typeName.indexOf("Xam") === 0) {
                typeName = typeName.substring(3);
            }
            genericPrefix = toSpinal("PopupComponent");
            let additionalPrefixes = [];
            let prefix = toSpinal(typeName);
            additionalPrefixes.push(prefix + "-");
            let 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);
                }
                let basePrefix = toSpinal(typeName);
                additionalPrefixes.push(basePrefix + "-");
                b = b.baseType;
            }
            if (parent) {
                let parentTypeName = parent.i.$type.name;
                if (parentTypeName.indexOf("Xam") === 0) {
                    parentTypeName = parentTypeName.substring(3);
                }
                let 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;
        }
        /**
     * Closes the popup.
    
    */
        close() {
            this.i.ao();
        }
        showRelativeToExclusionRect(exclusionRect, popupDirection, popupAlignment) {
            this.i.showRelativeToExclusionRect(toRect(exclusionRect), (popupDirection == null ? null : popupDirection), (popupAlignment == null ? null : popupAlignment));
        }
        /**
         * Exports visual information about the current state of the grid.
        
        */
        exportVisualModel() {
            let iv = this.i.ab();
            return (iv);
        }
        /**
         * Returns a serialized copy of the exported visual model
        
        */
        exportSerializedVisualModel() {
            let iv = this.i.ak();
            return (iv);
        }
        get onClosed() {
            return this._onClosed;
        }
        set onClosed(ev) {
            if (this._onClosed_wrapped !== null) {
                this.i.onClosed = delegateRemove(this.i.onClosed, this._onClosed_wrapped);
                this._onClosed_wrapped = null;
                this._onClosed = null;
            }
            this._onClosed = ev;
            this._onClosed_wrapped = (o, e) => {
                let outerArgs = new IgcOnClosedEventArgs();
                outerArgs._provideImplementation(e);
                if (this.beforeOnClosed) {
                    this.beforeOnClosed(this, outerArgs);
                }
                if (this._onClosed) {
                    this._onClosed(this, outerArgs);
                }
            };
            this.i.onClosed = delegateCombine(this.i.onClosed, this._onClosed_wrapped);
            ;
        }
        /**
         * Fires when the popup is shown
        */
        get onPopup() {
            return this._onPopup;
        }
        set onPopup(ev) {
            if (this._onPopup_wrapped !== null) {
                this.i.onPopup = delegateRemove(this.i.onPopup, this._onPopup_wrapped);
                this._onPopup_wrapped = null;
                this._onPopup = null;
            }
            this._onPopup = ev;
            this._onPopup_wrapped = (o, e) => {
                let outerArgs = new IgcOnPopupEventArgs();
                outerArgs._provideImplementation(e);
                if (this.beforeOnPopup) {
                    this.beforeOnPopup(this, outerArgs);
                }
                if (this._onPopup) {
                    this._onPopup(this, outerArgs);
                }
            };
            this.i.onPopup = delegateCombine(this.i.onPopup, this._onPopup_wrapped);
            ;
        }
    }
    IgcPopupComponent._observedAttributesIgcPopupComponent = null;
    IgcPopupComponent.htmlTagName = "igc-popup";
    IgcPopupComponent._isElementRegistered = false;
    return IgcPopupComponent;
})();
