"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HistoryInputBox = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const history_1 = require("@opensumi/monaco-editor-core/esm/vs/base/common/history");
const Input_1 = require("./Input");
class HistoryInputBox extends react_1.default.Component {
    constructor() {
        super(...arguments);
        this.inputRef = react_1.default.createRef();
        this.addToHistory = (v) => {
            if (this.history && v && v !== this.getCurrentValue()) {
                this.history.add(v);
            }
        };
        this.getHistory = () => (this.history && this.history.getHistory()) || [];
        this.showNextValue = () => {
            const value = this.getCurrentValue() || '';
            if (this.history && !this.history.has(value)) {
                this.addToHistory(value);
            }
            let next = this.getNextValue();
            if (next) {
                next = next === value ? this.getNextValue() : next;
            }
            if (next) {
                this.onValueChange(next);
            }
        };
        this.showPreviousValue = () => {
            const value = this.getCurrentValue() || '';
            if (this.history && !this.history.has(value)) {
                this.addToHistory(value);
            }
            let previous = this.getPreviousValue();
            if (previous) {
                previous = previous === value ? this.getPreviousValue() : previous;
            }
            if (previous) {
                this.onValueChange(previous);
            }
        };
        this.clearHistory = () => {
            this.history && this.history.clear();
        };
        this.getCurrentValue = () => {
            if (!this.history) {
                return null;
            }
            let currentValue = this.history.current();
            if (!currentValue) {
                currentValue = this.history.last();
                this.history.next();
            }
            return currentValue;
        };
        this.getPreviousValue = () => (this.history && (this.history.previous() || this.history.first())) || null;
        this.getNextValue = () => (this.history && (this.history.next() || this.history.last())) || null;
        this.onValueChange = (v) => {
            const { onValueChange } = this.props;
            this.setState({
                inputValue: v,
            });
            if (onValueChange) {
                onValueChange(v);
            }
        };
        this.onKeyDown = (e) => {
            const { onKeyDown } = this.props;
            if (onKeyDown) {
                onKeyDown(e);
            }
        };
        this.focus = () => {
            if (this.inputRef && this.inputRef.current) {
                this.inputRef.current.focus();
            }
        };
    }
    componentDidMount() {
        var _a;
        const { history, onReady } = this.props;
        this.history = new history_1.HistoryNavigator(history || [], 100);
        this.inputProps = { ...this.props };
        delete this.inputProps.onReady;
        this.setState({
            inputValue: ((_a = this.props.value) !== null && _a !== void 0 ? _a : this.props.defaultValue) || '',
        });
        if (typeof onReady === 'function') {
            onReady({
                addToHistory: this.addToHistory,
                getHistory: this.getHistory,
                clearHistory: this.clearHistory,
                getCurrentValue: this.getCurrentValue,
                showNextValue: this.showNextValue,
                showPreviousValue: this.showPreviousValue,
                focus: this.focus,
            });
        }
    }
    render() {
        const inputValue = this.state && this.state.inputValue;
        return (react_1.default.createElement(Input_1.Input, { ref: this.inputRef, ...this.inputProps, onValueChange: this.onValueChange, onKeyDown: this.onKeyDown, value: inputValue }));
    }
}
exports.HistoryInputBox = HistoryInputBox;
//# sourceMappingURL=HistoryInputBox.js.map