import { createSelector } from '@ngrx/store';
import { createEffect, ofType } from '@ngrx/effects';
import { concatLatestFrom } from '@ngrx/operators';
import { tap, filter, withLatestFrom, map } from 'rxjs';
import equal from 'fast-deep-equal';
import { RoutesRecognized } from '@angular/router';

function capitalize(text) {
    return (text.charAt(0).toUpperCase() + text.substring(1));
}
/**
 * @deprecated Please import from \@onecx/ngrx-accelerator
 */
function createChildSelectors(featureSelector, initialChildState) {
    return Object.keys(initialChildState).reduce((nestedSelectors, nestedKey) => ({
        ...nestedSelectors,
        [`select${capitalize(nestedKey)}`]: createSelector(featureSelector, (parentState) => parentState?.[nestedKey]),
    }), {});
}

/**
 * @deprecated Please import from \@onecx/ngrx-accelerator
 */
function createQueryParamsEffect(actions$, actionType, router, activatedRoute, reducer) {
    return createEffect(() => {
        return actions$.pipe(ofType(actionType), concatLatestFrom(() => activatedRoute.queryParams), tap(([action, queryParams]) => {
            const params = reducer(queryParams, action);
            router.navigate([], {
                relativeTo: activatedRoute,
                queryParams: params,
                replaceUrl: true,
                onSameUrlNavigation: 'reload',
            });
        }));
    }, { dispatch: false });
}

/**
 * @deprecated Please import from \@onecx/ngrx-accelerator
 */
function filterForNavigatedTo(router, component) {
    return (source) => {
        return source.pipe(filter(() => {
            return checkForComponent(component, router.routerState.root);
        }));
    };
}
function checkForComponent(component, route) {
    if (route.component === component) {
        return true;
    }
    for (const c of route.children) {
        const r = checkForComponent(component, c);
        if (r) {
            return true;
        }
    }
    return false;
}

/**
 * @deprecated use filterOutQueryParamsHaveNotChanged
 */
function filterForQueryParamsChanged(router, queryParamsTypeDef, allowEmptyQueryParamsList = false) {
    return filterOutQueryParamsHaveNotChanged(router, queryParamsTypeDef, allowEmptyQueryParamsList);
}
/**
 * @deprecated Please import from \@onecx/ngrx-accelerator
 */
function filterOutQueryParamsHaveNotChanged(router, queryParamsTypeDef, allowEmptyQueryParamsList = false) {
    return (source) => {
        return source.pipe(withLatestFrom(router.events.pipe(filter((e) => e instanceof RoutesRecognized), map(() => router.routerState))), filter(([action, previousRouterState]) => {
            if (!allowEmptyQueryParamsList &&
                Object.keys(action?.payload?.routerState?.root?.queryParams || {}).length === 0) {
                return false;
            }
            const currentQueryParams = previousRouterState.snapshot.root.queryParams;
            const actionResult = queryParamsTypeDef.safeParse(action?.payload?.routerState?.root?.queryParams);
            const currentResult = queryParamsTypeDef.safeParse(currentQueryParams);
            if (actionResult.success && currentResult.success) {
                const actionParams = actionResult.data;
                const currentParams = currentResult.data;
                if (allowEmptyQueryParamsList &&
                    Object.keys(actionParams).length === 0 &&
                    Object.keys(currentParams).length === 0) {
                    return true;
                }
                return !equal(actionParams, currentParams);
            }
            return false;
        }), map(([action]) => action));
    };
}

/**
 * @deprecated use filterOutOnlyQueryParamsChanged
 */
function filterForOnlyQueryParamsChanged(router) {
    return filterOutOnlyQueryParamsChanged(router);
}
/**
 * @deprecated Please import from \@onecx/ngrx-accelerator
 */
function filterOutOnlyQueryParamsChanged(router) {
    return (source) => {
        return source.pipe(withLatestFrom(router.events.pipe(filter((e) => e instanceof RoutesRecognized), map(() => router.routerState))), filter(([action, previousRouterState]) => {
            const previousPath = previousRouterState.snapshot.url.split('?')[0];
            const currentPath = action.payload.event.urlAfterRedirects.split('?')[0];
            return previousPath !== currentPath;
        }), map(([action]) => action));
    };
}

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

export { createChildSelectors, createQueryParamsEffect, filterForNavigatedTo, filterForOnlyQueryParamsChanged, filterForQueryParamsChanged, filterOutOnlyQueryParamsChanged, filterOutQueryParamsHaveNotChanged };
//# sourceMappingURL=onecx-portal-integration-angular-ngrx.mjs.map
