package com.google.firebase.firestore.model.mutation;

import com.google.firebase.Timestamp;
import com.google.firebase.firestore.model.Values;
import com.google.firebase.firestore.util.Assert;
import e4.G0;
import e4.H0;

/* JADX INFO: loaded from: classes3.dex */
public class NumericIncrementTransformOperation implements TransformOperation {
    private H0 operand;

    public NumericIncrementTransformOperation(H0 h0) {
        Assert.hardAssert(Values.isNumber(h0), "NumericIncrementTransformOperation expects a NumberValue operand", new Object[0]);
        this.operand = h0;
    }

    private double operandAsDouble() {
        if (Values.isDouble(this.operand)) {
            return this.operand.u();
        }
        if (Values.isInteger(this.operand)) {
            return this.operand.w();
        }
        throw Assert.fail("Expected 'operand' to be of Number type, but was " + this.operand.getClass().getCanonicalName(), new Object[0]);
    }

    private long operandAsLong() {
        if (Values.isDouble(this.operand)) {
            return (long) this.operand.u();
        }
        if (Values.isInteger(this.operand)) {
            return this.operand.w();
        }
        throw Assert.fail("Expected 'operand' to be of Number type, but was " + this.operand.getClass().getCanonicalName(), new Object[0]);
    }

    private long safeIncrement(long j4, long j6) {
        long j7 = j4 + j6;
        return ((j4 ^ j7) & (j6 ^ j7)) >= 0 ? j7 : j7 >= 0 ? Long.MIN_VALUE : Long.MAX_VALUE;
    }

    @Override // com.google.firebase.firestore.model.mutation.TransformOperation
    public H0 applyToLocalView(H0 h0, Timestamp timestamp) {
        H0 h0ComputeBaseValue = computeBaseValue(h0);
        if (Values.isInteger(h0ComputeBaseValue) && Values.isInteger(this.operand)) {
            long jSafeIncrement = safeIncrement(h0ComputeBaseValue.w(), operandAsLong());
            G0 g0C = H0.C();
            g0C.k(jSafeIncrement);
            return (H0) g0C.m359build();
        }
        if (Values.isInteger(h0ComputeBaseValue)) {
            double dW = h0ComputeBaseValue.w() + operandAsDouble();
            G0 g0C2 = H0.C();
            g0C2.i(dW);
            return (H0) g0C2.m359build();
        }
        Assert.hardAssert(Values.isDouble(h0ComputeBaseValue), "Expected NumberValue to be of type DoubleValue, but was ", h0.getClass().getCanonicalName());
        double dU = h0ComputeBaseValue.u() + operandAsDouble();
        G0 g0C3 = H0.C();
        g0C3.i(dU);
        return (H0) g0C3.m359build();
    }

    @Override // com.google.firebase.firestore.model.mutation.TransformOperation
    public H0 computeBaseValue(H0 h0) {
        if (Values.isNumber(h0)) {
            return h0;
        }
        G0 g0C = H0.C();
        g0C.k(0L);
        return (H0) g0C.m359build();
    }

    public H0 getOperand() {
        return this.operand;
    }

    @Override // com.google.firebase.firestore.model.mutation.TransformOperation
    public H0 applyToRemoteDocument(H0 h0, H0 h02) {
        return h02;
    }
}
