package com.github.mustachejava.codes;

import com.github.mustachejava.Code;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheException;
import com.github.mustachejava.TemplateContext;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/* JADX INFO: loaded from: classes.dex */
public class ExtendCode extends PartialCode {
    private final DefaultMustacheFactory mf;

    public ExtendCode(TemplateContext templateContext, DefaultMustacheFactory defaultMustacheFactory, Mustache mustache, String str) throws MustacheException {
        super(templateContext, defaultMustacheFactory, mustache, "<", str);
        this.mf = defaultMustacheFactory;
    }

    private Code[] replaceCodes(Code[] codeArr, Map<String, ExtendNameCode> map, Set<Code> set) {
        Code[] codeArr2 = (Code[]) codeArr.clone();
        for (int i = 0; i < codeArr.length; i++) {
            Code code = codeArr[i];
            if (set.add(code)) {
                if (code instanceof ExtendNameCode) {
                    ExtendNameCode extendNameCode = (ExtendNameCode) code;
                    ExtendNameCode extendNameCode2 = map.get(extendNameCode.getName());
                    if (extendNameCode2 != null) {
                        Code code2 = (Code) extendNameCode2.clone();
                        codeArr2[i] = code2;
                        ((ExtendNameCode) code2).appended = extendNameCode.appended;
                    } else {
                        extendNameCode.setCodes(replaceCodes(extendNameCode.getCodes(), map, set));
                    }
                } else {
                    if ((code instanceof ExtendCheckNameCode) && !map.containsKey(((ExtendCheckNameCode) code).getName())) {
                        code.setCodes(new Code[0]);
                    }
                    Code[] codes = code.getCodes();
                    if (codes != null) {
                        code.setCodes(replaceCodes(codes, map, set));
                    }
                }
                set.remove(code);
            }
        }
        return codeArr2;
    }

    @Override // com.github.mustachejava.codes.PartialCode, com.github.mustachejava.codes.DefaultCode, com.github.mustachejava.Code
    public synchronized void init() {
        filterText();
        HashMap map = new HashMap();
        for (Code code : this.mustache.getCodes()) {
            if (code instanceof ExtendNameCode) {
                ExtendNameCode extendNameCode = (ExtendNameCode) code;
                map.put(extendNameCode.getName(), extendNameCode);
                extendNameCode.init();
            } else if (!(code instanceof WriteCode) && !(code instanceof CommentCode)) {
                throw new IllegalArgumentException("Illegal code in extend section: " + code.getClass().getName());
            }
        }
        this.partial = (Mustache) this.mf.compilePartial(partialName()).clone();
        Code[] codes = this.partial.getCodes();
        HashSet hashSet = new HashSet();
        hashSet.add(this.partial);
        this.partial.setCodes(replaceCodes(codes, map, hashSet));
    }
}
