import { RxapHttpDataSourceError } from '@rxap/data-source/http';
import { AbstractPaginationDataSource, RXAP_PAGINATION_DATA_SOURCE, RXAP_PAGINATION_DATA_SOURCE_PAGINATOR, RxapAbstractPaginationDataSource } from '@rxap/data-source/pagination';
import * as i0 from '@angular/core';
import { InjectionToken, Injectable, Inject, Optional, Pipe } from '@angular/core';
import { RXAP_DATA_SOURCE_METADATA } from '@rxap/data-source';
import { EMPTY } from 'rxjs';
import { startWith, tap, filter, map } from 'rxjs/operators';

class RxapHttpPaginationDataSourceError extends RxapHttpDataSourceError {
    constructor(message, code, className) {
        super(message, code, className);
        this.addSubPackageName('pagination');
    }
}

const RXAP_HTTP_PAGINATION_DATA_SOURCE_TO_OPTIONS_FUNCTION = new InjectionToken('rxap/data-source/http/pagination');

class HttpPaginationDataSource extends AbstractPaginationDataSource {
    constructor(dataSource, paginator = null, pageEventToHttpOptions = null, metadata = dataSource.metadata) {
        super(paginator, metadata);
        this.dataSource = dataSource;
    }
    pageEventToHttpOptions(event) {
        throw new RxapHttpPaginationDataSourceError('The PageEventToHttpOptionsFunction is not defined', '');
    }
    _connect(viewer) {
        this.assertPaginator();
        if (viewer.viewChange && viewer.viewChange !== EMPTY) {
            console.warn('The viewChange property is readonly');
        }
        if (this.paginator === undefined || this.paginator.page === undefined) {
            throw new RxapHttpPaginationDataSourceError('The paginator instance has not a page property', '');
        }
        viewer.viewChange = this.paginator.page.pipe(startWith({
            pageIndex: this.paginator.pageIndex,
            pageSize: this.paginator.pageSize,
            length: this.paginator.length,
        }), tap((page) => {
            if (typeof page.pageSize !== 'number') {
                console.warn('The page size is not defined!');
            }
        }), filter((page) => typeof page.pageSize === 'number'), map((page) => this.pageEventToHttpOptions(page)));
        return [
            this.dataSource.connect(viewer).pipe(tap((paginationData) => {
                if (typeof paginationData !== 'object') {
                    throw new RxapHttpPaginationDataSourceError('The http data source does not return a PaginationData object. Value is not an object', '');
                }
                if (paginationData['total'] === undefined || paginationData['total'] === null) {
                    throw new RxapHttpPaginationDataSourceError('The http data source does not return a PaginationData object. Object has not the property total', '');
                }
                if (!paginationData['data'] &&
                    !Array.isArray(paginationData['data'])) {
                    throw new RxapHttpPaginationDataSourceError('The http data source does not return a PaginationData object. Object has not the property data' +
                        ' property of the value not an array', '');
                }
                if (paginationData['size'] !== undefined &&
                    paginationData['size'] !== this.paginator.pageSize) {
                    console.warn('The selected page size from the paginator is not equal to the page size from the PaginationData object');
                }
                if (paginationData['page'] !== undefined &&
                    paginationData['page'] !== this.paginator.pageIndex) {
                    console.warn('The selected page index from the paginator is not equal to the page index from the PaginationData object');
                }
            }), tap((paginationData) => this.updateTotalLength(paginationData['total'])), map((paginationData) => paginationData['data'])),
            () => this.dataSource.disconnect(viewer),
        ];
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: HttpPaginationDataSource, deps: [{ token: RXAP_PAGINATION_DATA_SOURCE }, { token: RXAP_PAGINATION_DATA_SOURCE_PAGINATOR, optional: true }, { token: RXAP_HTTP_PAGINATION_DATA_SOURCE_TO_OPTIONS_FUNCTION, optional: true }, { token: RXAP_DATA_SOURCE_METADATA, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: HttpPaginationDataSource }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: HttpPaginationDataSource, decorators: [{
            type: Injectable
        }], ctorParameters: () => [{ type: undefined, decorators: [{
                    type: Inject,
                    args: [RXAP_PAGINATION_DATA_SOURCE]
                }] }, { type: undefined, decorators: [{
                    type: Optional
                }, {
                    type: Inject,
                    args: [RXAP_PAGINATION_DATA_SOURCE_PAGINATOR]
                }] }, { type: undefined, decorators: [{
                    type: Optional
                }, {
                    type: Inject,
                    args: [RXAP_HTTP_PAGINATION_DATA_SOURCE_TO_OPTIONS_FUNCTION]
                }] }, { type: undefined, decorators: [{
                    type: Optional
                }, {
                    type: Inject,
                    args: [RXAP_DATA_SOURCE_METADATA]
                }] }] });
function RxapHttpPaginationDataSource(metadata, className = 'HttpPaginationDataSource', packageName = '@rxap/data-source/http/pagination') {
    return function (target) {
        RxapAbstractPaginationDataSource(metadata, className, packageName)(target);
    };
}

class ToHttpPaginationPipe {
    transform(dataSource, paginator, pageEventToHttpOptions) {
        return new HttpPaginationDataSource(dataSource, paginator, pageEventToHttpOptions);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: ToHttpPaginationPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); }
    static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.1", ngImport: i0, type: ToHttpPaginationPipe, isStandalone: true, name: "toHttpPagination" }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: ToHttpPaginationPipe, decorators: [{
            type: Pipe,
            args: [{
                    name: 'toHttpPagination',
                    standalone: true,
                }]
        }] });

// region 
// endregion

/**
 * Generated bundle index. Do not edit.
 */

export { HttpPaginationDataSource, RXAP_HTTP_PAGINATION_DATA_SOURCE_TO_OPTIONS_FUNCTION, RxapHttpPaginationDataSource, RxapHttpPaginationDataSourceError, ToHttpPaginationPipe };
//# sourceMappingURL=rxap-data-source-http-pagination.mjs.map
