/*
    *
    * Wijmo Library 5.20183.550
    * 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/
    *
    */
function tryGetModuleWijmoGrid(){let e;return(e=__glob.wijmo)&&e.grid}function tryGetModuleWijmoGridFilter(){let e,t;return(e=__glob.wijmo)&&(t=e.grid)&&t.filter}import*as wjcCore from"wijmo/wijmo";var __glob="undefined"!=typeof window?window:self;import*as wjcSelfRef from"wijmo/wijmo.odata";var wjcSelf=wjcSelfRef||exports;__glob.wijmo=__glob.wijmo||{},__glob.wijmo.odata=wjcSelf;export class ODataCollectionView extends wjcCore.CollectionView{constructor(e,t,r){super(),this._count=0,this._sortOnServer=!0,this._pageOnServer=!0,this._filterOnServer=!0,this._showDatesAsGmt=!1,this._inferDataTypes=!0,this._reviverBnd=this._reviver.bind(this),this.loading=new wjcCore.Event,this.loaded=new wjcCore.Event,this.error=new wjcCore.Event,this._url=wjcCore.asString(e,!1),this._tbl=wjcCore.asString(t),r&&wjcCore.copy(this,r),this.sortDescriptions.collectionChanged.addHandler(()=>{this.sortOnServer&&this._getData()}),this._getData()}get tableName(){return this._tbl}get fields(){return this._fields}set fields(e){this._fields!=e&&(this._fields=wjcCore.asArray(e),this._getData())}get requestHeaders(){return this._requestHeaders}set requestHeaders(e){this._requestHeaders=e}get keys(){return this._keys}set keys(e){this._keys=wjcCore.asArray(e)}get expand(){return this._expand}set expand(e){this._expand=wjcCore.asString(e)}get jsonReviver(){return this._jsonReviver}set jsonReviver(e){this._jsonReviver=wjcCore.asFunction(e)}get dataTypes(){return this._dataTypes}set dataTypes(e){this._dataTypes=e}get inferDataTypes(){return this._inferDataTypes}set inferDataTypes(e){e!=this.inferDataTypes&&(this._inferDataTypes=wjcCore.asBoolean(e),this._getData())}get showDatesAsGmt(){return this._showDatesAsGmt}set showDatesAsGmt(e){e!=this.showDatesAsGmt&&(this._showDatesAsGmt=wjcCore.asBoolean(e),this._getData())}get sortOnServer(){return this._sortOnServer}set sortOnServer(e){e!=this._sortOnServer&&(this._sortOnServer=wjcCore.asBoolean(e),this._getData())}get pageOnServer(){return this._pageOnServer}set pageOnServer(e){e!=this._pageOnServer&&(this._pageOnServer=wjcCore.asBoolean(e),this.pageSize&&this._getData())}get filterOnServer(){return this._filterOnServer}set filterOnServer(e){e!=this._filterOnServer&&(this._filterOnServer=wjcCore.asBoolean(e),this._getData())}get filterDefinition(){return this._filterDef}set filterDefinition(e){e!=this._filterDef&&(this._filterDef=wjcCore.asString(e),this._getData())}updateFilterDefinition(e){this.filterOnServer&&tryGetModuleWijmoGrid()&&tryGetModuleWijmoGridFilter()&&e instanceof tryGetModuleWijmoGridFilter().FlexGridFilter&&(this.filterDefinition=this._asODataFilter(e))}get oDataVersion(){return this._odv}set oDataVersion(e){this._odv=wjcCore.asNumber(e)}get isLoading(){return this._loading}onLoading(e){this.loading.raise(this,e)}onLoaded(e){this.loaded.raise(this,e)}load(){this._getData()}onError(e){return this.error.raise(this,e),!e.cancel}commitNew(){let e={Accept:"application/json"};if(this.requestHeaders)for(let t in this.requestHeaders)e[t]=this.requestHeaders[t];let t=this.currentAddItem;if(t){let r=this._getWriteUrl();wjcCore.httpRequest(r,{method:"POST",requestHeaders:e,data:this._convertToDbFormat(t),success:e=>{let r=JSON.parse(e.responseText,this._reviverBnd);this.keys.forEach(e=>{t[e]=r[e]}),this.refresh()},error:this._error.bind(this)})}super.commitNew()}commitEdit(){let e=this.currentEditItem;if(e&&!this.currentAddItem&&!this._sameContent(e,this._edtClone)){let t=this._getWriteUrl(this._edtClone);wjcCore.httpRequest(t,{method:"PUT",requestHeaders:this.requestHeaders,data:this._convertToDbFormat(e),error:this._error.bind(this)})}super.commitEdit()}remove(e){if(e&&e!=this.currentAddItem&&this.items.indexOf(e)>-1){let t=this._getWriteUrl(e);wjcCore.httpRequest(t,{method:"DELETE",requestHeaders:this.requestHeaders,error:this._error.bind(this)})}super.remove(e)}get totalItemCount(){return this._count}get pageCount(){return this.pageSize?Math.ceil(this.totalItemCount/this.pageSize):1}get pageSize(){return this._pgSz}set pageSize(e){e!=this._pgSz&&(this._pgSz=wjcCore.asInt(e),this.pageOnServer?(this._pgIdx=wjcCore.clamp(this._pgIdx,0,this.pageCount-1),this._getData()):this.refresh())}onPageChanging(e){return super.onPageChanging(e),!e.cancel&&this.pageOnServer&&this._getData(),!e.cancel}_getPageView(){return this.pageOnServer?this._view:super._getPageView()}_performRefresh(){let e=this._canFilter,t=this._canSort;this._canFilter=!this._filterOnServer,this._canSort=!this._sortOnServer,super._performRefresh(),this._canFilter=e,this._canSort=t}_storeItems(e,t){t?Array.prototype.push.apply(this.sourceCollection,e):this.sourceCollection=e}_getReadUrl(e){let t=this._url;return"/"!=t[t.length-1]&&(t+="/"),e?t=0==e.indexOf("http")?e:t+e:this._tbl&&(t+=this._tbl),t}_getReadParams(e){let t={$format:"json"};if(this._tbl&&!e){if(this._odv<4?t.$inlinecount="allpages":t.$count=!0,this.fields&&(t.$select=this.fields.join(",")),this.expand&&(t.$expand=this.expand),this.sortOnServer&&this.sortDescriptions.length){let e="";for(let t=0;t<this.sortDescriptions.length;t++){let r=this.sortDescriptions[t];e&&(e+=","),e+=r.property,r.ascending||(e+=" desc")}t.$orderby=e}this.pageOnServer&&this.pageSize>0&&(t.$skip=this.pageIndex*this.pageSize,t.$top=this.pageSize),this.filterDefinition&&(t.$filter=this.filterDefinition)}return t}_getData(e,t){this._toGetData&&clearTimeout(this._toGetData),this._toGetData=setTimeout(()=>{if(null==this._odv)return void this._getSchema();this._loading=!0,this.onLoading();let r=wjcCore.httpRequest(this._getReadUrl(e),{requestHeaders:this.requestHeaders,data:this._getReadParams(e),success:t=>{let r=JSON.parse(t.responseText,this._reviverBnd),s=r.d?r.d.results:r.value,i=r.d?r.d.__count:r["odata.count"]||r["@odata.count"];if(null!=i&&(this._count=parseInt(i)),this.pageIndex>0&&this.pageIndex>=this.pageCount){let e=this.pageIndex;if(this.moveToLastPage(),this.pageIndex!=e)return}e||this.inferDataTypes&&!this._dataTypesInferred&&(this._dataTypesInferred=this._getInferredDataTypes(s));let a=this.dataTypes?this.dataTypes:this._dataTypesInferred;if(a)for(let e=0;e<s.length;e++)this._convertItem(a,s[e]);this._storeItems(s,null!=e),this.refresh(),(e=r.d?r.d.__next:r["odata.nextLink"]||r["@odata.nextLink"])?this._getData(e):(this._loading=!1,this.onLoaded())},error:e=>{if(this._loading=!1,this.onLoaded(),this.onError(new wjcCore.RequestErrorEventArgs(e)))throw"HttpRequest Error: "+e.status+" "+e.statusText}});wjcCore.isFunction(t)&&t(r)},100)}_convertToDbFormat(e){let t={};for(let r in e){let s=e[r];wjcCore.isDate(s)&&this._showDatesAsGmt?s=new Date(s.getTime()-6e4*s.getTimezoneOffset()):wjcCore.isNumber(s)&&this._odv<4&&(s=s.toString()),t[r]=s}return t}_reviver(e,t){return"string"==typeof t&&ODataCollectionView._rxDate.test(t)&&(t=0==t.indexOf("/Date(")?new Date(parseInt(t.substr(6))):new Date(t),wjcCore.isDate(t)&&this._showDatesAsGmt&&(t=new Date(t.getTime()+6e4*t.getTimezoneOffset()))),this._jsonReviver?this._jsonReviver(e,t):t}_convertItem(e,t){for(let r in e){let s=e[r],i=t[r];null!=i&&(i=s==wjcCore.DataType.Date&&wjcCore.isString(i)&&0==i.indexOf("/Date(")?new Date(parseInt(i.substr(6))):wjcCore.changeType(i,s,null),wjcCore.isDate(i)&&this._showDatesAsGmt&&(i=new Date(i.getTime()+6e4*i.getTimezoneOffset())),t[r]=i)}}_getInferredDataTypes(e){let t=null;if(e.length>0){let r={};for(let t=0;t<e.length&&t<10;t++)this._extend(r,e[t]);for(let e in r){let s=r[e];wjcCore.isString(s)&&ODataCollectionView._rxDate.test(s)&&(t||(t={}),t[e]=wjcCore.DataType.Date)}}return t}_getServiceUrl(){let e=this._url;return"/"!=e[e.length-1]&&(e+="/"),e}_getSchema(){let e=this._getServiceUrl()+"$metadata",t=ODataCollectionView._odvCache;this._odv=t[e],this._odv?this._getData():wjcCore.httpRequest(e,{requestHeaders:this.requestHeaders,success:r=>{let s=r.responseText.match(/<.*Version\s*=\s*"([0-9.]+)"/),i=s?parseFloat(s[1]):4;t[e]=this._odv=i},error:r=>{t[e]=this._odv=4},complete:e=>{this._getData()}})}_getWriteUrl(e){let t=this._getServiceUrl();if(t+=this._tbl,e){wjcCore.assert(this.keys&&this.keys.length>0,"write operations require keys.");let r=[];this.keys.forEach(t=>{wjcCore.assert(null!=e[t],"key values cannot be null."),r.push(t+"="+e[t])}),t+="("+r.join(",")+")"}return t}_asODataFilter(e){let t="";for(let r=0;r<e.grid.columns.length;r++){let s=e.grid.columns[r],i=e.getColumnFilter(s,!1);i&&i.isActive&&(t&&(t+=" and "),i.conditionFilter&&i.conditionFilter.isActive?t+=this._asODataConditionFilter(i.conditionFilter):i.valueFilter&&i.valueFilter.isActive&&(t+=this._asODataValueFilter(i.valueFilter)))}return t}_asODataValueFilter(e){let t=e.column,r=t.binding,s=t.dataMap,i=(e._getUniqueValues(t,!1),"");for(let a in e.showValues){let e=wjcCore.changeType(a,t.dataType,t.format);s&&wjcCore.isString(e)&&(e=s.getKeyValue(e)),i&&(i+=" or "),i+=this._asEquals(r,e,t.dataType)}return i.length&&(i="("+i+")"),i}_asEquals(e,t,r){return r==wjcCore.DataType.Date?"("+e+" ge "+this._asODataValue(t,r)+" and "+e+" lt "+this._asODataValue(wjcCore.DateTime.addDays(t,1),r)+")":"("+e+" eq "+this._asODataValue(t,r)+")"}_asODataConditionFilter(e){let t=this._asODataCondition(e,e.condition1),r=this._asODataCondition(e,e.condition2);return t&&r?"("+t+(e.and?" and ":" or ")+r+")":t?"("+t+")":r?"("+r+")":null}_asODataCondition(e,t){let r=e.column.binding,s=this._asODataValue(t.value,e.column.dataType);switch(t.operator){case 0:return this._asEquals(r,t.value,e.column.dataType);case 1:return r+" ne "+s;case 2:return r+" gt "+s;case 3:return r+" ge "+s;case 4:return r+" lt "+s;case 5:return r+" le "+s;case 6:return"startswith("+r+","+s+")";case 7:return"endswith("+r+","+s+")";case 8:return this._odv>=4?"contains("+r+","+s+")":"substringof("+s.toLowerCase()+", tolower("+r+"))";case 9:return this._odv>=4?"not contains("+r+","+s+")":"not substringof("+s.toLowerCase()+", tolower("+r+"))"}}_asODataValue(e,t){return wjcCore.isDate(e)?(this._showDatesAsGmt&&(e=new Date(e.getTime()-6e4*e.getTimezoneOffset())),e=e.toJSON(),this._odv<4&&(e="datetime'"+e.substr(0,10)+"'"),e):wjcCore.isString(e)?"'"+e.replace(/'/g,"''")+"'":null!=e?e.toString():t==wjcCore.DataType.String?"''":null}_error(e){if(this.onError(new wjcCore.RequestErrorEventArgs(e)))throw this._getData(),"HttpRequest Error: "+e.status+" "+e.statusText}};ODataCollectionView._odvCache={},ODataCollectionView._rxDate=/^\d{4}\-\d{2}\-\d{2}T\d{2}\:\d{2}\:\d{2}|\/Date\([\d\-]*?\)/;export class ODataVirtualCollectionView extends ODataCollectionView{constructor(e,t,r){null==r&&(r={}),r.pageOnServer=!0,r.sortOnServer=!0,r.canGroup=!1,r.pageSize||(r.pageSize=100),super(e,t,r),this._data=[],this.sourceCollection=this._data,this._skip=0,this.setWindow(0,this.pageSize)}setWindow(e,t){this._toSetWindow&&clearTimeout(this._toSetWindow),this._toSetWindow=setTimeout(()=>{this._toSetWindow=null,this._performSetWindow(e,t)},50)}get pageOnServer(){return!0}set pageOnServer(e){if(!e)throw"ODataVirtualCollectionView requires pageOnServer = true."}get sortOnServer(){return!0}set sortOnServer(e){if(!wjcCore.asBoolean(e))throw"ODataVirtualCollectionView requires sortOnServer = true."}get filterOnServer(){return!0}set filterOnServer(e){if(!wjcCore.asBoolean(e))throw"ODataVirtualCollectionView requires filterOnServer = true."}get canGroup(){return this._canGroup}set canGroup(e){if(wjcCore.asBoolean(e))throw"ODataVirtualCollectionView does not support grouping."}_performRefresh(){this.isLoading||(this._refresh=!0),super._performRefresh()}_getReadParams(e){var t=super._getReadParams(e);return e||(t.$skip=this._skip||0,t.$top=this.pageSize),t}_storeItems(e,t){if(this._refresh||this._data.length!=this.totalItemCount){this._data.length=this.totalItemCount;for(s=0;s<this._data.length;s++)this._data[s]=null;this._refresh=!1}t||(this.currentPosition<0&&this.moveCurrentToFirst(),this._loadOffset=0);for(var r=this._loadOffset+(this._skip||0),s=0;s<e.length;s++)this._data[s+r]=e[s];this._loadOffset+=e.length}_performSetWindow(e,t){this._pendingRequest&&(this._pendingRequest.abort(),this._pendingRequest=null),e=wjcCore.asInt(e),t=wjcCore.asInt(t),wjcCore.assert(t>=e,"Start must be smaller than end.");var r=wjcCore.isNumber(this._start)&&e>this._start;this._start=e,this._end=t;for(var s=!1,i=e;i<t&&i<this._data.length&&!s;i++)s=null==this._data[i];if(s){for(var a=Math.max(0,r?e:t-this.pageSize),i=a;i<this._data.length&&null!=this._data[i];i++)a++;this._skip=a,this._getData(null,e=>{this._pendingRequest=e})}}};