package com.google.firebase.firestore.model;

import com.google.firebase.Timestamp;
import com.google.protobuf.Q0;
import com.google.protobuf.R0;
import e4.G0;
import e4.H;
import e4.H0;
import e4.J;

/* JADX INFO: loaded from: classes3.dex */
public final class ServerTimestamps {
    private static final String LOCAL_WRITE_TIME_KEY = "__local_write_time__";
    private static final String PREVIOUS_VALUE_KEY = "__previous_value__";
    private static final String SERVER_TIMESTAMP_SENTINEL = "server_timestamp";
    private static final String TYPE_KEY = "__type__";

    private ServerTimestamps() {
    }

    public static R0 getLocalWriteTime(H0 h0) {
        return h0.x().k(LOCAL_WRITE_TIME_KEY).A();
    }

    public static H0 getPreviousValue(H0 h0) {
        H0 h0J = h0.x().j(PREVIOUS_VALUE_KEY);
        return isServerTimestamp(h0J) ? getPreviousValue(h0J) : h0J;
    }

    public static boolean isServerTimestamp(H0 h0) {
        H0 h0J = h0 == null ? null : h0.x().j("__type__");
        return h0J != null && SERVER_TIMESTAMP_SENTINEL.equals(h0J.z());
    }

    public static H0 valueOf(Timestamp timestamp, H0 h0) {
        G0 g0C = H0.C();
        g0C.p(SERVER_TIMESTAMP_SENTINEL);
        H0 h02 = (H0) g0C.m359build();
        G0 g0C2 = H0.C();
        Q0 q0K = R0.k();
        q0K.d(timestamp.getSeconds());
        q0K.c(timestamp.getNanoseconds());
        g0C2.q(q0K);
        H0 h03 = (H0) g0C2.m359build();
        H hL = J.l();
        hL.e(h02, "__type__");
        hL.e(h03, LOCAL_WRITE_TIME_KEY);
        if (isServerTimestamp(h0)) {
            h0 = getPreviousValue(h0);
        }
        if (h0 != null) {
            hL.e(h0, PREVIOUS_VALUE_KEY);
        }
        G0 g0C3 = H0.C();
        g0C3.l(hL);
        return (H0) g0C3.m359build();
    }
}
