package net.minecraft.network.chat;

import java.util.Arrays;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.world.entity.Entity;
import net.minecraft.commands.CommandSourceStack;
import java.util.Iterator;
import java.util.Optional;
import java.util.regex.Matcher;
import com.google.common.collect.Lists;
import java.util.regex.Pattern;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.locale.Language;

public class TranslatableComponent extends BaseComponent implements ContextAwareComponent {
    private static final Object[] NO_ARGS;
    private static final FormattedText TEXT_PERCENT;
    private static final FormattedText TEXT_NULL;
    private final String key;
    private final Object[] args;
    @Nullable
    private Language decomposedWith;
    private final List<FormattedText> decomposedParts;
    private static final Pattern FORMAT_PATTERN;
    
    public TranslatableComponent(final String string) {
        this.decomposedParts = Lists.newArrayList();
        this.key = string;
        this.args = TranslatableComponent.NO_ARGS;
    }
    
    public TranslatableComponent(final String string, final Object... arr) {
        this.decomposedParts = Lists.newArrayList();
        this.key = string;
        this.args = arr;
    }
    
    private void decompose() {
        final Language kz2 = Language.getInstance();
        if (kz2 == this.decomposedWith) {
            return;
        }
        this.decomposedWith = kz2;
        this.decomposedParts.clear();
        final String string3 = kz2.getOrDefault(this.key);
        try {
            this.decomposeTemplate(kz2.reorder(string3, true), kz2);
        }
        catch (TranslatableFormatException nf4) {
            this.decomposedParts.clear();
            this.decomposedParts.add(FormattedText.of(string3));
        }
    }
    
    private void decomposeTemplate(final String string, final Language kz) {
        final Matcher matcher4 = TranslatableComponent.FORMAT_PATTERN.matcher(string);
        try {
            int integer5 = 0;
            int integer6;
            int integer8;
            for (integer6 = 0; matcher4.find(integer6); integer6 = integer8) {
                final int integer7 = matcher4.start();
                integer8 = matcher4.end();
                if (integer7 > integer6) {
                    final String string2 = string.substring(integer6, integer7);
                    if (string2.indexOf(37) != -1) {
                        throw new IllegalArgumentException();
                    }
                    this.decomposedParts.add(FormattedText.of(string2));
                }
                final String string2 = matcher4.group(2);
                final String string3 = string.substring(integer7, integer8);
                if ("%".equals(string2) && "%%".equals(string3)) {
                    this.decomposedParts.add(TranslatableComponent.TEXT_PERCENT);
                }
                else {
                    if (!"s".equals(string2)) {
                        throw new TranslatableFormatException(this, "Unsupported format: '" + string3 + "'");
                    }
                    final String string4 = matcher4.group(1);
                    final int integer9 = (string4 != null) ? (Integer.parseInt(string4) - 1) : integer5++;
                    if (integer9 < this.args.length) {
                        this.decomposedParts.add(this.getArgument(integer9, kz));
                    }
                }
            }
            if (integer6 < string.length()) {
                final String string5 = string.substring(integer6);
                if (string5.indexOf(37) != -1) {
                    throw new IllegalArgumentException();
                }
                this.decomposedParts.add(FormattedText.of(string5));
            }
        }
        catch (IllegalArgumentException illegalArgumentException5) {
            throw new TranslatableFormatException(this, illegalArgumentException5);
        }
    }
    
    private FormattedText getArgument(final int integer, final Language kz) {
        if (integer >= this.args.length) {
            throw new TranslatableFormatException(this, integer);
        }
        final Object object4 = this.args[integer];
        if (object4 instanceof Component) {
            return (Component)object4;
        }
        return (object4 == null) ? TranslatableComponent.TEXT_NULL : FormattedText.of(kz.reorder(object4.toString(), false));
    }
    
    @Override
    public TranslatableComponent plainCopy() {
        return new TranslatableComponent(this.key, this.args);
    }
    
    @Override
    public <T> Optional<T> visitSelf(final FormattedText.StyledContentConsumer<T> b, final Style nb) {
        this.decompose();
        for (final FormattedText mu5 : this.decomposedParts) {
            final Optional<T> optional6 = mu5.<T>visit(b, nb);
            if (optional6.isPresent()) {
                return optional6;
            }
        }
        return Optional.<T>empty();
    }
    
    @Override
    public <T> Optional<T> visitSelf(final FormattedText.ContentConsumer<T> a) {
        this.decompose();
        for (final FormattedText mu4 : this.decomposedParts) {
            final Optional<T> optional5 = mu4.<T>visit(a);
            if (optional5.isPresent()) {
                return optional5;
            }
        }
        return Optional.<T>empty();
    }
    
    @Override
    public MutableComponent resolve(@Nullable final CommandSourceStack cz, @Nullable final Entity aom, final int integer) throws CommandSyntaxException {
        final Object[] arr5 = new Object[this.args.length];
        for (int integer2 = 0; integer2 < arr5.length; ++integer2) {
            final Object object7 = this.args[integer2];
            if (object7 instanceof Component) {
                arr5[integer2] = ComponentUtils.updateForEntity(cz, (Component)object7, aom, integer);
            }
            else {
                arr5[integer2] = object7;
            }
        }
        return new TranslatableComponent(this.key, arr5);
    }
    
    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }
        if (object instanceof TranslatableComponent) {
            final TranslatableComponent ne3 = (TranslatableComponent)object;
            return Arrays.equals(this.args, ne3.args) && this.key.equals(ne3.key) && super.equals(object);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int integer2 = super.hashCode();
        integer2 = 31 * integer2 + this.key.hashCode();
        integer2 = 31 * integer2 + Arrays.hashCode(this.args);
        return integer2;
    }
    
    @Override
    public String toString() {
        return "TranslatableComponent{key='" + this.key + '\'' + ", args=" + Arrays.toString(this.args) + ", siblings=" + this.siblings + ", style=" + this.getStyle() + '}';
    }
    
    public String getKey() {
        return this.key;
    }
    
    public Object[] getArgs() {
        return this.args;
    }
    
    static {
        NO_ARGS = new Object[0];
        TEXT_PERCENT = FormattedText.of("%");
        TEXT_NULL = FormattedText.of("null");
        FORMAT_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([A-Za-z%]|$)");
    }
}
