package com.sistemasolton.itranvias.home.presentation.routecalculation;

import androidx.compose.runtime.MutableState;
import androidx.compose.runtime.SnapshotStateKt__SnapshotStateKt;
import androidx.core.view.MotionEventCompat;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelKt;
import com.google.android.gms.actions.SearchIntents;
import com.sistemasolton.itranvias.home.data.remote.AutocompleteResponse;
import com.sistemasolton.itranvias.home.domain.repository.GooglePlacesRepository;
import kotlin.Metadata;
import kotlin.ResultKt;
import kotlin.Unit;
import kotlin.collections.CollectionsKt;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.IntrinsicsKt;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.SuspendLambda;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.internal.Intrinsics;
import kotlin.text.StringsKt;
import kotlinx.coroutines.BuildersKt__Builders_commonKt;
import kotlinx.coroutines.CoroutineScope;

/* JADX INFO: compiled from: RouteCalculationViewModel.kt */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000(\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\b\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0007\b\u0007\u0018\u00002\u00020\u0001B\u000f\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0004\b\u0004\u0010\u0005J\u000e\u0010\u000f\u001a\u00020\u00102\u0006\u0010\u0011\u001a\u00020\u0012J\u000e\u0010\u0013\u001a\u00020\u00102\u0006\u0010\u0011\u001a\u00020\u0012J\u000e\u0010\u0014\u001a\u00020\u00102\u0006\u0010\u0015\u001a\u00020\u0012J\u000e\u0010\u0016\u001a\u00020\u00102\u0006\u0010\u0015\u001a\u00020\u0012J\u0010\u0010\u0017\u001a\u00020\u00102\u0006\u0010\u0011\u001a\u00020\u0012H\u0002J\u0010\u0010\u0018\u001a\u00020\u00102\u0006\u0010\u0011\u001a\u00020\u0012H\u0002R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000R+\u0010\b\u001a\u00020\u00072\u0006\u0010\u0006\u001a\u00020\u00078F@BX\u0086\u008e\u0002¢\u0006\u0012\n\u0004\b\r\u0010\u000e\u001a\u0004\b\t\u0010\n\"\u0004\b\u000b\u0010\f¨\u0006\u0019"}, d2 = {"Lcom/sistemasolton/itranvias/home/presentation/routecalculation/RouteCalculationViewModel;", "Landroidx/lifecycle/ViewModel;", "googlePlacesRepository", "Lcom/sistemasolton/itranvias/home/domain/repository/GooglePlacesRepository;", "<init>", "(Lcom/sistemasolton/itranvias/home/domain/repository/GooglePlacesRepository;)V", "<set-?>", "Lcom/sistemasolton/itranvias/home/presentation/routecalculation/RouteCalculationState;", "state", "getState", "()Lcom/sistemasolton/itranvias/home/presentation/routecalculation/RouteCalculationState;", "setState", "(Lcom/sistemasolton/itranvias/home/presentation/routecalculation/RouteCalculationState;)V", "state$delegate", "Landroidx/compose/runtime/MutableState;", "onOriginQueryChanged", "", SearchIntents.EXTRA_QUERY, "", "onDestinationQueryChanged", "onOriginSelected", "description", "onDestinationSelected", "searchOrigin", "searchDestination", "composeApp_release"}, k = 1, mv = {2, 1, 0}, xi = 48)
public final class RouteCalculationViewModel extends ViewModel {
    public static final int $stable = 8;
    private final GooglePlacesRepository googlePlacesRepository;

    /* JADX INFO: renamed from: state$delegate, reason: from kotlin metadata */
    private final MutableState state;

    public RouteCalculationViewModel(GooglePlacesRepository googlePlacesRepository) {
        Intrinsics.checkNotNullParameter(googlePlacesRepository, "googlePlacesRepository");
        this.googlePlacesRepository = googlePlacesRepository;
        this.state = SnapshotStateKt__SnapshotStateKt.mutableStateOf$default(new RouteCalculationState(null, null, null, null, null, null, false, null, 255, null), null, 2, null);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void setState(RouteCalculationState routeCalculationState) {
        this.state.setValue(routeCalculationState);
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final RouteCalculationState getState() {
        return (RouteCalculationState) this.state.getValue();
    }

    public final void onOriginQueryChanged(String query) {
        Intrinsics.checkNotNullParameter(query, "query");
        RouteCalculationState state = getState();
        setState(state.copy((183 & 1) != 0 ? state.originQuery : query, (183 & 2) != 0 ? state.destinationQuery : null, (183 & 4) != 0 ? state.originPredictions : null, (183 & 8) != 0 ? state.destinationPredictions : null, (183 & 16) != 0 ? state.selectedOrigin : null, (183 & 32) != 0 ? state.selectedDestination : null, (183 & 64) != 0 ? state.isLoading : false, (183 & 128) != 0 ? state.error : null));
        if (!StringsKt.isBlank(query)) {
            searchOrigin(query);
        } else {
            RouteCalculationState state2 = getState();
            setState(state2.copy((183 & 1) != 0 ? state2.originQuery : null, (183 & 2) != 0 ? state2.destinationQuery : null, (183 & 4) != 0 ? state2.originPredictions : CollectionsKt.emptyList(), (183 & 8) != 0 ? state2.destinationPredictions : null, (183 & 16) != 0 ? state2.selectedOrigin : null, (183 & 32) != 0 ? state2.selectedDestination : null, (183 & 64) != 0 ? state2.isLoading : false, (183 & 128) != 0 ? state2.error : null));
        }
    }

    public final void onDestinationQueryChanged(String query) {
        Intrinsics.checkNotNullParameter(query, "query");
        RouteCalculationState state = getState();
        setState(state.copy((183 & 1) != 0 ? state.originQuery : null, (183 & 2) != 0 ? state.destinationQuery : query, (183 & 4) != 0 ? state.originPredictions : null, (183 & 8) != 0 ? state.destinationPredictions : null, (183 & 16) != 0 ? state.selectedOrigin : null, (183 & 32) != 0 ? state.selectedDestination : null, (183 & 64) != 0 ? state.isLoading : false, (183 & 128) != 0 ? state.error : null));
        if (!StringsKt.isBlank(query)) {
            searchDestination(query);
        } else {
            RouteCalculationState state2 = getState();
            setState(state2.copy((183 & 1) != 0 ? state2.originQuery : null, (183 & 2) != 0 ? state2.destinationQuery : null, (183 & 4) != 0 ? state2.originPredictions : null, (183 & 8) != 0 ? state2.destinationPredictions : CollectionsKt.emptyList(), (183 & 16) != 0 ? state2.selectedOrigin : null, (183 & 32) != 0 ? state2.selectedDestination : null, (183 & 64) != 0 ? state2.isLoading : false, (183 & 128) != 0 ? state2.error : null));
        }
    }

    public final void onOriginSelected(String description) {
        Intrinsics.checkNotNullParameter(description, "description");
        RouteCalculationState state = getState();
        setState(state.copy((183 & 1) != 0 ? state.originQuery : null, (183 & 2) != 0 ? state.destinationQuery : null, (183 & 4) != 0 ? state.originPredictions : CollectionsKt.emptyList(), (183 & 8) != 0 ? state.destinationPredictions : null, (183 & 16) != 0 ? state.selectedOrigin : description, (183 & 32) != 0 ? state.selectedDestination : null, (183 & 64) != 0 ? state.isLoading : false, (183 & 128) != 0 ? state.error : null));
    }

    public final void onDestinationSelected(String description) {
        Intrinsics.checkNotNullParameter(description, "description");
        RouteCalculationState state = getState();
        setState(state.copy((183 & 1) != 0 ? state.originQuery : null, (183 & 2) != 0 ? state.destinationQuery : null, (183 & 4) != 0 ? state.originPredictions : null, (183 & 8) != 0 ? state.destinationPredictions : CollectionsKt.emptyList(), (183 & 16) != 0 ? state.selectedOrigin : null, (183 & 32) != 0 ? state.selectedDestination : description, (183 & 64) != 0 ? state.isLoading : false, (183 & 128) != 0 ? state.error : null));
    }

    /* JADX INFO: renamed from: com.sistemasolton.itranvias.home.presentation.routecalculation.RouteCalculationViewModel$searchOrigin$1, reason: invalid class name and case insensitive filesystem */
    /* JADX INFO: compiled from: RouteCalculationViewModel.kt */
    @Metadata(d1 = {"\u0000\n\n\u0000\n\u0002\u0010\u0002\n\u0002\u0018\u0002\u0010\u0000\u001a\u00020\u0001*\u00020\u0002H\n"}, d2 = {"<anonymous>", "", "Lkotlinx/coroutines/CoroutineScope;"}, k = 3, mv = {2, 1, 0}, xi = 48)
    @DebugMetadata(c = "com.sistemasolton.itranvias.home.presentation.routecalculation.RouteCalculationViewModel$searchOrigin$1", f = "RouteCalculationViewModel.kt", i = {}, l = {MotionEventCompat.AXIS_GENERIC_14}, m = "invokeSuspend", n = {}, s = {})
    static final class C06501 extends SuspendLambda implements Function2<CoroutineScope, Continuation<? super Unit>, Object> {
        final /* synthetic */ String $query;
        int label;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        C06501(String str, Continuation<? super C06501> continuation) {
            super(2, continuation);
            this.$query = str;
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        public final Continuation<Unit> create(Object obj, Continuation<?> continuation) {
            return RouteCalculationViewModel.this.new C06501(this.$query, continuation);
        }

        @Override // kotlin.jvm.functions.Function2
        public final Object invoke(CoroutineScope coroutineScope, Continuation<? super Unit> continuation) {
            return ((C06501) create(coroutineScope, continuation)).invokeSuspend(Unit.INSTANCE);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        public final Object invokeSuspend(Object obj) throws Throwable {
            Object coroutine_suspended = IntrinsicsKt.getCOROUTINE_SUSPENDED();
            int i = this.label;
            try {
                if (i == 0) {
                    ResultKt.throwOnFailure(obj);
                    RouteCalculationViewModel routeCalculationViewModel = RouteCalculationViewModel.this;
                    RouteCalculationState state = routeCalculationViewModel.getState();
                    routeCalculationViewModel.setState(state.copy((183 & 1) != 0 ? state.originQuery : null, (183 & 2) != 0 ? state.destinationQuery : null, (183 & 4) != 0 ? state.originPredictions : null, (183 & 8) != 0 ? state.destinationPredictions : null, (183 & 16) != 0 ? state.selectedOrigin : null, (183 & 32) != 0 ? state.selectedDestination : null, (183 & 64) != 0 ? state.isLoading : true, (183 & 128) != 0 ? state.error : null));
                    this.label = 1;
                    obj = RouteCalculationViewModel.this.googlePlacesRepository.searchPlaces(this.$query, this);
                    if (obj == coroutine_suspended) {
                        return coroutine_suspended;
                    }
                } else {
                    if (i != 1) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    ResultKt.throwOnFailure(obj);
                }
                RouteCalculationViewModel routeCalculationViewModel2 = RouteCalculationViewModel.this;
                RouteCalculationState state2 = routeCalculationViewModel2.getState();
                routeCalculationViewModel2.setState(state2.copy((183 & 1) != 0 ? state2.originQuery : null, (183 & 2) != 0 ? state2.destinationQuery : null, (183 & 4) != 0 ? state2.originPredictions : ((AutocompleteResponse) obj).getPredictions(), (183 & 8) != 0 ? state2.destinationPredictions : null, (183 & 16) != 0 ? state2.selectedOrigin : null, (183 & 32) != 0 ? state2.selectedDestination : null, (183 & 64) != 0 ? state2.isLoading : false, (183 & 128) != 0 ? state2.error : null));
            } catch (Exception e) {
                RouteCalculationViewModel routeCalculationViewModel3 = RouteCalculationViewModel.this;
                RouteCalculationState state3 = routeCalculationViewModel3.getState();
                String message = e.getMessage();
                if (message == null) {
                    message = "Error desconocido";
                }
                routeCalculationViewModel3.setState(state3.copy((183 & 1) != 0 ? state3.originQuery : null, (183 & 2) != 0 ? state3.destinationQuery : null, (183 & 4) != 0 ? state3.originPredictions : null, (183 & 8) != 0 ? state3.destinationPredictions : null, (183 & 16) != 0 ? state3.selectedOrigin : null, (183 & 32) != 0 ? state3.selectedDestination : null, (183 & 64) != 0 ? state3.isLoading : false, (183 & 128) != 0 ? state3.error : message));
            }
            return Unit.INSTANCE;
        }
    }

    private final void searchOrigin(String query) {
        BuildersKt__Builders_commonKt.launch$default(ViewModelKt.getViewModelScope(this), null, null, new C06501(query, null), 3, null);
    }

    /* JADX INFO: renamed from: com.sistemasolton.itranvias.home.presentation.routecalculation.RouteCalculationViewModel$searchDestination$1, reason: invalid class name */
    /* JADX INFO: compiled from: RouteCalculationViewModel.kt */
    @Metadata(d1 = {"\u0000\n\n\u0000\n\u0002\u0010\u0002\n\u0002\u0018\u0002\u0010\u0000\u001a\u00020\u0001*\u00020\u0002H\n"}, d2 = {"<anonymous>", "", "Lkotlinx/coroutines/CoroutineScope;"}, k = 3, mv = {2, 1, 0}, xi = 48)
    @DebugMetadata(c = "com.sistemasolton.itranvias.home.presentation.routecalculation.RouteCalculationViewModel$searchDestination$1", f = "RouteCalculationViewModel.kt", i = {}, l = {57}, m = "invokeSuspend", n = {}, s = {})
    static final class AnonymousClass1 extends SuspendLambda implements Function2<CoroutineScope, Continuation<? super Unit>, Object> {
        final /* synthetic */ String $query;
        int label;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        AnonymousClass1(String str, Continuation<? super AnonymousClass1> continuation) {
            super(2, continuation);
            this.$query = str;
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        public final Continuation<Unit> create(Object obj, Continuation<?> continuation) {
            return RouteCalculationViewModel.this.new AnonymousClass1(this.$query, continuation);
        }

        @Override // kotlin.jvm.functions.Function2
        public final Object invoke(CoroutineScope coroutineScope, Continuation<? super Unit> continuation) {
            return ((AnonymousClass1) create(coroutineScope, continuation)).invokeSuspend(Unit.INSTANCE);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        public final Object invokeSuspend(Object obj) throws Throwable {
            Object coroutine_suspended = IntrinsicsKt.getCOROUTINE_SUSPENDED();
            int i = this.label;
            try {
                if (i == 0) {
                    ResultKt.throwOnFailure(obj);
                    RouteCalculationViewModel routeCalculationViewModel = RouteCalculationViewModel.this;
                    RouteCalculationState state = routeCalculationViewModel.getState();
                    routeCalculationViewModel.setState(state.copy((183 & 1) != 0 ? state.originQuery : null, (183 & 2) != 0 ? state.destinationQuery : null, (183 & 4) != 0 ? state.originPredictions : null, (183 & 8) != 0 ? state.destinationPredictions : null, (183 & 16) != 0 ? state.selectedOrigin : null, (183 & 32) != 0 ? state.selectedDestination : null, (183 & 64) != 0 ? state.isLoading : true, (183 & 128) != 0 ? state.error : null));
                    this.label = 1;
                    obj = RouteCalculationViewModel.this.googlePlacesRepository.searchPlaces(this.$query, this);
                    if (obj == coroutine_suspended) {
                        return coroutine_suspended;
                    }
                } else {
                    if (i != 1) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    ResultKt.throwOnFailure(obj);
                }
                RouteCalculationViewModel routeCalculationViewModel2 = RouteCalculationViewModel.this;
                RouteCalculationState state2 = routeCalculationViewModel2.getState();
                routeCalculationViewModel2.setState(state2.copy((183 & 1) != 0 ? state2.originQuery : null, (183 & 2) != 0 ? state2.destinationQuery : null, (183 & 4) != 0 ? state2.originPredictions : null, (183 & 8) != 0 ? state2.destinationPredictions : ((AutocompleteResponse) obj).getPredictions(), (183 & 16) != 0 ? state2.selectedOrigin : null, (183 & 32) != 0 ? state2.selectedDestination : null, (183 & 64) != 0 ? state2.isLoading : false, (183 & 128) != 0 ? state2.error : null));
            } catch (Exception e) {
                RouteCalculationViewModel routeCalculationViewModel3 = RouteCalculationViewModel.this;
                RouteCalculationState state3 = routeCalculationViewModel3.getState();
                String message = e.getMessage();
                if (message == null) {
                    message = "Error desconocido";
                }
                routeCalculationViewModel3.setState(state3.copy((183 & 1) != 0 ? state3.originQuery : null, (183 & 2) != 0 ? state3.destinationQuery : null, (183 & 4) != 0 ? state3.originPredictions : null, (183 & 8) != 0 ? state3.destinationPredictions : null, (183 & 16) != 0 ? state3.selectedOrigin : null, (183 & 32) != 0 ? state3.selectedDestination : null, (183 & 64) != 0 ? state3.isLoading : false, (183 & 128) != 0 ? state3.error : message));
            }
            return Unit.INSTANCE;
        }
    }

    private final void searchDestination(String query) {
        BuildersKt__Builders_commonKt.launch$default(ViewModelKt.getViewModelScope(this), null, null, new AnonymousClass1(query, null), 3, null);
    }
}