package net.minecraft.network.chat;

import java.util.Optional;
import java.util.function.Supplier;
import java.util.function.Function;

public class KeybindComponent extends BaseComponent {
    private static Function<String, Supplier<Component>> keyResolver;
    private final String name;
    private Supplier<Component> nameResolver;
    
    public KeybindComponent(final String string) {
        this.name = string;
    }
    
    public static void setKeyResolver(final Function<String, Supplier<Component>> function) {
        KeybindComponent.keyResolver = function;
    }
    
    private Component getNestedComponent() {
        if (this.nameResolver == null) {
            this.nameResolver = KeybindComponent.keyResolver.apply(this.name);
        }
        return this.nameResolver.get();
    }
    
    @Override
    public <T> Optional<T> visitSelf(final FormattedText.ContentConsumer<T> a) {
        return this.getNestedComponent().<T>visit(a);
    }
    
    @Override
    public <T> Optional<T> visitSelf(final FormattedText.StyledContentConsumer<T> b, final Style nb) {
        return this.getNestedComponent().<T>visit(b, nb);
    }
    
    @Override
    public KeybindComponent plainCopy() {
        return new KeybindComponent(this.name);
    }
    
    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }
        if (object instanceof KeybindComponent) {
            final KeybindComponent mw3 = (KeybindComponent)object;
            return this.name.equals(mw3.name) && super.equals(object);
        }
        return false;
    }
    
    @Override
    public String toString() {
        return "KeybindComponent{keybind='" + this.name + '\'' + ", siblings=" + this.siblings + ", style=" + this.getStyle() + '}';
    }
    
    public String getName() {
        return this.name;
    }
    
    static {
        KeybindComponent.keyResolver = (string -> () -> new TextComponent(string));
    }
}
