import React from "react";
import { generateOutputNode } from "../../utils/output.utils";
const BooleanInput = ({ prefix, prompt, inputRef, cursorClassName, isFocused, focusInput, blurInput, handleEnter, handleError, options, displayOutput, }) => {
    const [choice, setChoice] = React.useState(options?.default || false);
    const handleInputChanged = (event) => {
        setChoice(event.target.checked);
    };
    const keyDownEvents = {
        Enter: (event) => {
            const choiceText = choice
                ? options?.true || "Yes"
                : options?.false || "No";
            displayOutput(generateOutputNode({
                prefix,
                prompt,
                text: choiceText,
            }));
            handleEnter(choice);
        },
        ArrowLeft: (event) => {
            if (!choice) {
                setChoice(true);
            }
        },
        ArrowRight: (event) => {
            if (choice) {
                setChoice(false);
            }
        },
        Tab: (event) => {
            event.preventDefault();
        },
        Escape: (event) => {
            event.preventDefault();
        },
    };
    const handleKeyDown = (event) => {
        const keyEvent = keyDownEvents[event.key];
        if (keyEvent) {
            keyEvent(event);
        }
    };
    React.useEffect(() => {
        if (isFocused) {
            if (inputRef.current) {
                focusInput();
                setChoice(inputRef.current.checked);
            }
            else {
                blurInput();
            }
        }
    }, [isFocused]);
    return (<>
      <div className="react-terminal__input input__consent">
        <div className="react-terminal__input-prefix">
          {prefix} {prompt}
        </div>
        <div className={`react-terminal__input-content ${isFocused ? "input__consent-focused" : ""}`}>
          <div className={`input__consent-choices consent-choices-yes ${choice ? "input__consent-choices-chosen" : ""}`}>
            <span>{options?.true || "Yes"}</span>
          </div>
          <div className="input__consent-choices-separator">/</div>
          <div className={`input__consent-choices consent-choices-no ${!choice ? "input__consent-choices-chosen" : ""}`}>
            <span>{options?.false || "No"}</span>
          </div>
        </div>
      </div>
      <div className="react-terminal__input-wrapper">
        <input ref={inputRef} type="checkbox" checked={choice} onChange={handleInputChanged} onKeyDown={handleKeyDown} onFocus={focusInput} onBlur={blurInput}/>
      </div>
    </>);
};
export default BooleanInput;
