/*
    *
    * Wijmo Library 5.20181.462
    * http://wijmo.com/
    *
    * Copyright(c) GrapeCity, Inc.  All rights reserved.
    *
    * Licensed under the GrapeCity Commercial License.
    * sales@wijmo.com
    * wijmo.com/products/wijmo-5/license/
    *
    */
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const wjcCore=require("wijmo/wijmo"),wjcSelfRef=require("wijmo/wijmo.react.base");var wjcSelf=wjcSelfRef||exports;window.wijmo=window.wijmo||{},window.wijmo.react=window.wijmo.react||{},window.wijmo.react.base=wjcSelf;const ReactDOM=require("react-dom"),React=require("react");class ComponentBase extends React.Component{constructor(e,t,i){super(e),this._objPropHash={},this._isMounted=!1,this._mountedCBs=[],this.props=e,this.controlType=t;for(let e of i&&i.objectProps||[])this._objPropHash[e]=!0}render(){let e={};e[ComponentBase._propsParent]=this;let t=React.Children.map(this.props.children,t=>React.cloneElement(t,e));return React.createElement("div",this._isChild()?{style:{display:"none"}}:null,t)}componentDidMount(){if(this._isChild()){let e=this.props[ComponentBase._propsParent];e&&e._mountedCB(()=>{this._setParent(e)})}else this._prepareControl();return this.control}componentWillUnmount(){this._siblingInsertedEH&&this._getElement().removeEventListener("DOMNodeInserted",this._siblingInsertedEH);let e=this.control;if(e)if(this._isChild()){let t=this._getParentProp();if(t){let i=this.parent.control[t];if(wjcCore.isArray(i)){let t=i.indexOf(e);t>-1&&i.splice(t,1)}}}else e instanceof wjcCore.Control&&e.dispose()}shouldComponentUpdate(e){var t=this.control;return this._copy(t,e),!0}_mountedCB(e){this._isMounted?e():this._mountedCBs.push(e)}_createControl(){let e=this._isChild()?this._isParentInCtor()?this.parent.control:void 0:this._getElement();return new this.controlType(e)}_prepareControl(){let e=this.control=this._createControl(),t=this._getElement(),i=this.props,n=e instanceof wjcCore.Control,r=ComponentBase;this._siblingId||(null==this.constructor[r._typeSiblingIdProp]&&(this.constructor[r._typeSiblingIdProp]=++r._siblingDirId+""),this._siblingId=this.constructor[r._typeSiblingIdProp]),t.setAttribute(r._typeSiblingIdProp,this._siblingId);var s={};for(var o in i)if(!this._ignoreProp(o))if(o in e)s[o]=i[o];else switch(o){case"className":wjcCore.addClass(t,i.className);break;case"style":wjcCore.setCss(t,i.style);break;default:null!=t[o]&&(t[o]=i[o])}n?e.initialize(s):this._copy(e,s,!0),this._isMounted=!0;let l=this._mountedCBs;this._mountedCBs=[];for(let e of l)e();wjcCore.isFunction(i.initialized)&&i.initialized(e)}_initParent(){let e=this._getParentProp();if(e){let t=this.parent.control,i=t[e];if(wjcCore.isArray(i)){let e=this._getSiblingIndex();(e<0||e>=i.length)&&(e=i.length),i.splice(e,0,this.control),this._siblingInsertedEH=this._siblingInserted.bind(this),this._getElement().addEventListener("DOMNodeInserted",this._siblingInsertedEH)}else t[e]=this.control}}_setParent(e){if(e!==this.parent){if(this.parent)throw"Wijmo child component is already attached to a different parent.";this.parent=e,this._prepareControl(),this._initParent()}}_isChild(){return null!=this._parentProp||null!=this._parentInCtor}_isParentInCtor(){return!0===this._parentInCtor}_getParentProp(){return this.props.wjProperty||this._parentProp}_getSiblingIndex(){var e=this._getElement(),t=e.parentElement;if(!t)return-1;for(var i=t.childNodes,n=-1,r=this._siblingId,s=0;s<i.length;s++){var o=i[s];if(1==o.nodeType&&o.getAttribute(ComponentBase._typeSiblingIdProp)==r&&(++n,o===e))return n}return-1}_siblingInserted(e){if(e.target===this._getElement()){var t=this._getSiblingIndex(),i=this.control,n=this.parent.control[this._getParentProp()],r=n.indexOf(i);t>=0&&r>=0&&t!==r&&(n.splice(r,1),t=Math.min(t,n.length),n.splice(t,0,i))}}_copy(e,t,i=!1){if(!e||!t)return;let n,r=e===this.control;for(var s in t)if(!this._ignoreProp(s)||!r){var o=t[s];if(s in e){if(this._isEvent(e,s))i&&wjcCore.isFunction(o)&&e[s].addHandler(o);else if(!this._sameValue(e[s],o))if(null==o)e[s]=o;else if(wjcCore.isPrimitive(o)||wjcCore.isFunction(o)||this._objPropHash[s]&&e===(n||(n=this.control)))e[s]=o;else if(wjcCore.isArray(o)&&wjcCore.isArray(e[s])){let t=e[s],i=o;if(i.length==t.length)for(var l=0;l<i.length;l++)this._copy(t[l],i[l])}else wjcCore.isObject(o)&&this._copy(e[s],t[s])}else"className"==s?e.hostElement&&wjcCore.addClass(e.hostElement,t[s]):"style"==s&&e.hostElement&&wjcCore.setCss(e.hostElement,t[s])}}_sameValue(e,t){return e==t||wjcCore.DateTime.equals(e,t)}_isEvent(e,t){let i=e&&e[t];return null!=i&&i instanceof wjcCore.Event}_getElement(){return ReactDOM.findDOMNode(this)}_ignoreProp(e){return"children"===e}}ComponentBase._propsParent="$parent",ComponentBase._typeSiblingIdProp="_wjSiblingIdProp",ComponentBase._siblingDirId=0,exports.ComponentBase=ComponentBase;