package com.bytedance.sdk.openadsdk.lse;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/* JADX INFO: loaded from: classes2.dex */
public final class jnr {
    static final /* synthetic */ boolean fm = true;
    private static final List<CharsetEncoder> ro = new ArrayList();

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private final CharsetEncoder[] f7466lb;
    private final int yz;

    /* JADX WARN: Removed duplicated region for block: B:28:0x006e  */
    /* JADX WARN: Removed duplicated region for block: B:61:0x006f A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public jnr(String str, Charset charset, int i10) {
        int i11;
        boolean z10;
        ArrayList<CharsetEncoder> arrayList = new ArrayList();
        arrayList.add(StandardCharsets.ISO_8859_1.newEncoder());
        boolean z11 = charset != null && charset.name().startsWith("UTF");
        for (int i12 = 0; i12 < str.length(); i12++) {
            for (CharsetEncoder charsetEncoder : arrayList) {
                char cCharAt = str.charAt(i12);
                if (cCharAt == i10 || charsetEncoder.canEncode(cCharAt)) {
                    z10 = true;
                    break;
                }
            }
            z10 = false;
            if (!z10) {
                for (CharsetEncoder charsetEncoder2 : ro) {
                    if (charsetEncoder2.canEncode(str.charAt(i12))) {
                        arrayList.add(charsetEncoder2);
                        break;
                    }
                }
                if (z10) {
                    z11 = true;
                }
            } else if (z10) {
            }
        }
        if (arrayList.size() != 1 || z11) {
            this.f7466lb = new CharsetEncoder[arrayList.size() + 2];
            Iterator it = arrayList.iterator();
            int i13 = 0;
            while (it.hasNext()) {
                this.f7466lb[i13] = (CharsetEncoder) it.next();
                i13++;
            }
            this.f7466lb[i13] = StandardCharsets.UTF_8.newEncoder();
            this.f7466lb[i13 + 1] = StandardCharsets.UTF_16BE.newEncoder();
        } else {
            this.f7466lb = new CharsetEncoder[]{(CharsetEncoder) arrayList.get(0)};
        }
        if (charset != null) {
            i11 = 0;
            while (true) {
                CharsetEncoder[] charsetEncoderArr = this.f7466lb;
                if (i11 >= charsetEncoderArr.length) {
                    break;
                }
                if (charsetEncoderArr[i11] != null && charset.name().equals(this.f7466lb[i11].charset().name())) {
                    break;
                } else {
                    i11++;
                }
            }
        } else {
            i11 = -1;
        }
        this.yz = i11;
        if (!fm && !this.f7466lb[0].charset().equals(StandardCharsets.ISO_8859_1)) {
            throw new AssertionError();
        }
    }

    public int fm() {
        return this.f7466lb.length;
    }

    public int ro() {
        return this.yz;
    }

    public Charset fm(int i10) {
        if (fm || i10 < fm()) {
            return this.f7466lb[i10].charset();
        }
        throw new AssertionError();
    }

    public int ro(int i10) {
        return yz.fm(this.f7466lb[i10].charset()).fm();
    }

    public boolean fm(char c6, int i10) {
        if (fm || i10 < fm()) {
            return this.f7466lb[i10].canEncode(String.valueOf(c6));
        }
        throw new AssertionError();
    }

    public byte[] fm(String str, int i10) {
        if (fm || i10 < fm()) {
            return str.getBytes(this.f7466lb[i10].charset());
        }
        throw new AssertionError();
    }
}
