package com.github.mustachejava.codes;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.FragmentKey;
import com.github.mustachejava.MustacheException;
import com.github.mustachejava.MustacheParser;
import com.github.mustachejava.TemplateContext;
import com.github.mustachejava.util.LatchedWriter;
import com.github.mustachejava.util.Node;
import com.github.mustachejava.util.NodeValue;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* JADX INFO: loaded from: classes.dex */
public class ValueCode extends DefaultCode {
    private Pattern compiledAppended;
    protected final boolean encoded;
    protected final ExecutorService les;

    @Override // com.github.mustachejava.codes.DefaultCode, com.github.mustachejava.Code
    public void identity(Writer writer) {
        try {
            if (this.name != null) {
                writer.write(this.tc.startChars());
                if (!this.encoded) {
                    writer.write("{");
                }
                writer.write(this.type);
                writer.write(this.name);
                if (!this.encoded) {
                    writer.write("}");
                }
                writer.write(this.tc.endChars());
            }
            appendText(writer);
        } catch (IOException e) {
            throw new MustacheException(e, this.tc);
        }
    }

    public ValueCode(TemplateContext templateContext, DefaultMustacheFactory defaultMustacheFactory, String str, boolean z) {
        super(templateContext, defaultMustacheFactory, null, str, "");
        this.encoded = z;
        this.les = defaultMustacheFactory.getExecutorService();
    }

    @Override // com.github.mustachejava.codes.DefaultCode, com.github.mustachejava.Code
    public Writer execute(Writer writer, List<Object> list) {
        try {
            Object obj = get(list);
            if (obj != null) {
                if (obj instanceof Function) {
                    handleFunction(writer, (Function) obj, list);
                } else {
                    if (obj instanceof Callable) {
                        return handleCallable(writer, (Callable) obj, list);
                    }
                    execute(writer, this.oh.stringify(obj));
                }
            }
            return super.execute(writer, list);
        } catch (Exception e) {
            throw new MustacheException("Failed to get value for " + this.name, e, this.tc);
        }
    }

    protected Writer handleCallable(final Writer writer, final Callable callable, List<Object> list) throws Exception {
        if (this.les == null) {
            execute(writer, callable);
            return super.execute(writer, list);
        }
        try {
            writer.flush();
            final LatchedWriter latchedWriter = new LatchedWriter(writer);
            this.les.execute(new Runnable() { // from class: com.github.mustachejava.codes.ValueCode$$ExternalSyntheticLambda0
                @Override // java.lang.Runnable
                public final void run() {
                    this.f$0.m23lambda$handleCallable$0$comgithubmustachejavacodesValueCode(writer, callable, latchedWriter);
                }
            });
            return super.execute(latchedWriter, list);
        } catch (IOException e) {
            throw new MustacheException("Failed to flush writer", e, this.tc);
        }
    }

    /* JADX INFO: renamed from: lambda$handleCallable$0$com-github-mustachejava-codes-ValueCode, reason: not valid java name */
    /* synthetic */ void m23lambda$handleCallable$0$comgithubmustachejavacodesValueCode(Writer writer, Callable callable, LatchedWriter latchedWriter) {
        try {
            execute(writer, callable);
            latchedWriter.done();
        } catch (Throwable th) {
            latchedWriter.failed(th);
        }
    }

    private void execute(Writer writer, Callable callable) throws Exception {
        Object objCall = callable.call();
        execute(writer, objCall == null ? null : this.oh.stringify(objCall));
    }

    protected void handleFunction(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();
            this.df.getFragment(new FragmentKey(new TemplateContext(MustacheParser.DEFAULT_SM, MustacheParser.DEFAULT_EM, this.tc.file(), this.tc.line(), this.tc.startOfLine()), string2)).execute((Writer) stringWriter, list).close();
            string = stringWriter.toString();
        }
        execute(writer, string);
    }

    protected void execute(Writer writer, String str) throws IOException {
        if (str != null) {
            if (this.encoded) {
                this.df.encode(str, writer);
            } else {
                writer.write(str);
            }
        }
    }

    @Override // com.github.mustachejava.codes.DefaultCode, com.github.mustachejava.Code
    public Node invert(Node node, String str, AtomicInteger atomicInteger) {
        if (this.compiledAppended == null) {
            if (this.appended == null) {
                this.compiledAppended = Pattern.compile("$");
            } else {
                this.compiledAppended = Pattern.compile(this.appended);
            }
        }
        int i = atomicInteger.get();
        Matcher matcher = this.compiledAppended.matcher(str);
        if (!matcher.find(atomicInteger.get())) {
            return null;
        }
        String strSubstring = str.substring(i, matcher.start());
        atomicInteger.set(matcher.start() + matcher.group(0).length());
        node.put(this.name, NodeValue.value(strSubstring));
        return node;
    }
}
