"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get editableConfig () {
        return editableConfig;
    },
    get editableRoot () {
        return editableRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _hooks = require("../../hooks");
const _utils = require("../../utils");
const _utils1 = require("./utils");
const _Editabletokens = require("./Editable.tokens");
const _Editablestyles = require("./Editable.styles");
const _base = require("./variations/_view/base");
const _base1 = require("./variations/_size/base");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const editableRoot = (typographyVariants)=>(Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ textComponent, value, icon, maxLength, spellCheck = 'false', placeholder, name, onChange, onBlur, onPaste, ...props }, outerRef)=>{
            const Component = textComponent;
            const [isEditing, setIsEditing] = (0, _react.useState)(false);
            const inputRef = (0, _react.useRef)(null);
            const innerRef = (0, _hooks.useForkRef)(inputRef, outerRef);
            const prevValueRef = (0, _react.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);
                (0, _utils1.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 && (0, _utils1.selectText)(inputRef.current);
            };
            const handleEditClick = ()=>{
                inputRef.current?.focus();
            };
            const handleKeyDown = (e)=>{
                if ([
                    _utils.constants.keyCodes.Enter,
                    _utils.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;
                }
            };
            (0, _react.useEffect)(()=>{
                if (!inputRef.current || typeof value === 'undefined' || value === inputRef.current.textContent) {
                    return;
                }
                inputRef.current.textContent = value;
            }, [
                value,
                textComponent
            ]);
            return /*#__PURE__*/ _react.default.createElement(Root, props, /*#__PURE__*/ _react.default.createElement(Component, {
                ref: innerRef,
                className: _Editabletokens.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.default.createElement(Component, null, /*#__PURE__*/ _react.default.createElement(_Editablestyles.StyledPlacehpolder, {
                onClick: handleEditClick
            }, placeholder)), /*#__PURE__*/ _react.default.createElement(_Editablestyles.StyledInput, {
                type: "hidden",
                value: prevValueRef.current,
                name: name
            }), /*#__PURE__*/ _react.default.createElement(_Editablestyles.EditButton, {
                onClick: handleEditClick,
                isHidden: isEditing
            }, icon));
        });
const editableConfig = (typographyVariants)=>({
        name: 'Editable',
        tag: 'span',
        layout: editableRoot(typographyVariants),
        base: _Editablestyles.base,
        variations: {
            view: {
                css: _base.base
            },
            size: {
                css: _base1.base
            }
        },
        defaults: {
            view: 'default',
            size: 'm'
        }
    });
