import { ToMethod } from '@rxap/pattern';
import { isPromise, noop } from '@rxap/utilities';

function ProvideMethodMock(method, implementation = noop, delay) {
    return {
        provide: method,
        useValue: ToMethod((parameters, ...args) => {
            if (delay) {
                return new Promise(resolve => setTimeout(() => resolve(implementation(parameters, ...args)), Math.min(2147483647, delay)));
            }
            const result = implementation(parameters, ...args);
            if (isPromise(result)) {
                return result;
            }
            return Promise.resolve(result);
        }),
    };
}
function ProvideMethodWithParametersMock(method, implementation, delay) {
    return {
        provide: method,
        useValue: ToMethod((parameters, ...args) => {
            if (!parameters) {
                throw new Error(`Method requires a parameter`);
            }
            if (delay) {
                return new Promise(resolve => setTimeout(() => resolve(implementation(parameters, ...args)), Math.min(2147483647, delay)));
            }
            const result = implementation(parameters, ...args);
            if (isPromise(result)) {
                return result;
            }
            return Promise.resolve(result);
        }),
    };
}
function ProvideRemoteMethodMock(method, implementation, delay) {
    return ProvideMethodWithParametersMock(method, implementation, delay);
}

function ProvideRxapNestjsGetPageMethod(method, data) {
    return ProvideMethodWithParametersMock(method, (input) => {
        const parameters = input?.parameters;
        if (!parameters) {
            console.warn('No parameters provided');
            return {
                rows: [],
                pageSize: 0,
                pageIndex: 0,
                total: 0,
            };
        }
        const { pageIndex, pageSize, sortBy, sortDirection, filter, } = parameters;
        let filteredData = data.slice();
        if (filter?.length) {
            filteredData =
                filteredData.filter((row) => filter.map(f => f.split('|'))
                    .some(([key, value]) => row[key] === undefined || (row[key] + '').includes(value)));
        }
        if (sortBy && sortDirection) {
            filteredData.sort((a, b) => {
                const result = (a[sortBy] + '').localeCompare(b[sortBy] + '');
                return sortDirection === 'asc' ? result : result * -1;
            });
        }
        return {
            rows: filteredData.slice(pageIndex * pageSize, pageIndex * pageSize + pageSize),
            pageSize,
            pageIndex,
            total: filteredData.length,
            sortDirection,
            sortBy,
            filter,
        };
    });
}

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

export { ProvideMethodMock, ProvideMethodWithParametersMock, ProvideRemoteMethodMock, ProvideRxapNestjsGetPageMethod };
//# sourceMappingURL=rxap-open-api-testing.mjs.map
