/**
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates.
 * The Universal Permissive License (UPL), Version 1.0
 */
"use strict";
define(["ojs/ojcore","jquery","knockout","ojs/ojdatasource-common","ojs/ojmodel","ojs/ojknockout-model"],function(a,g,b){a.bc=function(a){this.gb=a;this.current=0;this.Init();this.pi=[];this.sW(10)};o_("CollectionPagingDataSource",a.bc,a);a.b.sa(a.bc,a.Gm,"oj.CollectionPagingDataSource");a.bc.prototype.Init=function(){a.bc.u.Init.call(this)};a.b.g("CollectionPagingDataSource.prototype.Init",{Init:a.bc.prototype.Init});a.bc.prototype.ET=function(){return this.nn()?this.Vq:this.totalSize()-this.current};
a.bc.prototype.ew=function(){this.pi=Array(this.ET());var a=this;return this.gb.Ex(this.current,this.current+this.pi.length).then(function(b){for(var e=0;e<b.length;e++)a.pi[e]=b[e];a.nE();a.Rk=a.pa+a.pi.length-1})};a.bc.prototype.nE=function(){if(void 0!==this.Tn){this.Tn.removeAll();for(var b=0;b<this.pi.length;b++)this.Tn.push(a.lb.map(this.pi[b]))}};a.bc.prototype.kr=function(){return this.pi};a.b.g("CollectionPagingDataSource.prototype.getWindow",{kr:a.bc.prototype.kr});a.bc.prototype.vF=function(){void 0===
this.Tn&&(this.Tn=b.observableArray(),this.nE());return this.Tn};a.b.g("CollectionPagingDataSource.prototype.getWindowObservable",{vF:a.bc.prototype.vF});a.bc.prototype.getPage=function(){return this.ie};a.b.g("CollectionPagingDataSource.prototype.getPage",{getPage:a.bc.prototype.getPage});a.bc.prototype.setPage=function(b,d){d=d||{};b=parseInt(b,10);try{a.bc.u.handleEvent.call(this,a.bd.O.BEFOREPAGE,{page:b,previousPage:this.ie})}catch(e){return Promise.reject(null)}this.pageSize=null!=d.pageSize?
d.pageSize:this.pageSize;d.startIndex=b*this.pageSize;var f=this.ie;this.ie=b;this.pa=d.startIndex;var g=this;return new Promise(function(b,c){g.Hg(d).then(function(){a.bc.u.handleEvent.call(g,a.bd.O.PAGE,{page:g.ie,previousPage:f});b(null)},function(){g.ie=f;g.pa=g.ie*g.pageSize;c(null)})})};a.b.g("CollectionPagingDataSource.prototype.setPage",{setPage:a.bc.prototype.setPage});a.bc.prototype.getStartItemIndex=function(){return this.pa};a.b.g("CollectionPagingDataSource.prototype.getStartItemIndex",
{getStartItemIndex:a.bc.prototype.getStartItemIndex});a.bc.prototype.getEndItemIndex=function(){return this.Rk};a.b.g("CollectionPagingDataSource.prototype.getEndItemIndex",{getEndItemIndex:a.bc.prototype.getEndItemIndex});a.bc.prototype.getPageCount=function(){var a=this.totalSize();return-1==a?-1:Math.ceil(a/this.pageSize)};a.b.g("CollectionPagingDataSource.prototype.getPageCount",{getPageCount:a.bc.prototype.getPageCount});a.bc.prototype.fetch=function(a){var b=a||{};if(void 0!==b.pageSize&&void 0!==
b.startIndex){if(!this.nn())return this.NK(null),Promise.resolve();this.Vq=b.startIndex+b.pageSize;var e=this;return this.ew().then(function(){e.NK(null)})}return this.Hg(a)};a.b.g("CollectionPagingDataSource.prototype.fetch",{fetch:a.bc.prototype.fetch});a.bc.prototype.Hg=function(a){var b=a||{};void 0!==b.startIndex&&(this.current=b.startIndex);void 0!==b.pageSize&&(this.Vq=this.pageSize=b.pageSize);var e=this;return new Promise(function(a){try{e.gb.fetch({success:function(){e.ew().then(function(){e.NK(b);
a({data:e.kr(),startIndex:e.current})})}})}catch(c){e.ew().then(function(){e.NK(b);a({data:e.kr(),startIndex:e.current})})}})};a.bc.prototype.NK=function(a){a=a||{};a.silent||this.handleEvent("sync",{data:this.kr(),startIndex:this.current});a.success&&a.success()};a.bc.prototype.handleEvent=function(b,d){return a.bc.u.handleEvent.call(this,b,d)};a.bc.prototype.nn=function(){return this.current+this.Vq<this.totalSize()};a.bc.prototype.sW=function(a){this.Vq=this.pageSize=a};a.bc.prototype.size=function(){var a=
this.kr();return a?a.length:0};a.b.g("CollectionPagingDataSource.prototype.size",{size:a.bc.prototype.size});a.bc.prototype.totalSize=function(){return this.gb.length};a.b.g("CollectionPagingDataSource.prototype.totalSize",{totalSize:a.bc.prototype.totalSize});a.bc.prototype.totalSizeConfidence=function(){return"actual"};a.b.g("CollectionPagingDataSource.prototype.totalSizeConfidence",{totalSizeConfidence:a.bc.prototype.totalSizeConfidence});a.bc.prototype.getCapability=function(){return null};a.b.g("CollectionPagingDataSource.prototype.getCapability",
{getCapability:a.bc.prototype.getCapability})});