<commit_before>import { Action, Select, Selector, State, StateContext } from '@ngxs/store';
import { RichiesteMarkerService } from '../../../../../core/service/maps-service';
import { RichiestaMarker } from '../../../maps/maps-model/richiesta-marker.model';
import {
    AddRichiesteMarkers,
    ClearRichiesteMarkers,
    GetRichiesteMarkers,
    InsertRichiestaMarker,
    OpacizzaRichiesteMarkers, PatchRichiesteMarkers,
    RemoveRichiestaMarker,
    SetRichiestaMarkerById,
    SetRichiesteMarkers,
    UpdateRichiestaMarker
} from '../../actions/maps/richieste-markers.actions';
import { wipeStatoRichiesta } from '../../../../../shared/helper/function';
import { SetMarkerOpachiRichieste } from '../../actions/maps/marker-opachi.actions';
import { append, insertItem, patch, removeItem, updateItem } from '@ngxs/store/operators';
import { Observable } from 'rxjs';
import { HomeState } from '../home.state';
import { ToggleAnimation } from '../../actions/maps/maps-buttons.actions';
import { ShowToastr } from '../../../../../shared/store/actions/toastr/toastr.actions';
import { RichiesteMarkerAdapterService } from '../../../../../core/service/maps-service/richieste-marker/adapters/richieste-marker-adapter.service';
import { ToastrType } from '../../../../../shared/enum/toastr';

export interface RichiesteMarkersStateModel {
    richiesteMarkers: RichiestaMarker[];
    richiestaMarkerById: RichiestaMarker;
}

export const RichiesteMarkersStateDefaults: RichiesteMarkersStateModel = {
    richiesteMarkers: null,
    richiestaMarkerById: null
};

@State<RichiesteMarkersStateModel>({
    name: 'richiesteMarkers',
    defaults: RichiesteMarkersStateDefaults
})

export class RichiesteMarkersState {

    @Select(HomeState.mapIsLoaded) mapIsLoaded$: Observable<boolean>;

    @Selector()
    static richiesteMarkers(state: RichiesteMarkersStateModel) {
        return state.richiesteMarkers;
    }

    @Selector()
    static richiesteLenghtMarkers(state: RichiesteMarkersStateModel) {
        return state.richiesteMarkers.length;
    }

    @Selector()
    static getRichiestaById(state: RichiesteMarkersStateModel) {
        return state.richiestaMarkerById;
    }

    constructor(private _richieste: RichiesteMarkerService) {
    }

    @Action(GetRichiesteMarkers)
    getRichiesteMarkers({ dispatch }: StateContext<RichiesteMarkersStateModel>, action: GetRichiesteMarkers) {
        this._richieste.getRichiesteMarkers(action.areaMappa).subscribe((data: RichiestaMarker[]) => {
            dispatch(new SetRichiesteMarkers(data));
        }, () => dispatch(new ShowToastr(ToastrType.Error, 'Errore', 'Il server web non risponde', 5)));
    }

    @Action(SetRichiesteMarkers)
    setRichiesteMarkers({ dispatch }: StateContext<RichiesteMarkersStateModel>, action: SetRichiesteMarkers) {
        if (action.richiesteMarkers) {
            dispatch(new PatchRichiesteMarkers(action.richiesteMarkers));
            this.mapIsLoaded$.subscribe(isLoaded => {
                if (isLoaded) {
                    dispatch(new ToggleAnimation());
                }
            });
        }
    }

    @Action(PatchRichiesteMarkers)
    patchRichiesteMarkers({ patchState }: StateContext<RichiesteMarkersStateModel>, { payload }: PatchRichiesteMarkers) {
        patchState({
            richiesteMarkers: payload.map(item => RichiesteMarkerAdapterService.adapt(item))
        });
    }

    @Action(AddRichiesteMarkers)
    addRichiesteMarkers({ setState }: StateContext<RichiesteMarkersStateModel>, { payload }: AddRichiesteMarkers) {
        setState(
            patch({
                richiesteMarkers: append(payload.map(item => RichiesteMarkerAdapterService.adapt(item)))
            })
        );
    }

    @Action(InsertRichiestaMarker)
    insertRichiestaMarker({ setState }: StateContext<RichiesteMarkersStateModel>, { payload, before }: InsertRichiestaMarker) {
        setState(
            patch({
                richiesteMarkers: insertItem(RichiesteMarkerAdapterService.adapt(payload), before)
            })
        );
    }

    @Action(UpdateRichiestaMarker)
    updateRichiestaMarker({ setState }: StateContext<RichiesteMarkersStateModel>, { payload }: UpdateRichiestaMarker) {
        setState(
            patch({
                richiesteMarkers: updateItem<RichiestaMarker>(richiesta => richiesta.id === payload.id, RichiesteMarkerAdapterService.adapt(payload))
            })
        );
    }

    @Action(RemoveRichiestaMarker)
    removeRichiestaMarker({ setState }: StateContext<RichiesteMarkersStateModel>, { payload }: RemoveRichiestaMarker) {
        setState(
            patch({
                richiesteMarkers: removeItem<RichiestaMarker>(richiesta => richiesta.id === payload)
            })
        );
    }

    @Action(SetRichiestaMarkerById)
    setRichiestaMarkerById({ getState, patchState }: StateContext<RichiesteMarkersStateModel>, action: SetRichiestaMarkerById) {
        const state = getState();
        if (action.id) {
            patchState({
                richiestaMarkerById: state.richiesteMarkers.filter(richieste => richieste.id === action.id)[0]
            });
        } else {
            patchState({
                richiestaMarkerById: null
            });
        }
    }

    @Action(OpacizzaRichiesteMarkers)
    opacizzaRichiesteMarkers({ getState, dispatch }: StateContext<RichiesteMarkersStateModel>, action: OpacizzaRichiesteMarkers) {
        const state = getState();
        const filteredId: string[] = [];
        if (action.stato) {<commit_msg>Fix: ottimizzazione lazy loading richieste Marker<commit_after>import { Action, Select, Selector, State, StateContext } from '@ngxs/store';
import { RichiesteMarkerService } from '../../../../../core/service/maps-service';
import { RichiestaMarker } from '../../../maps/maps-model/richiesta-marker.model';
import {
    AddRichiesteMarkers,
    ClearRichiesteMarkers,
    GetRichiesteMarkers,
    InsertRichiestaMarker,
    OpacizzaRichiesteMarkers, PatchRichiesteMarkers,
    RemoveRichiestaMarker,
    SetRichiestaMarkerById,
    SetRichiesteMarkers,
    UpdateRichiestaMarker
} from '../../actions/maps/richieste-markers.actions';
import { wipeStatoRichiesta } from '../../../../../shared/helper/function';
import { SetMarkerOpachiRichieste } from '../../actions/maps/marker-opachi.actions';
import { append, insertItem, patch, removeItem, updateItem } from '@ngxs/store/operators';
import { Observable } from 'rxjs';
import { HomeState } from '../home.state';
import { ToggleAnimation } from '../../actions/maps/maps-buttons.actions';
import { ShowToastr } from '../../../../../shared/store/actions/toastr/toastr.actions';
import { RichiesteMarkerAdapterService } from '../../../../../core/service/maps-service/richieste-marker/adapters/richieste-marker-adapter.service';
import { ToastrType } from '../../../../../shared/enum/toastr';

export interface RichiesteMarkersStateModel {
    richiesteMarkers: RichiestaMarker[];
    richiesteMarkersId: string[];
    richiestaMarkerById: RichiestaMarker;
}

export const RichiesteMarkersStateDefaults: RichiesteMarkersStateModel = {
    richiesteMarkers: [],
    richiesteMarkersId: [],
    richiestaMarkerById: null
};

@State<RichiesteMarkersStateModel>({
    name: 'richiesteMarkers',
    defaults: RichiesteMarkersStateDefaults
})

export class RichiesteMarkersState {

    @Select(HomeState.mapIsLoaded) mapIsLoaded$: Observable<boolean>;

    @Selector()
    static richiesteMarkers(state: RichiesteMarkersStateModel) {
        return state.richiesteMarkers;
    }

    @Selector()
    static richiesteLenghtMarkers(state: RichiesteMarkersStateModel) {
        return state.richiesteMarkers.length;
    }

    @Selector()
    static getRichiestaById(state: RichiesteMarkersStateModel) {
        return state.richiestaMarkerById;
    }

    constructor(private _richieste: RichiesteMarkerService) {
    }

    @Action(GetRichiesteMarkers)
    getRichiesteMarkers({ dispatch }: StateContext<RichiesteMarkersStateModel>, action: GetRichiesteMarkers) {
        this._richieste.getRichiesteMarkers(action.areaMappa).subscribe((data: RichiestaMarker[]) => {
            dispatch(new SetRichiesteMarkers(data));
        }, () => dispatch(new ShowToastr(ToastrType.Error, 'Errore', 'Il server web non risponde', 5)));
    }

    @Action(SetRichiesteMarkers)
    setRichiesteMarkers({ getState, dispatch }: StateContext<RichiesteMarkersStateModel>, action: SetRichiesteMarkers) {
        const state = getState();
        if (action.richiesteMarkers) {
            if (state.richiesteMarkers.length === 0) {
                dispatch(new PatchRichiesteMarkers(action.richiesteMarkers));
            } else {
                const actionRichiesteId: string[] = [];
                const richiesteMarkerRemoveId: string[] = [];
                const richiesteMarkerAdd: RichiestaMarker[] = [];
                /**
                 * marker da aggiungere
                 */
                action.richiesteMarkers.forEach( richiesta => {
                    actionRichiesteId.push(richiesta.id);
                    if (!state.richiesteMarkersId.includes(richiesta.id)) {
                        richiesteMarkerAdd.push(richiesta);
                    }
                });
                /**
                 * marker da rimuovere
                 */
                state.richiesteMarkers.forEach( richiesta => {
                    if (!actionRichiesteId.includes(richiesta.id)) {
                        richiesteMarkerRemoveId.push(richiesta.id);
                    }
                });
                /**
                 * tolgo i marker dallo stato
                 */
                if (richiesteMarkerRemoveId.length > 0) {
                    richiesteMarkerRemoveId.forEach( id => {
                        dispatch(new RemoveRichiestaMarker(id));
                    });
                }
                /**
                 * aggiungo i marker allo stato
                 */
                if (richiesteMarkerAdd.length > 0) {
                    dispatch(new AddRichiesteMarkers(richiesteMarkerAdd));
                }
            }
            this.mapIsLoaded$.subscribe(isLoaded => {
                if (isLoaded) {
                    dispatch(new ToggleAnimation());
                }
            });
        }
    }

    @Action(PatchRichiesteMarkers)
    patchRichiesteMarkers({ patchState }: StateContext<RichiesteMarkersStateModel>, { payload }: PatchRichiesteMarkers) {
        patchState({
            richiesteMarkers: payload.map(item => RichiesteMarkerAdapterService.adapt(item)),
            richiesteMarkersId: payload.map(item => item.id)
        });
    }

    @Action(AddRichiesteMarkers)
    addRichiesteMarkers({ setState }: StateContext<RichiesteMarkersStateModel>, { payload }: AddRichiesteMarkers) {
        setState(
            patch({
                richiesteMarkers: append(payload.map(item => RichiesteMarkerAdapterService.adapt(item))),
                richiesteMarkersId: append(payload.map(item => item.id))
            })
        );
    }

    @Action(InsertRichiestaMarker)
    insertRichiestaMarker({ setState }: StateContext<RichiesteMarkersStateModel>, { payload, before }: InsertRichiestaMarker) {
        setState(
            patch({
                richiesteMarkers: insertItem(RichiesteMarkerAdapterService.adapt(payload), before),
                richiesteMarkersId: insertItem(payload.id, before)
            })
        );
    }

    @Action(UpdateRichiestaMarker)
    updateRichiestaMarker({ setState }: StateContext<RichiesteMarkersStateModel>, { payload }: UpdateRichiestaMarker) {
        setState(
            patch({
                richiesteMarkers: updateItem<RichiestaMarker>(richiesta => richiesta.id === payload.id, RichiesteMarkerAdapterService.adapt(payload))
            })
        );
    }

    @Action(RemoveRichiestaMarker)
    removeRichiestaMarker({ setState }: StateContext<RichiesteMarkersStateModel>, { payload }: RemoveRichiestaMarker) {
        setState(
            patch({
                richiesteMarkers: removeItem<RichiestaMarker>(richiesta => richiesta.id === payload),
                richiesteMarkersId: removeItem<string>(id => id === payload)
            })
        );
    }

    @Action(SetRichiestaMarkerById)
    setRichiestaMarkerById({ getState, patchState }: StateContext<RichiesteMarkersStateModel>, action: SetRichiestaMarkerById) {
        const state = getState();
        if (action.id) {
            patchState({
                richiestaMarkerById: state.richiesteMarkers.filter(richieste => richieste.id === action.id)[0]
            });
        } else {
            patchState({
                richiestaMarkerById: null
            });
        }
    }

    @Action(OpacizzaRichiesteMarkers)
    opacizzaRichiesteMarkers({ getState, dispatch }: StateContext<RichiesteMarkersStateModel>, action: OpacizzaRichiesteMarkers) {
        const state = getState();
        const filteredId: string[] = [];
        if (action.stato) {
{'fraction_non_alphanumeric': 0.11504492742492896, 'fraction_numerical': 0.0005375931188080792, 'mean_word_length': 2.7163242009132422, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4745493', 'n_tokens_mistral': 3772, 'n_tokens_neox': 3702, 'n_words': 564}