package org.joda.time.format;

import com.google.android.gms.common.api.Api;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/* loaded from: classes5.dex */
public class o {

    /* renamed from: j, reason: collision with root package name */
    private static final ConcurrentMap f32153j = new ConcurrentHashMap();

    /* renamed from: a, reason: collision with root package name */
    private int f32154a;

    /* renamed from: b, reason: collision with root package name */
    private int f32155b;

    /* renamed from: c, reason: collision with root package name */
    private int f32156c;

    /* renamed from: d, reason: collision with root package name */
    private boolean f32157d;

    /* renamed from: e, reason: collision with root package name */
    private f f32158e;

    /* renamed from: f, reason: collision with root package name */
    private List f32159f;

    /* renamed from: g, reason: collision with root package name */
    private boolean f32160g;

    /* renamed from: h, reason: collision with root package name */
    private boolean f32161h;

    /* renamed from: i, reason: collision with root package name */
    private c[] f32162i;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes5.dex */
    public static class a implements q, p {

        /* renamed from: a, reason: collision with root package name */
        private final q[] f32163a;

        /* renamed from: b, reason: collision with root package name */
        private final p[] f32164b;

        a(List list) {
            ArrayList arrayList = new ArrayList();
            ArrayList arrayList2 = new ArrayList();
            b(list, arrayList, arrayList2);
            if (arrayList.size() <= 0) {
                this.f32163a = null;
            } else {
                this.f32163a = (q[]) arrayList.toArray(new q[arrayList.size()]);
            }
            if (arrayList2.size() <= 0) {
                this.f32164b = null;
            } else {
                this.f32164b = (p[]) arrayList2.toArray(new p[arrayList2.size()]);
            }
        }

        private void a(List list, Object[] objArr) {
            if (objArr != null) {
                for (Object obj : objArr) {
                    list.add(obj);
                }
            }
        }

        private void b(List list, List list2, List list3) {
            int size = list.size();
            for (int i10 = 0; i10 < size; i10 += 2) {
                Object obj = list.get(i10);
                if (obj instanceof q) {
                    if (obj instanceof a) {
                        a(list2, ((a) obj).f32163a);
                    } else {
                        list2.add(obj);
                    }
                }
                Object obj2 = list.get(i10 + 1);
                if (obj2 instanceof p) {
                    if (obj2 instanceof a) {
                        a(list3, ((a) obj2).f32164b);
                    } else {
                        list3.add(obj2);
                    }
                }
            }
        }
    }

    /* loaded from: classes5.dex */
    static class b extends d {

        /* renamed from: b, reason: collision with root package name */
        private final f f32165b;

        /* renamed from: c, reason: collision with root package name */
        private final f f32166c;

        /* renamed from: d, reason: collision with root package name */
        private final String[] f32167d;

        b(f fVar, f fVar2) {
            this.f32165b = fVar;
            this.f32166c = fVar2;
            HashSet hashSet = new HashSet();
            for (String str : fVar.a()) {
                for (String str2 : this.f32166c.a()) {
                    hashSet.add(str + str2);
                }
            }
            this.f32167d = (String[]) hashSet.toArray(new String[hashSet.size()]);
        }

        @Override // org.joda.time.format.o.f
        public String[] a() {
            return (String[]) this.f32167d.clone();
        }
    }

    /* loaded from: classes5.dex */
    static abstract class d implements f {

        /* renamed from: a, reason: collision with root package name */
        private volatile String[] f32176a;

        d() {
        }

        @Override // org.joda.time.format.o.f
        public void b(Set set) {
            if (this.f32176a == null) {
                String[] a10 = a();
                int i10 = Api.BaseClientBuilder.API_PRIORITY_OTHER;
                String str = null;
                for (String str2 : a10) {
                    if (str2.length() < i10) {
                        i10 = str2.length();
                        str = str2;
                    }
                }
                HashSet hashSet = new HashSet();
                Iterator it = set.iterator();
                while (it.hasNext()) {
                    f fVar = (f) it.next();
                    if (fVar != null) {
                        for (String str3 : fVar.a()) {
                            if (str3.length() > i10 || (str3.equalsIgnoreCase(str) && !str3.equals(str))) {
                                hashSet.add(str3);
                            }
                        }
                    }
                }
                this.f32176a = (String[]) hashSet.toArray(new String[hashSet.size()]);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes5.dex */
    public static class e implements q, p {

        /* renamed from: b, reason: collision with root package name */
        static final e f32177b = new e("");

        /* renamed from: a, reason: collision with root package name */
        private final String f32178a;

        e(String str) {
            this.f32178a = str;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes5.dex */
    public interface f {
        String[] a();

        void b(Set set);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes5.dex */
    public static class g implements q, p {

        /* renamed from: a, reason: collision with root package name */
        private final String f32179a;

        /* renamed from: b, reason: collision with root package name */
        private final String f32180b;

        /* renamed from: c, reason: collision with root package name */
        private final String[] f32181c;

        /* renamed from: d, reason: collision with root package name */
        private final boolean f32182d;

        /* renamed from: e, reason: collision with root package name */
        private final boolean f32183e;

        /* renamed from: f, reason: collision with root package name */
        private final q f32184f;

        /* renamed from: g, reason: collision with root package name */
        private volatile q f32185g;

        /* renamed from: h, reason: collision with root package name */
        private final p f32186h;

        /* renamed from: i, reason: collision with root package name */
        private volatile p f32187i;

        g(String str, String str2, String[] strArr, q qVar, p pVar, boolean z10, boolean z11) {
            this.f32179a = str;
            this.f32180b = str2;
            if ((str2 == null || str.equals(str2)) && (strArr == null || strArr.length == 0)) {
                this.f32181c = new String[]{str};
            } else {
                TreeSet treeSet = new TreeSet(String.CASE_INSENSITIVE_ORDER);
                treeSet.add(str);
                treeSet.add(str2);
                if (strArr != null) {
                    int length = strArr.length;
                    while (true) {
                        length--;
                        if (length < 0) {
                            break;
                        } else {
                            treeSet.add(strArr[length]);
                        }
                    }
                }
                ArrayList arrayList = new ArrayList(treeSet);
                Collections.reverse(arrayList);
                this.f32181c = (String[]) arrayList.toArray(new String[arrayList.size()]);
            }
            this.f32184f = qVar;
            this.f32186h = pVar;
            this.f32182d = z10;
            this.f32183e = z11;
        }

        g c(q qVar, p pVar) {
            this.f32185g = qVar;
            this.f32187i = pVar;
            return this;
        }
    }

    /* loaded from: classes5.dex */
    static class h extends d {

        /* renamed from: b, reason: collision with root package name */
        private final String f32188b;

        h(String str) {
            this.f32188b = str;
        }

        @Override // org.joda.time.format.o.f
        public String[] a() {
            return new String[]{this.f32188b};
        }
    }

    public o() {
        p();
    }

    private o a(q qVar, p pVar) {
        this.f32159f.add(qVar);
        this.f32159f.add(pVar);
        this.f32160g = (qVar == null) | this.f32160g;
        this.f32161h |= pVar == null;
        return this;
    }

    private void c(int i10) {
        d(i10, this.f32154a);
    }

    private void d(int i10, int i11) {
        c cVar = new c(i11, this.f32155b, this.f32156c, this.f32157d, i10, this.f32162i, this.f32158e, null);
        a(cVar, cVar);
        this.f32162i[i10] = cVar;
        this.f32158e = null;
    }

    private o j(String str, String str2, String[] strArr, boolean z10, boolean z11) {
        g gVar;
        if (str == null || str2 == null) {
            throw new IllegalArgumentException();
        }
        q();
        List list = this.f32159f;
        if (list.size() == 0) {
            if (z11 && !z10) {
                e eVar = e.f32177b;
                g gVar2 = new g(str, str2, strArr, eVar, eVar, z10, z11);
                a(gVar2, gVar2);
            }
            return this;
        }
        int size = list.size();
        while (true) {
            int i10 = size - 1;
            if (i10 < 0) {
                gVar = null;
                break;
            }
            if (list.get(i10) instanceof g) {
                gVar = (g) list.get(i10);
                list = list.subList(i10 + 1, list.size());
                break;
            }
            size = i10 - 1;
        }
        List list2 = list;
        if (gVar != null && list2.size() == 0) {
            throw new IllegalStateException("Cannot have two adjacent separators");
        }
        Object[] r10 = r(list2);
        list2.clear();
        g gVar3 = new g(str, str2, strArr, (q) r10[0], (p) r10[1], z10, z11);
        list2.add(gVar3);
        list2.add(gVar3);
        return this;
    }

    private o m(f fVar) {
        Object obj;
        Object obj2;
        if (this.f32159f.size() > 0) {
            obj = this.f32159f.get(r0.size() - 2);
            obj2 = this.f32159f.get(r1.size() - 1);
        } else {
            obj = null;
            obj2 = null;
        }
        if (obj == null || obj2 == null || obj != obj2 || !(obj instanceof c)) {
            throw new IllegalStateException("No field to apply suffix to");
        }
        q();
        c cVar = new c((c) obj, fVar);
        this.f32159f.set(r4.size() - 2, cVar);
        this.f32159f.set(r4.size() - 1, cVar);
        this.f32162i[cVar.b()] = cVar;
        return this;
    }

    private void q() {
        if (this.f32158e != null) {
            throw new IllegalStateException("Prefix not followed by field");
        }
        this.f32158e = null;
    }

    private static Object[] r(List list) {
        int size = list.size();
        if (size == 0) {
            e eVar = e.f32177b;
            return new Object[]{eVar, eVar};
        }
        if (size == 1) {
            return new Object[]{list.get(0), list.get(1)};
        }
        a aVar = new a(list);
        return new Object[]{aVar, aVar};
    }

    private static n t(List list, boolean z10, boolean z11) {
        if (z10 && z11) {
            throw new IllegalStateException("Builder has created neither a printer nor a parser");
        }
        int size = list.size();
        if (size >= 2 && (list.get(0) instanceof g)) {
            g gVar = (g) list.get(0);
            if (gVar.f32187i == null && gVar.f32185g == null) {
                n t10 = t(list.subList(2, size), z10, z11);
                g c10 = gVar.c(t10.b(), t10.a());
                return new n(c10, c10);
            }
        }
        Object[] r10 = r(list);
        return z10 ? new n(null, (p) r10[1]) : z11 ? new n((q) r10[0], null) : new n((q) r10[0], (p) r10[1]);
    }

    public o b() {
        c(3);
        return this;
    }

    public o e() {
        c(4);
        return this;
    }

    public o f(String str) {
        if (str == null) {
            throw new IllegalArgumentException("Literal must not be null");
        }
        q();
        e eVar = new e(str);
        a(eVar, eVar);
        return this;
    }

    public o g() {
        c(5);
        return this;
    }

    public o h() {
        c(1);
        return this;
    }

    public o i() {
        c(9);
        return this;
    }

    public o k(String str) {
        return j(str, str, null, false, true);
    }

    public o l(String str) {
        if (str != null) {
            return m(new h(str));
        }
        throw new IllegalArgumentException();
    }

    public o n() {
        c(2);
        return this;
    }

    public o o() {
        c(0);
        return this;
    }

    public void p() {
        this.f32154a = 1;
        this.f32155b = 2;
        this.f32156c = 10;
        this.f32157d = false;
        this.f32158e = null;
        List list = this.f32159f;
        if (list == null) {
            this.f32159f = new ArrayList();
        } else {
            list.clear();
        }
        this.f32160g = false;
        this.f32161h = false;
        this.f32162i = new c[10];
    }

    public n s() {
        n t10 = t(this.f32159f, this.f32160g, this.f32161h);
        for (c cVar : this.f32162i) {
            if (cVar != null) {
                cVar.a(this.f32162i);
            }
        }
        this.f32162i = (c[]) this.f32162i.clone();
        return t10;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes5.dex */
    public static class c implements q, p {

        /* renamed from: a, reason: collision with root package name */
        private final int f32168a;

        /* renamed from: b, reason: collision with root package name */
        private final int f32169b;

        /* renamed from: c, reason: collision with root package name */
        private final int f32170c;

        /* renamed from: d, reason: collision with root package name */
        private final boolean f32171d;

        /* renamed from: e, reason: collision with root package name */
        private final int f32172e;

        /* renamed from: f, reason: collision with root package name */
        private final c[] f32173f;

        /* renamed from: g, reason: collision with root package name */
        private final f f32174g;

        /* renamed from: h, reason: collision with root package name */
        private final f f32175h;

        c(int i10, int i11, int i12, boolean z10, int i13, c[] cVarArr, f fVar, f fVar2) {
            this.f32168a = i10;
            this.f32169b = i11;
            this.f32170c = i12;
            this.f32171d = z10;
            this.f32172e = i13;
            this.f32173f = cVarArr;
            this.f32174g = fVar;
            this.f32175h = fVar2;
        }

        public void a(c[] cVarArr) {
            HashSet hashSet = new HashSet();
            HashSet hashSet2 = new HashSet();
            for (c cVar : cVarArr) {
                if (cVar != null && !equals(cVar)) {
                    hashSet.add(cVar.f32174g);
                    hashSet2.add(cVar.f32175h);
                }
            }
            f fVar = this.f32174g;
            if (fVar != null) {
                fVar.b(hashSet);
            }
            f fVar2 = this.f32175h;
            if (fVar2 != null) {
                fVar2.b(hashSet2);
            }
        }

        int b() {
            return this.f32172e;
        }

        c(c cVar, f fVar) {
            this.f32168a = cVar.f32168a;
            this.f32169b = cVar.f32169b;
            this.f32170c = cVar.f32170c;
            this.f32171d = cVar.f32171d;
            this.f32172e = cVar.f32172e;
            this.f32173f = cVar.f32173f;
            this.f32174g = cVar.f32174g;
            f fVar2 = cVar.f32175h;
            this.f32175h = fVar2 != null ? new b(fVar2, fVar) : fVar;
        }
    }
}
