import { __extends, __values } from "tslib";
import * as React from 'react';
import { toRect, ensureEnum, brushToString, stringToBrush, ensureBool, initializePropertiesFromCss, NamePatcher, isValidProp, toSpinal, getModifiedProps } from "./componentUtil";
import { ReactRenderer, PortalManager } from "./react-renderer";
import { delegateCombine, delegateRemove } from "./type";
import { Popup } from './Popup';
import { IgrOnPopupEventArgs } from './igr-on-popup-event-args';
import { IgrOnClosedEventArgs } from './igr-on-closed-event-args';
import { IgrFocusEventArgs } from './igr-focus-event-args';
import { PopupAnimationType_$type } from "./PopupAnimationType";
import { PopupPointerPosition_$type } from "./PopupPointerPosition";
var IgrPopup = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(IgrPopup, _super);
    function IgrPopup(props) {
        var _this = _super.call(this, props) || this;
        _this._reactRenderer = null;
        _this._implementation = null;
        _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;
        _this._popupGotFocus = null;
        _this._popupGotFocus_wrapped = null;
        _this._popupLostFocus = null;
        _this._popupLostFocus_wrapped = null;
        if (_this._styling) {
            NamePatcher.ensureStylablePatched(Object.getPrototypeOf(_this));
        }
        _this._implementation = _this.createImplementation();
        _this._getMainRef = _this._getMainRef.bind(_this);
        if (props) {
            _this.initializeProperties();
        }
        _this._initialized = true;
        return _this;
    }
    Object.defineProperty(IgrPopup.prototype, "height", {
        get: function () {
            return this._height;
        },
        set: function (value) {
            this._height = value;
            if (this._elRef) {
                this._elRef.style.height = value;
                this.i.notifySizeChanged();
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "width", {
        get: function () {
            return this._width;
        },
        set: function (value) {
            this._width = value;
            if (this._elRef) {
                this._elRef.style.height = value;
                this.i.notifySizeChanged();
            }
        },
        enumerable: false,
        configurable: true
    });
    IgrPopup.prototype._getMainRef = function (ref) {
        this._elRef = ref;
        this.verifyReferences();
    };
    IgrPopup.prototype.verifyReferences = function () {
        if (this._elRef) {
            this.requestRender = this.requestRender.bind(this);
            this._portalManager = new PortalManager("popupContent", this.requestRender);
            this._reactRenderer = new ReactRenderer(this._elRef, document, true, {}, this._portalManager);
            // render needs to fire again after the portalManager is created. 
            this.requestRender();
            this.i.provideRenderer(this._reactRenderer);
        }
    };
    IgrPopup.prototype.render = function () {
        var propChildren = this.props.children;
        var children = [];
        React.Children.forEach(propChildren, function (ch) {
            children.push(React.cloneElement(ch));
        });
        if (this._portalManager)
            this._portalManager.onRender(children);
        var div = React.createElement("div", {
            ref: this._getMainRef,
            children: children
        });
        return div;
    };
    IgrPopup.prototype.requestRender = function () {
        if (this._initialized)
            this.setState({});
    };
    IgrPopup.prototype.shouldComponentUpdate = function (nextProps, nextState) {
        var e_1, _a;
        var mod = getModifiedProps(this.props, nextProps);
        try {
            for (var _b = __values(Object.keys(mod)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var p = _c.value;
                if (isValidProp(this, p)) {
                    this[p] = mod[p];
                }
            }
        }
        catch (e_1_1) {
            e_1 = { error: e_1_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_1)
                    throw e_1.error;
            }
        }
        return true;
    };
    IgrPopup.prototype.initializeProperties = function () {
        var e_2, _a;
        try {
            for (var _b = __values(Object.keys(this.props)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var p = _c.value;
                if (isValidProp(this, p)) {
                    this[p] = this.props[p];
                }
            }
        }
        catch (e_2_1) {
            e_2 = { error: e_2_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_2)
                    throw e_2.error;
            }
        }
    };
    // supports angular themes or custom properties set in CSS
    IgrPopup.prototype.updateStyle = function () {
        this._styling(this._elRef, this);
    };
    IgrPopup.prototype.destroy = function () {
        this.i.destroy();
        if (this._reactRenderer)
            this._reactRenderer.destroy();
    };
    IgrPopup.prototype.componentWillUnmount = function () {
    };
    IgrPopup.prototype.componentDidMount = function () {
        this._elRef.style.width = this._width ? this._width : "";
        this._elRef.style.height = this._height ? this._height : "";
        this.initializeContent();
    };
    IgrPopup.prototype.initializeContent = function () {
        this._styling(this._elRef, this);
        this.updateStyle();
    };
    IgrPopup.prototype.createImplementation = function () {
        return new Popup();
    };
    Object.defineProperty(IgrPopup.prototype, "i", {
        get: function () {
            return this._implementation;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "isFocusable", {
        get: function () {
            return this.i.aq;
        },
        set: function (v) {
            this.i.aq = ensureBool(v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "animationDuration", {
        /**
         * Gets or sets the duration of the Popup and Closeup animations in milliseconds.
        */
        get: function () {
            return this.i.a6;
        },
        set: function (v) {
            this.i.a6 = +v;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "animationEnabled", {
        /**
         * Gets or sets whether the popup and closeup animations are animated.
        */
        get: function () {
            return this.i.al;
        },
        set: function (v) {
            this.i.al = ensureBool(v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "animationType", {
        /**
         * Gets or sets the type of animation to use when the popup opens and closes.
        */
        get: function () {
            return this.i.o;
        },
        set: function (v) {
            this.i.o = ensureEnum(PopupAnimationType_$type, v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "elevation", {
        /**
         * Gets or sets the elevation to use for the button regardless of type.
        */
        get: function () {
            return this.i.a8;
        },
        set: function (v) {
            this.i.a8 = +v;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "actualElevation", {
        /**
         * Gets or sets the elevation of the border shadow
        */
        get: function () {
            return this.i.a4;
        },
        set: function (v) {
            this.i.a4 = +v;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "isShown", {
        /**
         * Gets / sets the isShown state of the panel.
        */
        get: function () {
            return this.i.av;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "actualUmbraShadowColor", {
        /**
         * Gets or sets the color to use for the check mark when the checkbox is checked.
        */
        get: function () {
            return brushToString(this.i.cm);
        },
        set: function (v) {
            this.i.cm = stringToBrush(v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "actualPenumbraShadowColor", {
        /**
         * Gets or sets the color to use for the check mark when the checkbox is checked.
        */
        get: function () {
            return brushToString(this.i.cl);
        },
        set: function (v) {
            this.i.cl = stringToBrush(v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "actualAmbientShadowColor", {
        /**
         * Gets or sets the color to use for the check mark when the checkbox is checked.
        */
        get: function () {
            return brushToString(this.i.ck);
        },
        set: function (v) {
            this.i.ck = stringToBrush(v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "background", {
        /**
         * Gets or sets the background color of the popup.
        */
        get: function () {
            return brushToString(this.i.cn);
        },
        set: function (v) {
            this.i.cn = stringToBrush(v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "cornerRadius", {
        get: function () {
            return this.i.ay;
        },
        set: function (v) {
            this.i.ay = +v;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "isHitTestVisible", {
        /**
         * Gets or sets whether to disable hit testing on the popup.
        */
        get: function () {
            return this.i.ar;
        },
        set: function (v) {
            this.i.ar = ensureBool(v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "disableHitTestDuringAnimation", {
        /**
         * Gets or sets whether to disable hit testing during the open and close animations.
        */
        get: function () {
            return this.i.an;
        },
        set: function (v) {
            this.i.an = ensureBool(v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "isPointerEnabled", {
        /**
         * Gets or sets whether to show a pointer off the side of the popup towards the popup target.
        */
        get: function () {
            return this.i.as;
        },
        set: function (v) {
            this.i.as = ensureBool(v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "pointerPosition", {
        /**
         * Gets or sets the pointer position.
        */
        get: function () {
            return this.i.t;
        },
        set: function (v) {
            this.i.t = ensureEnum(PopupPointerPosition_$type, v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "pointerBackground", {
        /**
         * Gets or sets the pointer background color.
        */
        get: function () {
            return brushToString(this.i.cr);
        },
        set: function (v) {
            this.i.cr = stringToBrush(v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "pointerSize", {
        /**
         * Gets or sets the pointer size.
        */
        get: function () {
            return this.i.a1;
        },
        set: function (v) {
            this.i.a1 = +v;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "isShowing", {
        /**
         * Gets whether the popup is in the middle of opening or not.
        */
        get: function () {
            return this.i.at;
        },
        set: function (v) {
            this.i.at = ensureBool(v);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "isClosing", {
        /**
         * Gets whether the popup is in the middle of closing or not.
        */
        get: function () {
            return this.i.ap;
        },
        set: function (v) {
            this.i.ap = ensureBool(v);
        },
        enumerable: false,
        configurable: true
    });
    IgrPopup.prototype.findByName = function (name) {
        if (this.findEphemera) {
            if (name && name.indexOf("@@e:") == 0) {
                return this.findEphemera(name);
            }
        }
        return null;
    };
    Object.defineProperty(IgrPopup.prototype, "hasUserValues", {
        get: function () {
            return this._hasUserValues;
        },
        enumerable: false,
        configurable: true
    });
    IgrPopup.prototype.__m = function (propertyName) {
        if (!this._inStyling) {
            this._hasUserValues.add(propertyName);
        }
    };
    IgrPopup.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("Popup");
        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;
    };
    /**
 * Closes the popup.

*/
    IgrPopup.prototype.close = function () {
        this.i.bx();
    };
    IgrPopup.prototype.showRelativeToExclusionRect = function (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.
    
    */
    IgrPopup.prototype.exportVisualModel = function () {
        var iv = this.i.a9();
        return (iv);
    };
    /**
     * Returns a serialized copy of the exported visual model
    
    */
    IgrPopup.prototype.exportSerializedVisualModel = function () {
        var iv = this.i.bl();
        return (iv);
    };
    Object.defineProperty(IgrPopup.prototype, "onClosed", {
        get: function () {
            return this._onClosed;
        },
        set: function (ev) {
            var _this = this;
            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 = function (o, e) {
                var outerArgs = new IgrOnClosedEventArgs();
                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);
            ;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "onPopup", {
        /**
         * Fires when the popup is shown
        */
        get: function () {
            return this._onPopup;
        },
        set: function (ev) {
            var _this = this;
            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 = function (o, e) {
                var outerArgs = new IgrOnPopupEventArgs();
                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);
            ;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "popupGotFocus", {
        /**
         * Fired when the open popup gains focus.
        */
        get: function () {
            return this._popupGotFocus;
        },
        set: function (ev) {
            var _this = this;
            if (this._popupGotFocus_wrapped !== null) {
                this.i.popupGotFocus = delegateRemove(this.i.popupGotFocus, this._popupGotFocus_wrapped);
                this._popupGotFocus_wrapped = null;
                this._popupGotFocus = null;
            }
            this._popupGotFocus = ev;
            this._popupGotFocus_wrapped = function (o, e) {
                var outerArgs = new IgrFocusEventArgs();
                outerArgs._provideImplementation(e);
                if (_this.beforePopupGotFocus) {
                    _this.beforePopupGotFocus(_this, outerArgs);
                }
                if (_this._popupGotFocus) {
                    _this._popupGotFocus(_this, outerArgs);
                }
            };
            this.i.popupGotFocus = delegateCombine(this.i.popupGotFocus, this._popupGotFocus_wrapped);
            ;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(IgrPopup.prototype, "popupLostFocus", {
        /**
         * Fired when the open popup loses focus.
        */
        get: function () {
            return this._popupLostFocus;
        },
        set: function (ev) {
            var _this = this;
            if (this._popupLostFocus_wrapped !== null) {
                this.i.popupLostFocus = delegateRemove(this.i.popupLostFocus, this._popupLostFocus_wrapped);
                this._popupLostFocus_wrapped = null;
                this._popupLostFocus = null;
            }
            this._popupLostFocus = ev;
            this._popupLostFocus_wrapped = function (o, e) {
                var outerArgs = new IgrFocusEventArgs();
                outerArgs._provideImplementation(e);
                if (_this.beforePopupLostFocus) {
                    _this.beforePopupLostFocus(_this, outerArgs);
                }
                if (_this._popupLostFocus) {
                    _this._popupLostFocus(_this, outerArgs);
                }
            };
            this.i.popupLostFocus = delegateCombine(this.i.popupLostFocus, this._popupLostFocus_wrapped);
            ;
        },
        enumerable: false,
        configurable: true
    });
    return IgrPopup;
}(React.Component));
export { IgrPopup };
