package com.github.mustachejava.codes;

import com.github.mustachejava.Binding;
import com.github.mustachejava.Code;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheException;
import com.github.mustachejava.ObjectHandler;
import com.github.mustachejava.TemplateContext;
import com.github.mustachejava.util.Node;
import java.io.IOException;
import java.io.Writer;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: loaded from: classes.dex */
public class DefaultCode implements Code, Cloneable {
    protected String appended;
    private char[] appendedChars;
    protected final Binding binding;
    protected final DefaultMustacheFactory df;
    protected Mustache mustache;
    protected final String name;
    protected final ObjectHandler oh;
    protected final boolean returnThis;
    protected final TemplateContext tc;
    protected final String type;

    @Override // com.github.mustachejava.Code
    public Object clone() {
        HashSet hashSet = new HashSet();
        hashSet.add(this);
        return clone(hashSet);
    }

    @Override // com.github.mustachejava.Code
    public Object clone(Set<Code> set) {
        try {
            DefaultCode defaultCode = (DefaultCode) super.clone();
            Code[] codes = defaultCode.getCodes();
            if (codes != null) {
                Code[] codeArr = (Code[]) codes.clone();
                for (int i = 0; i < codeArr.length; i++) {
                    if (!set.add(codeArr[i])) {
                        Code code = (Code) codeArr[i].clone(set);
                        codeArr[i] = code;
                        set.remove(code);
                    }
                }
                defaultCode.setCodes(codeArr);
            }
            Mustache mustache = this.mustache;
            if (mustache != null && !set.add(mustache)) {
                defaultCode.mustache = (Mustache) this.mustache.clone(set);
                set.remove(this.mustache);
            }
            return defaultCode;
        } catch (CloneNotSupportedException e) {
            throw new MustacheException("Clone not supported", e, this.tc);
        }
    }

    public DefaultCode() {
        this(null, null, null, null, null);
    }

    public DefaultCode(TemplateContext templateContext, DefaultMustacheFactory defaultMustacheFactory, Mustache mustache, String str, String str2) {
        this.df = defaultMustacheFactory;
        ObjectHandler objectHandler = defaultMustacheFactory == null ? null : defaultMustacheFactory.getObjectHandler();
        this.oh = objectHandler;
        this.mustache = mustache;
        this.type = str2;
        this.name = str;
        this.tc = templateContext;
        this.binding = objectHandler != null ? objectHandler.createBinding(str, templateContext, this) : null;
        this.returnThis = ".".equals(str);
    }

    @Override // com.github.mustachejava.Code
    public Node invert(Node node, String str, AtomicInteger atomicInteger) {
        int i = atomicInteger.get();
        Code[] codes = getCodes();
        if (codes != null) {
            for (Code code : codes) {
                if (code.invert(node, str, atomicInteger) == null) {
                    atomicInteger.set(i);
                    return null;
                }
            }
        }
        return matchAppended(node, str, atomicInteger, i);
    }

    protected Node matchAppended(Node node, String str, AtomicInteger atomicInteger, int i) {
        if (this.appended == null) {
            return node;
        }
        if (str.substring(atomicInteger.get()).startsWith(this.appended)) {
            atomicInteger.addAndGet(this.appended.length());
            return node;
        }
        atomicInteger.set(i);
        return null;
    }

    @Override // com.github.mustachejava.Code
    public Code[] getCodes() {
        Mustache mustache = this.mustache;
        if (mustache == null) {
            return null;
        }
        return mustache.getCodes();
    }

    @Override // com.github.mustachejava.Code
    public synchronized void init() {
        filterText();
        Code[] codes = getCodes();
        if (codes != null) {
            for (Code code : codes) {
                code.init();
            }
        }
    }

    protected void filterText() {
        String str;
        DefaultMustacheFactory defaultMustacheFactory = this.df;
        if (defaultMustacheFactory == null || (str = this.appended) == null) {
            return;
        }
        this.appended = defaultMustacheFactory.filterText(str, this.tc.startOfLine());
    }

    @Override // com.github.mustachejava.Code
    public void setCodes(Code[] codeArr) {
        this.mustache.setCodes(codeArr);
    }

    public Object get(List<Object> list) {
        if (this.returnThis) {
            int size = list == null ? 0 : list.size();
            if (size == 0) {
                return null;
            }
            return list.get(size - 1);
        }
        try {
            return this.binding.get(list);
        } catch (MustacheException e) {
            e.setContext(this.tc);
            throw e;
        } catch (Throwable th) {
            throw new MustacheException(th.getMessage(), th, this.tc);
        }
    }

    @Override // com.github.mustachejava.Code
    public Writer execute(Writer writer, List<Object> list) {
        return appendText(run(writer, list));
    }

    @Override // com.github.mustachejava.Code
    public void identity(Writer writer) {
        try {
            if (this.name != null) {
                tag(writer, this.type);
                if (getCodes() != null) {
                    runIdentity(writer);
                    tag(writer, "/");
                }
            }
            appendText(writer);
        } catch (IOException e) {
            throw new MustacheException("Failed to write", e, this.tc);
        }
    }

    protected void runIdentity(Writer writer) {
        int length = getCodes().length;
        for (int i = 0; i < length; i++) {
            getCodes()[i].identity(writer);
        }
    }

    protected void tag(Writer writer, String str) throws IOException {
        writer.write(this.tc.startChars());
        writer.write(str);
        writer.write(this.name);
        writer.write(this.tc.endChars());
    }

    protected Writer appendText(Writer writer) {
        String str = this.appended;
        if (str != null) {
            try {
                writer.write(str);
            } catch (IOException e) {
                throw new MustacheException("Failed to write", e, this.tc);
            }
        }
        return writer;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public Writer run(Writer writer, List<Object> list) {
        Mustache mustache = this.mustache;
        return mustache == null ? writer : mustache.run(writer, list);
    }

    @Override // com.github.mustachejava.Code
    public void append(String str) {
        if (this.appended == null) {
            this.appended = str;
        } else {
            this.appended += str;
        }
    }

    protected boolean addScope(List<Object> list, Object obj) {
        if (obj == null) {
            return false;
        }
        list.add(obj);
        return true;
    }

    @Override // com.github.mustachejava.Code
    public String getName() {
        return this.name;
    }
}
