import React from "react";
import { storeHistory, getHistory } from "../../utils/history.util";
import { generateOutputNode } from "../../utils/output.utils";
const CommandInput = ({ prefix, prompt, inputRef, cursorClassName, handleCommand, isFocused, focusInput, blurInput, displayOutput, }) => {
    const [input, setInput] = React.useState("");
    const [history, setHistory] = React.useState([]);
    const [historyIndex, setHistoryIndex] = React.useState(0);
    const [cursorPosition, setCursorPosition] = React.useState(0);
    const handleInputChanged = (event) => {
        setInput((prev) => {
            if (cursorPosition >= prev.length) {
                setCursorPosition(event.target.value.length);
            }
            else {
                if (prev.length > event.target.value.length) {
                    setCursorPosition(cursorPosition - 1);
                }
                else {
                    setCursorPosition(cursorPosition + 1);
                }
            }
            return event.target.value;
        });
    };
    const changeCursorPos = (pos) => {
        if (inputRef.current) {
            inputRef.current.focus();
            inputRef.current.setSelectionRange(pos, pos);
            setCursorPosition(pos);
        }
    };
    const changeInput = (text) => {
        setInput(text);
        changeCursorPos(text.length);
    };
    const keyDownEvents = {
        Enter: (event) => {
            if (input.length > 0) {
                setHistory([...history, input]);
                storeHistory([...history, input]);
                setHistoryIndex(history.length + 1);
                displayOutput(generateOutputNode({
                    prefix,
                    prompt,
                    text: `${input}`,
                }));
                changeInput("");
                handleCommand(input);
            }
        },
        ArrowUp: (event) => {
            if (historyIndex > 0) {
                setHistoryIndex(historyIndex - 1);
                changeInput(history[historyIndex - 1]);
            }
        },
        ArrowDown: (event) => {
            if (historyIndex < history.length) {
                setHistoryIndex(historyIndex + 1);
                changeInput(history[historyIndex + 1] || "");
            }
        },
        ArrowLeft: (event) => {
            if (cursorPosition > 0) {
                changeCursorPos(cursorPosition - 1);
            }
        },
        ArrowRight: (event) => {
            if (cursorPosition < input.length) {
                changeCursorPos(cursorPosition + 1);
            }
        },
        Tab: (event) => {
            event.preventDefault();
        },
        Escape: (event) => {
            if (input.length > 0) {
                setInput("");
            }
        },
    };
    const handleKeyDown = (event) => {
        const keyEvent = keyDownEvents[event.key];
        if (keyEvent) {
            keyEvent(event);
        }
    };
    React.useEffect(() => {
        if (isFocused) {
            if (inputRef.current) {
                focusInput();
            }
            else {
                blurInput();
            }
        }
    }, [isFocused]);
    React.useEffect(() => {
        const existingHistory = getHistory();
        if (existingHistory) {
            setHistory(existingHistory);
            setHistoryIndex(existingHistory.length);
        }
    }, []);
    return (<>
      <div className="react-terminal__input">
        <div className="react-terminal__input-prefix">{prefix}</div>
        <div className="react-terminal__input-prompt">{prompt}</div>
        <div className="react-terminal__input-content">
          <p>{input}</p>
          {isFocused && (<span className={`react-terminal__terminal-cursor blink-fast ${cursorClassName || "cursor__box"}`} style={{
                left: `${cursorPosition}ch`,
            }}/>)}
        </div>
      </div>
      <div className="react-terminal__input-wrapper">
        <input ref={inputRef} type="text" value={input} onChange={handleInputChanged} onKeyDown={handleKeyDown} onFocus={focusInput} onBlur={blurInput}/>
      </div>
    </>);
};
export default CommandInput;
