package g.d.a.s;

import g.d.a.r;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.function.Function;

/* JADX INFO: loaded from: classes.dex */
public class k extends b {

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final boolean f3503u;

    public k(r rVar, g.d.a.f fVar, String str, boolean z2) {
        super(rVar, fVar, null, str, "");
        this.f3503u = z2;
        Objects.requireNonNull(fVar);
    }

    @Override // g.d.a.s.b, g.d.a.e
    public Writer c(Writer writer, List<Object> list) {
        try {
            Object objO = o(list);
            if (objO != null) {
                if (objO instanceof Function) {
                    t(writer, (Function) objO, list);
                } else {
                    if (objO instanceof Callable) {
                        s(writer, (Callable) objO);
                        Writer writerK = k(writer, list);
                        m(writerK);
                        return writerK;
                    }
                    Objects.requireNonNull(this.f3486n);
                    r(writer, objO.toString());
                }
            }
            Writer writerK2 = k(writer, list);
            m(writerK2);
            return writerK2;
        } catch (Exception e2) {
            StringBuilder sbF = g.a.a.a.a.F("Failed to get value for ");
            sbF.append(this.f3487o);
            throw new g.d.a.k(sbF.toString(), e2, this.f3488p);
        }
    }

    @Override // g.d.a.s.b, g.d.a.e
    public void j(Writer writer) {
        try {
            if (this.f3487o != null) {
                writer.write(this.f3488p.a);
                if (!this.f3503u) {
                    writer.write("{");
                }
                writer.write(this.f3489q);
                writer.write(this.f3487o);
                if (!this.f3503u) {
                    writer.write("}");
                }
                writer.write(this.f3488p.f3480b);
            }
            m(writer);
        } catch (IOException e2) {
            throw new g.d.a.k(e2, this.f3488p);
        }
    }

    public void r(Writer writer, String str) throws IOException {
        char[] cArr;
        if (str != null) {
            if (!this.f3503u) {
                writer.write(str);
                return;
            }
            Objects.requireNonNull(this.f3492t);
            char[][] cArr2 = g.d.a.v.b.a;
            try {
                char[] charArray = str.toCharArray();
                int length = charArray.length;
                int i2 = 0;
                for (int i3 = 0; i3 < length; i3++) {
                    char c2 = charArray[i3];
                    if (c2 <= '`' && (cArr = g.d.a.v.b.a[c2]) != null) {
                        if (i3 > i2) {
                            writer.write(charArray, i2, i3 - i2);
                        }
                        writer.write(cArr);
                        i2 = i3 + 1;
                    }
                }
                writer.write(charArray, i2, length - i2);
            } catch (IOException e2) {
                throw new g.d.a.k(g.a.a.a.a.t("Failed to encode value: ", str), e2);
            }
        }
    }

    public final void s(Writer writer, Callable callable) throws Exception {
        String string;
        Object objCall = callable.call();
        if (objCall == null) {
            string = null;
        } else {
            Objects.requireNonNull(this.f3486n);
            string = objCall.toString();
        }
        r(writer, string);
    }

    public void t(Writer writer, Function function, List<Object> list) throws IOException {
        String string;
        Object objApply = function.apply(null);
        if (objApply == null) {
            string = "";
        } else {
            String string2 = objApply.toString();
            StringWriter stringWriter = new StringWriter();
            r rVar = this.f3488p;
            r rVar2 = new r("{{", "}}", rVar.f3481c, rVar.f3482d, rVar.f3483e);
            g.d.a.f fVar = this.f3492t;
            g.d.a.j jVarComputeIfAbsent = fVar.f3468c.computeIfAbsent(new g.d.a.h(rVar2, string2), new g.d.a.a(fVar));
            jVarComputeIfAbsent.init();
            jVarComputeIfAbsent.c(stringWriter, list).close();
            string = stringWriter.toString();
        }
        r(writer, string);
    }
}
