package com.google.firebase.firestore.core;

import com.google.firebase.firestore.core.OrderBy;
import com.google.firebase.firestore.model.Document;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.FieldPath;
import com.google.firebase.firestore.model.Values;
import com.google.firebase.firestore.util.Assert;
import e4.H0;
import java.util.List;

/* JADX INFO: loaded from: classes3.dex */
public final class Bound {
    private final boolean inclusive;
    private final List<H0> position;

    public Bound(List<H0> list, boolean z6) {
        this.position = list;
        this.inclusive = z6;
    }

    private int compareToDocument(List<OrderBy> list, Document document) {
        int iCompare;
        Assert.hardAssert(this.position.size() <= list.size(), "Bound has more components than query's orderBy", new Object[0]);
        int i6 = 0;
        for (int i7 = 0; i7 < this.position.size(); i7++) {
            OrderBy orderBy = list.get(i7);
            H0 h0 = this.position.get(i7);
            if (orderBy.field.equals(FieldPath.KEY_PATH)) {
                Assert.hardAssert(Values.isReferenceValue(h0), "Bound has a non-key value where the key path is being used %s", h0);
                iCompare = DocumentKey.fromName(h0.y()).compareTo(document.getKey());
            } else {
                H0 field = document.getField(orderBy.getField());
                Assert.hardAssert(field != null, "Field should exist since document matched the orderBy already.", new Object[0]);
                iCompare = Values.compare(h0, field);
            }
            if (orderBy.getDirection().equals(OrderBy.Direction.DESCENDING)) {
                iCompare *= -1;
            }
            i6 = iCompare;
            if (i6 != 0) {
                return i6;
            }
        }
        return i6;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj != null && Bound.class == obj.getClass()) {
            Bound bound = (Bound) obj;
            if (this.inclusive == bound.inclusive && this.position.equals(bound.position)) {
                return true;
            }
        }
        return false;
    }

    public List<H0> getPosition() {
        return this.position;
    }

    public int hashCode() {
        return this.position.hashCode() + ((this.inclusive ? 1 : 0) * 31);
    }

    public boolean isInclusive() {
        return this.inclusive;
    }

    public String positionString() {
        StringBuilder sb = new StringBuilder();
        boolean z6 = true;
        for (H0 h0 : this.position) {
            if (!z6) {
                sb.append(",");
            }
            sb.append(Values.canonicalId(h0));
            z6 = false;
        }
        return sb.toString();
    }

    public boolean sortsAfterDocument(List<OrderBy> list, Document document) {
        int iCompareToDocument = compareToDocument(list, document);
        return this.inclusive ? iCompareToDocument >= 0 : iCompareToDocument > 0;
    }

    public boolean sortsBeforeDocument(List<OrderBy> list, Document document) {
        int iCompareToDocument = compareToDocument(list, document);
        return this.inclusive ? iCompareToDocument <= 0 : iCompareToDocument < 0;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder("Bound(inclusive=");
        sb.append(this.inclusive);
        sb.append(", position=");
        for (int i6 = 0; i6 < this.position.size(); i6++) {
            if (i6 > 0) {
                sb.append(" and ");
            }
            sb.append(Values.canonicalId(this.position.get(i6)));
        }
        sb.append(")");
        return sb.toString();
    }
}
