package com.github.mustachejava;

import com.github.mustachejava.codes.DefaultMustache;
import com.github.mustachejava.reflect.ReflectionObjectHandler;
import com.github.mustachejava.resolver.DefaultResolver;
import com.github.mustachejava.util.HtmlEscaper;
import java.io.File;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import java.util.function.Supplier;
import kotlinx.serialization.json.internal.AbstractJsonLexerKt;

/* JADX INFO: loaded from: classes.dex */
public class DefaultMustacheFactory implements MustacheFactory {
    protected ExecutorService es;
    protected final MustacheParser mc;
    protected final ConcurrentHashMap<String, Mustache> mustacheCache;
    private final MustacheResolver mustacheResolver;
    protected ObjectHandler oh;
    private final ThreadLocal<Map<String, Mustache>> partialCache;
    protected int recursionLimit;
    protected final ConcurrentHashMap<FragmentKey, Mustache> templateCache;

    public static /* synthetic */ HashMap $r8$lambda$zL9zIj_AuRar_VZKE_7we4Hn4rk() {
        return new HashMap();
    }

    public String filterText(String str, boolean z) {
        return str;
    }

    @Override // com.github.mustachejava.MustacheFactory
    public String translate(String str) {
        return str;
    }

    public DefaultMustacheFactory() {
        this.mustacheCache = createMustacheCache();
        this.oh = new ReflectionObjectHandler();
        this.mc = createParser();
        this.templateCache = createLambdaCache();
        this.recursionLimit = 100;
        this.partialCache = ThreadLocal.withInitial(new Supplier() { // from class: com.github.mustachejava.DefaultMustacheFactory$$ExternalSyntheticLambda1
            @Override // java.util.function.Supplier
            public final Object get() {
                return DefaultMustacheFactory.$r8$lambda$zL9zIj_AuRar_VZKE_7we4Hn4rk();
            }
        });
        this.mustacheResolver = new DefaultResolver();
    }

    public DefaultMustacheFactory(MustacheResolver mustacheResolver) {
        this.mustacheCache = createMustacheCache();
        this.oh = new ReflectionObjectHandler();
        this.mc = createParser();
        this.templateCache = createLambdaCache();
        this.recursionLimit = 100;
        this.partialCache = ThreadLocal.withInitial(new Supplier() { // from class: com.github.mustachejava.DefaultMustacheFactory$$ExternalSyntheticLambda1
            @Override // java.util.function.Supplier
            public final Object get() {
                return DefaultMustacheFactory.$r8$lambda$zL9zIj_AuRar_VZKE_7we4Hn4rk();
            }
        });
        this.mustacheResolver = mustacheResolver;
    }

    public DefaultMustacheFactory(String str) {
        this.mustacheCache = createMustacheCache();
        this.oh = new ReflectionObjectHandler();
        this.mc = createParser();
        this.templateCache = createLambdaCache();
        this.recursionLimit = 100;
        this.partialCache = ThreadLocal.withInitial(new Supplier() { // from class: com.github.mustachejava.DefaultMustacheFactory$$ExternalSyntheticLambda1
            @Override // java.util.function.Supplier
            public final Object get() {
                return DefaultMustacheFactory.$r8$lambda$zL9zIj_AuRar_VZKE_7we4Hn4rk();
            }
        });
        this.mustacheResolver = new DefaultResolver(str);
    }

    public DefaultMustacheFactory(File file) {
        this.mustacheCache = createMustacheCache();
        this.oh = new ReflectionObjectHandler();
        this.mc = createParser();
        this.templateCache = createLambdaCache();
        this.recursionLimit = 100;
        this.partialCache = ThreadLocal.withInitial(new Supplier() { // from class: com.github.mustachejava.DefaultMustacheFactory$$ExternalSyntheticLambda1
            @Override // java.util.function.Supplier
            public final Object get() {
                return DefaultMustacheFactory.$r8$lambda$zL9zIj_AuRar_VZKE_7we4Hn4rk();
            }
        });
        this.mustacheResolver = new DefaultResolver(file);
    }

    public String resolvePartialPath(String str, String str2, String str3) {
        String str4 = !str2.startsWith("/") ? str + str2 : str2;
        int iLastIndexOf = str2.lastIndexOf("/");
        if (iLastIndexOf != -1) {
            str2 = str2.substring(iLastIndexOf);
        }
        if (!str2.endsWith(str3) && !str2.contains(".")) {
            str4 = str4 + str3;
        }
        return ensureForwardSlash(new File(str4).getPath());
    }

    private static String ensureForwardSlash(String str) {
        return str.replace(AbstractJsonLexerKt.STRING_ESC, '/');
    }

    @Override // com.github.mustachejava.MustacheFactory
    public MustacheVisitor createMustacheVisitor() {
        return new DefaultMustacheVisitor(this);
    }

    @Override // com.github.mustachejava.MustacheFactory
    public Reader getReader(String str) {
        Reader reader = this.mustacheResolver.getReader(str);
        if (reader != null) {
            return reader;
        }
        throw new MustacheNotFoundException(str);
    }

    @Override // com.github.mustachejava.MustacheFactory
    public void encode(String str, Writer writer) {
        HtmlEscaper.escape(str, writer);
    }

    @Override // com.github.mustachejava.MustacheFactory
    public ObjectHandler getObjectHandler() {
        return this.oh;
    }

    public void setObjectHandler(ObjectHandler objectHandler) {
        this.oh = objectHandler;
    }

    public ExecutorService getExecutorService() {
        return this.es;
    }

    public void setExecutorService(ExecutorService executorService) {
        this.es = executorService;
    }

    public Mustache getFragment(FragmentKey fragmentKey) {
        Mustache mustacheComputeIfAbsent = this.templateCache.computeIfAbsent(fragmentKey, getFragmentCacheFunction());
        mustacheComputeIfAbsent.init();
        return mustacheComputeIfAbsent;
    }

    protected Function<FragmentKey, Mustache> getFragmentCacheFunction() {
        return new Function() { // from class: com.github.mustachejava.DefaultMustacheFactory$$ExternalSyntheticLambda0
            @Override // java.util.function.Function
            public final Object apply(Object obj) {
                return this.f$0.m19x6a0aeecd((FragmentKey) obj);
            }
        };
    }

    /* JADX INFO: renamed from: lambda$getFragmentCacheFunction$0$com-github-mustachejava-DefaultMustacheFactory, reason: not valid java name */
    /* synthetic */ Mustache m19x6a0aeecd(FragmentKey fragmentKey) {
        StringReader stringReader = new StringReader(fragmentKey.templateText);
        TemplateContext templateContext = fragmentKey.tc;
        return this.mc.compile(stringReader, templateContext.file(), templateContext.startChars(), templateContext.endChars(), templateContext.startOfLine());
    }

    @Override // com.github.mustachejava.MustacheFactory
    public Mustache compile(String str) {
        Mustache mustacheComputeIfAbsent = this.mustacheCache.computeIfAbsent(str, getMustacheCacheFunction());
        mustacheComputeIfAbsent.init();
        return mustacheComputeIfAbsent;
    }

    @Override // com.github.mustachejava.MustacheFactory
    public Mustache compile(Reader reader, String str) {
        return compile(reader, str, MustacheParser.DEFAULT_SM, MustacheParser.DEFAULT_EM);
    }

    public Mustache compile(Reader reader, String str, String str2, String str3) {
        Mustache mustacheCompile = this.mc.compile(reader, str, str2, str3);
        mustacheCompile.init();
        this.partialCache.remove();
        return mustacheCompile;
    }

    public void setRecursionLimit(int i) {
        this.recursionLimit = i;
    }

    public int getRecursionLimit() {
        return this.recursionLimit;
    }

    public Mustache compilePartial(String str) {
        Map<String, Mustache> map = this.partialCache.get();
        Mustache mustache = map.get(str);
        if (mustache != null) {
            if (mustache instanceof DefaultMustache) {
                ((DefaultMustache) mustache).setRecursive();
            }
            return mustache;
        }
        try {
            Mustache mustacheCompile = this.mc.compile(str);
            map.put(str, mustacheCompile);
            mustacheCompile.init();
            return mustacheCompile;
        } finally {
            map.remove(str);
        }
    }

    protected MustacheParser createParser() {
        return new MustacheParser(this);
    }

    protected Function<String, Mustache> getMustacheCacheFunction() {
        final MustacheParser mustacheParser = this.mc;
        mustacheParser.getClass();
        return new Function() { // from class: com.github.mustachejava.DefaultMustacheFactory$$ExternalSyntheticLambda2
            @Override // java.util.function.Function
            public final Object apply(Object obj) {
                return mustacheParser.compile((String) obj);
            }
        };
    }

    protected ConcurrentHashMap<String, Mustache> createMustacheCache() {
        return new ConcurrentHashMap<>();
    }

    protected ConcurrentHashMap<FragmentKey, Mustache> createLambdaCache() {
        return new ConcurrentHashMap<>();
    }
}
