// 
// Decompiled by Procyon v0.6.0
// 

package org.jline.style;

import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStyle;
import org.jline.utils.AttributedStringBuilder;
import java.util.Objects;

public class StyleExpression
{
    private final StyleResolver resolver;
    
    public StyleExpression() {
        this(new StyleResolver(new NopStyleSource(), ""));
    }
    
    public StyleExpression(final StyleResolver resolver) {
        this.resolver = Objects.requireNonNull(resolver);
    }
    
    public void evaluate(final AttributedStringBuilder buff, final String expression) {
        Objects.requireNonNull(buff);
        Objects.requireNonNull(expression);
        final String translated = InterpolationHelper.substVars(expression, this::style, false);
        buff.appendAnsi(translated);
    }
    
    private String style(final String key) {
        final int idx = key.indexOf(32);
        if (idx > 0) {
            final String spec = key.substring(0, idx);
            final String value = key.substring(idx + 1);
            final AttributedStyle style = this.resolver.resolve(spec);
            return new AttributedStringBuilder().style(style).ansiAppend(value).toAnsi();
        }
        return null;
    }
    
    public AttributedString evaluate(final String expression) {
        final AttributedStringBuilder buff = new AttributedStringBuilder();
        this.evaluate(buff, expression);
        return buff.toAttributedString();
    }
}
