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

package org.jline.reader;

import java.util.Iterator;
import org.jline.reader.impl.history.DefaultHistory;
import org.jline.reader.impl.LineReaderImpl;
import java.io.IOException;
import java.io.IOError;
import org.jline.terminal.TerminalBuilder;
import org.jline.utils.Log;
import java.util.Objects;
import java.util.HashMap;
import java.util.Map;
import org.jline.terminal.Terminal;

public final class LineReaderBuilder
{
    Terminal terminal;
    String appName;
    Map<String, Object> variables;
    Map<LineReader.Option, Boolean> options;
    History history;
    Completer completer;
    History memoryHistory;
    Highlighter highlighter;
    Parser parser;
    Expander expander;
    CompletionMatcher completionMatcher;
    
    public static LineReaderBuilder builder() {
        return new LineReaderBuilder();
    }
    
    private LineReaderBuilder() {
        this.variables = new HashMap<String, Object>();
        this.options = new HashMap<LineReader.Option, Boolean>();
    }
    
    public LineReaderBuilder terminal(final Terminal terminal) {
        this.terminal = terminal;
        return this;
    }
    
    public LineReaderBuilder appName(final String appName) {
        this.appName = appName;
        return this;
    }
    
    public LineReaderBuilder variables(final Map<String, Object> variables) {
        final Map<String, Object> old = this.variables;
        (this.variables = Objects.requireNonNull(variables)).putAll(old);
        return this;
    }
    
    public LineReaderBuilder variable(final String name, final Object value) {
        this.variables.put(name, value);
        return this;
    }
    
    public LineReaderBuilder option(final LineReader.Option option, final boolean value) {
        this.options.put(option, value);
        return this;
    }
    
    public LineReaderBuilder history(final History history) {
        this.history = history;
        return this;
    }
    
    public LineReaderBuilder completer(final Completer completer) {
        this.completer = completer;
        return this;
    }
    
    public LineReaderBuilder highlighter(final Highlighter highlighter) {
        this.highlighter = highlighter;
        return this;
    }
    
    public LineReaderBuilder parser(final Parser parser) {
        if (parser != null) {
            try {
                if (!Boolean.getBoolean("org.jline.reader.support.parsedline") && !(parser.parse("", 0) instanceof CompletingParsedLine)) {
                    Log.warn("The Parser of class " + parser.getClass().getName() + " does not support the CompletingParsedLine interface. Completion with escaped or quoted words won't work correctly.");
                }
            }
            catch (final Throwable t) {}
        }
        this.parser = parser;
        return this;
    }
    
    public LineReaderBuilder expander(final Expander expander) {
        this.expander = expander;
        return this;
    }
    
    public LineReaderBuilder completionMatcher(final CompletionMatcher completionMatcher) {
        this.completionMatcher = completionMatcher;
        return this;
    }
    
    public LineReader build() {
        Terminal terminal = this.terminal;
        if (terminal == null) {
            try {
                terminal = TerminalBuilder.terminal();
            }
            catch (final IOException e) {
                throw new IOError(e);
            }
        }
        String appName = this.appName;
        if (null == appName) {
            appName = terminal.getName();
        }
        final LineReaderImpl reader = new LineReaderImpl(terminal, appName, this.variables);
        if (this.history != null) {
            reader.setHistory(this.history);
        }
        else {
            if (this.memoryHistory == null) {
                this.memoryHistory = new DefaultHistory();
            }
            reader.setHistory(this.memoryHistory);
        }
        if (this.completer != null) {
            reader.setCompleter(this.completer);
        }
        if (this.highlighter != null) {
            reader.setHighlighter(this.highlighter);
        }
        if (this.parser != null) {
            reader.setParser(this.parser);
        }
        if (this.expander != null) {
            reader.setExpander(this.expander);
        }
        if (this.completionMatcher != null) {
            reader.setCompletionMatcher(this.completionMatcher);
        }
        for (final Map.Entry<LineReader.Option, Boolean> e2 : this.options.entrySet()) {
            reader.option(e2.getKey(), e2.getValue());
        }
        return reader;
    }
}
