package d.e.a.s;

import d.e.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: compiled from: ValueCode.java */
/* JADX INFO: loaded from: classes.dex */
public class k extends b {
    public final boolean m;

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

    @Override // d.e.a.s.b, d.e.a.e
    public Writer b(Writer writer, List<Object> list) {
        try {
            Object objM = m(list);
            if (objM != null) {
                if (objM instanceof Function) {
                    r(writer, (Function) objM, list);
                } else {
                    if (objM instanceof Callable) {
                        q(writer, (Callable) objM);
                        Writer writerI = i(writer, list);
                        k(writerI);
                        return writerI;
                    }
                    Objects.requireNonNull(this.f2515f);
                    p(writer, objM.toString());
                }
            }
            Writer writerI2 = i(writer, list);
            k(writerI2);
            return writerI2;
        } catch (Exception e2) {
            StringBuilder sbB = d.b.a.a.a.B("Failed to get value for ");
            sbB.append(this.f2516g);
            throw new d.e.a.k(sbB.toString(), e2, this.f2517h);
        }
    }

    @Override // d.e.a.s.b, d.e.a.e
    public void h(Writer writer) {
        try {
            if (this.f2516g != null) {
                writer.write(this.f2517h.a);
                if (!this.m) {
                    writer.write("{");
                }
                writer.write(this.f2518i);
                writer.write(this.f2516g);
                if (!this.m) {
                    writer.write("}");
                }
                writer.write(this.f2517h.f2509b);
            }
            k(writer);
        } catch (IOException e2) {
            throw new d.e.a.k(e2, this.f2517h);
        }
    }

    public void p(Writer writer, String str) throws IOException {
        char[] cArr;
        if (str != null) {
            if (!this.m) {
                writer.write(str);
                return;
            }
            Objects.requireNonNull(this.f2521l);
            char[][] cArr2 = d.e.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 = d.e.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 d.e.a.k(d.b.a.a.a.p("Failed to encode value: ", str), e2);
            }
        }
    }

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

    public void r(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.f2517h;
            r rVar2 = new r("{{", "}}", rVar.f2510c, rVar.f2511d, rVar.f2512e);
            d.e.a.f fVar = this.f2521l;
            d.e.a.j jVarComputeIfAbsent = fVar.f2497c.computeIfAbsent(new d.e.a.h(rVar2, string2), new d.e.a.a(fVar));
            jVarComputeIfAbsent.init();
            jVarComputeIfAbsent.b(stringWriter, list).close();
            string = stringWriter.toString();
        }
        p(writer, string);
    }
}
