import React, { forwardRef, useEffect, useLayoutEffect, useRef, useState } from "react";
import { canUseDOM, cx, getSizeValueFromProp } from "../../utils";
import { useResizeObserver } from "../../hooks";
import { IconCrossThin } from "../_Icon";
import { base as viewCSS } from "./variations/_view/base";
import { base as sizeCSS } from "./variations/_size/base";
import { base, CloseIconWrapper, ContentBefore, ContentWrapper, Text, TextHelper, Title, TitleHelper } from "./Note.styles";
import { classes, privateTokens, tokens } from "./Note.tokens";
export const noteRoot = (Root)=>/*#__PURE__*/ forwardRef(({ className, style, title, text, contentBefore, contentBeforeSizing = 'fixed', size, view, stretch, width, height, hasClose, onCloseButtonClick, ...rest }, outerRef)=>{
        const [innerText, setInnerText] = useState(text);
        const [contentBeforeWidth, setContentBeforeWidth] = useState(contentBefore ? `var(${tokens.fixedContentBeforeWidth})` : '0');
        const contentWrapperRef = useRef(null);
        const contentBeforeRef = useRef(null);
        const titleHelperRef = useRef(null);
        const textRenderHelperRef = useRef(null);
        const innerWidth = width ? getSizeValueFromProp(width) : 'fit-content';
        const innerHeight = height ? getSizeValueFromProp(height) : 'fit-content';
        const contentGapToken = contentBeforeSizing === 'scalable' ? tokens.gapScalable : tokens.gap;
        const contentWidthWithOffsetToken = contentBefore ? `calc(100% - ${contentBeforeWidth} - var(${contentGapToken}))` : '100%';
        const setTruncatedText = ()=>{
            if (!canUseDOM || !text || typeof text !== 'string' || !contentWrapperRef?.current || !textRenderHelperRef?.current || !width && !height) {
                return;
            }
            const contentHeight = contentWrapperRef.current.offsetHeight;
            const titleHeight = titleHelperRef.current?.offsetHeight || 0;
            const contentGap = titleHeight ? Number(window.getComputedStyle(contentWrapperRef.current).rowGap.replace('px', '')) || 0 : 0;
            const textAvailableHeight = contentHeight - titleHeight - contentGap;
            textRenderHelperRef.current.textContent = text;
            if (textRenderHelperRef.current.offsetHeight <= textAvailableHeight) {
                setInnerText(text);
                return;
            }
            let fullText = `${text.slice(0, -3)}...`;
            for(let i = text.length - 1; i >= 0; i -= 1){
                textRenderHelperRef.current.textContent = fullText;
                if (textRenderHelperRef.current.offsetHeight <= textAvailableHeight) {
                    break;
                }
                fullText = `${fullText.slice(0, i)}...`;
            }
            setInnerText(fullText);
        };
        useResizeObserver(contentWrapperRef, setTruncatedText);
        useLayoutEffect(()=>{
            setTruncatedText();
        }, [
            text,
            title,
            contentBefore,
            contentBeforeSizing,
            stretch
        ]);
        useEffect(()=>{
            if (!contentBeforeRef?.current) {
                return;
            }
            if (contentBeforeSizing === 'scalable') {
                setContentBeforeWidth(`${contentBeforeRef.current.offsetWidth}px`);
                return;
            }
            setContentBeforeWidth(`var(${tokens.fixedContentBeforeWidth})`);
        }, [
            contentBefore,
            contentBeforeSizing
        ]);
        return /*#__PURE__*/ React.createElement(Root, {
            ref: outerRef,
            className: cx(className, stretch && classes.stretch, contentBeforeSizing === 'scalable' && classes.contentAlignCenter),
            view: view,
            size: size,
            style: {
                ...style,
                [privateTokens.width]: innerWidth,
                [privateTokens.height]: innerHeight,
                [privateTokens.contentWidthWithOffset]: contentWidthWithOffsetToken
            },
            ...rest
        }, contentBefore && /*#__PURE__*/ React.createElement(ContentBefore, {
            ref: contentBeforeRef
        }, contentBefore), /*#__PURE__*/ React.createElement(ContentWrapper, {
            ref: contentWrapperRef
        }, title && /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(Title, {
            hasClose: hasClose
        }, title), /*#__PURE__*/ React.createElement(TitleHelper, {
            ref: titleHelperRef
        }, "C")), innerText && /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(Text, null, innerText, /*#__PURE__*/ React.createElement(TextHelper, {
            ref: textRenderHelperRef
        }, "C")))), hasClose && /*#__PURE__*/ React.createElement(CloseIconWrapper, {
            view: "clear",
            size: "s",
            onClick: onCloseButtonClick,
            className: classes.closeIcon
        }, /*#__PURE__*/ React.createElement(IconCrossThin, {
            size: "s",
            sizeCustomProperty: tokens.closeIconSize,
            color: "inherit"
        })));
    });
export const noteConfig = {
    name: 'Note',
    tag: 'div',
    layout: noteRoot,
    base,
    variations: {
        view: {
            css: viewCSS
        },
        size: {
            css: sizeCSS
        }
    },
    defaults: {
        view: 'default',
        size: 'm'
    }
};
