package com.github.mustachejava.codes;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.FragmentKey;
import com.github.mustachejava.Iteration;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheException;
import com.github.mustachejava.TemplateContext;
import com.github.mustachejava.TemplateFunction;
import com.github.mustachejava.util.InternalArrayList;
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.ArrayList;
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 org.eclipse.paho.mqttv5.common.util.MqttTopicValidator;

/* JADX INFO: loaded from: classes.dex */
public class IterableCode extends DefaultCode implements Iteration {
    private final ExecutorService les;

    public IterableCode(TemplateContext templateContext, DefaultMustacheFactory defaultMustacheFactory, Mustache mustache, String str, String str2) {
        super(templateContext, defaultMustacheFactory, mustache, str, str2);
        this.les = defaultMustacheFactory.getExecutorService();
    }

    public IterableCode(TemplateContext templateContext, DefaultMustacheFactory defaultMustacheFactory, Mustache mustache, String str) {
        this(templateContext, defaultMustacheFactory, mustache, str, MqttTopicValidator.MULTI_LEVEL_WILDCARD);
    }

    @Override // com.github.mustachejava.codes.DefaultCode, com.github.mustachejava.Code
    public Writer execute(Writer writer, List<Object> list) {
        Writer writerHandle = handle(writer, get(list), list);
        appendText(writerHandle);
        return writerHandle;
    }

    protected Writer handle(Writer writer, Object obj, List<Object> list) {
        if (obj == null) {
            return writer;
        }
        if (obj instanceof Function) {
            return handleFunction(writer, (Function) obj, list);
        }
        if (obj instanceof Callable) {
            return handleCallable(writer, (Callable) obj, list);
        }
        return execute(writer, obj, list);
    }

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

    /* JADX INFO: renamed from: lambda$handleCallable$0$com-github-mustachejava-codes-IterableCode, reason: not valid java name */
    /* synthetic */ void m22x75736ae6(Callable callable, Writer writer, List list, LatchedWriter latchedWriter) {
        try {
            Writer writerHandle = handle(writer, callable.call(), list);
            if (writerHandle instanceof LatchedWriter) {
                ((LatchedWriter) writerHandle).await();
            }
            latchedWriter.done();
        } catch (Throwable th) {
            latchedWriter.failed(th);
        }
    }

    protected Writer handleFunction(Writer writer, Function function, List<Object> list) {
        StringWriter stringWriter = new StringWriter();
        runIdentity(stringWriter);
        if (function instanceof TemplateFunction) {
            try {
                Object objApply = function.apply(stringWriter.toString());
                return objApply != null ? writeTemplate(writer, objApply.toString(), list) : writer;
            } catch (Exception e) {
                throw new MustacheException("Function failure", e, this.tc);
            }
        }
        try {
            StringWriter stringWriter2 = new StringWriter();
            writeTemplate(stringWriter2, stringWriter.toString(), list).close();
            try {
                Object objApply2 = function.apply(stringWriter2.toString());
                if (objApply2 == null) {
                    return writer;
                }
                writer.write(objApply2.toString());
                return writer;
            } catch (Exception e2) {
                throw new MustacheException("Function failure", e2, this.tc);
            }
        } catch (IOException e3) {
            throw new MustacheException("Failed to write function result", e3, this.tc);
        }
    }

    protected Writer writeTemplate(Writer writer, String str, List<Object> list) {
        return this.df.getFragment(new FragmentKey(this.tc, str)).execute(writer, list);
    }

    protected Writer execute(Writer writer, Object obj, List<Object> list) {
        return this.oh.iterate(this, writer, obj, list);
    }

    @Override // com.github.mustachejava.Iteration
    public Writer next(Writer writer, Object obj, List<Object> list) {
        boolean zAddScope = addScope(list, obj);
        Writer writerRun = run(writer, list);
        if (zAddScope) {
            list.remove(list.size() - 1);
        }
        return writerRun;
    }

    @Override // com.github.mustachejava.codes.DefaultCode, com.github.mustachejava.Code
    public Node invert(Node node, String str, AtomicInteger atomicInteger) {
        int i = atomicInteger.get();
        ArrayList arrayList = new ArrayList();
        while (true) {
            Node nodeInvert = this.mustache.invert(new Node(), str, atomicInteger);
            if (nodeInvert != null) {
                arrayList.add(nodeInvert);
            } else {
                node.put(this.name, NodeValue.list(arrayList));
                return matchAppended(node, str, atomicInteger, i);
            }
        }
    }
}
