import React, { forwardRef, useEffect, useRef, useState } from "react";
import { useForkRef } from "../../hooks";
import { constants } from "../../utils";
import { clearSelection, selectText } from "./utils";
import { classes } from "./Editable.tokens";
import { base, EditButton, StyledInput, StyledPlacehpolder } from "./Editable.styles";
import { base as viewCSS } from "./variations/_view/base";
import { base as sizeCSS } from "./variations/_size/base";
export const editableRoot = (typographyVariants)=>(Root)=>/*#__PURE__*/ forwardRef(({ textComponent, value, icon, maxLength, spellCheck = 'false', placeholder, name, onChange, onBlur, onPaste, ...props }, outerRef)=>{
            const Component = textComponent;
            const [isEditing, setIsEditing] = useState(false);
            const inputRef = useRef(null);
            const innerRef = useForkRef(inputRef, outerRef);
            const prevValueRef = useRef(value || '');
            const handlePaste = (e)=>{
                if (!inputRef.current) {
                    return;
                }
                e.preventDefault();
                const text = e.clipboardData.getData('text/plain').replace(/[\n\r]/gi, '');
                if (document.queryCommandSupported('insertText')) {
                    document.execCommand('insertText', false, text);
                } else {
                    try {
                        navigator.clipboard.writeText(text);
                    } catch  {
                        inputRef.current.textContent = text;
                    }
                }
                if (onPaste) {
                    onPaste(e);
                }
            };
            const handleBlur = (e)=>{
                setIsEditing(false);
                clearSelection();
                // NOTE: Remove <br> tag inserted by Safari
                const editableElement = inputRef.current;
                if (editableElement && editableElement.innerHTML === '<br>') {
                    editableElement.innerHTML = '';
                }
                if (onBlur) {
                    onBlur(e);
                }
            };
            const handleFocus = ()=>{
                setIsEditing(true);
                inputRef.current && selectText(inputRef.current);
            };
            const handleEditClick = ()=>{
                inputRef.current?.focus();
            };
            const handleKeyDown = (e)=>{
                if ([
                    constants.keyCodes.Enter,
                    constants.keyCodes.Escape
                ].includes(e.keyCode)) {
                    inputRef.current?.blur();
                }
            };
            const handleChange = (e)=>{
                if (!inputRef.current) {
                    return;
                }
                const contentLength = inputRef.current.textContent?.length || 0;
                if (!maxLength || contentLength <= maxLength) {
                    prevValueRef.current = inputRef.current.textContent || '';
                    onChange && onChange(e);
                } else {
                    inputRef.current.textContent = prevValueRef.current;
                }
            };
            useEffect(()=>{
                if (!inputRef.current || typeof value === 'undefined' || value === inputRef.current.textContent) {
                    return;
                }
                inputRef.current.textContent = value;
            }, [
                value,
                textComponent
            ]);
            return /*#__PURE__*/ React.createElement(Root, props, /*#__PURE__*/ React.createElement(Component, {
                ref: innerRef,
                className: classes.editableTextBox,
                role: "textbox",
                spellCheck: spellCheck,
                contentEditable: true,
                onPaste: handlePaste,
                onInput: handleChange,
                onBlur: handleBlur,
                onFocus: handleFocus,
                onKeyDown: handleKeyDown,
                "aria-label": placeholder
            }), prevValueRef.current?.length === 0 && !isEditing && /*#__PURE__*/ React.createElement(Component, null, /*#__PURE__*/ React.createElement(StyledPlacehpolder, {
                onClick: handleEditClick
            }, placeholder)), /*#__PURE__*/ React.createElement(StyledInput, {
                type: "hidden",
                value: prevValueRef.current,
                name: name
            }), /*#__PURE__*/ React.createElement(EditButton, {
                onClick: handleEditClick,
                isHidden: isEditing
            }, icon));
        });
export const editableConfig = (typographyVariants)=>({
        name: 'Editable',
        tag: 'span',
        layout: editableRoot(typographyVariants),
        base,
        variations: {
            view: {
                css: viewCSS
            },
            size: {
                css: sizeCSS
            }
        },
        defaults: {
            view: 'default',
            size: 'm'
        }
    });
