"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 noteConfig () {
        return noteConfig;
    },
    get noteRoot () {
        return noteRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _utils = require("../../utils");
const _hooks = require("../../hooks");
const _Icon = require("../_Icon");
const _base = require("./variations/_view/base");
const _base1 = require("./variations/_size/base");
const _base2 = require("./variations/_orientation/base");
const _Notestyles = require("./Note.styles");
const _Notetokens = require("./Note.tokens");
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 noteRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ className, style, title, text, contentBefore, contentBeforeSizing = 'fixed', size, view, stretch, width, height, hasClose, onCloseButtonClick, actionContent, orientation = 'vertical', ...rest }, outerRef)=>{
        const [innerText, setInnerText] = (0, _react.useState)(text);
        const [contentBeforeWidth, setContentBeforeWidth] = (0, _react.useState)(contentBefore ? `var(${_Notetokens.tokens.fixedContentBeforeWidth})` : '0');
        const contentWrapperRef = (0, _react.useRef)(null);
        const textBoxRef = (0, _react.useRef)(null);
        const contentBeforeRef = (0, _react.useRef)(null);
        const titleHelperRef = (0, _react.useRef)(null);
        const textRenderHelperRef = (0, _react.useRef)(null);
        const innerWidth = width ? (0, _utils.getSizeValueFromProp)(width) : 'fit-content';
        const innerHeight = height ? (0, _utils.getSizeValueFromProp)(height) : 'fit-content';
        const contentGapToken = contentBeforeSizing === 'scalable' ? _Notetokens.tokens.gapScalable : _Notetokens.tokens.gap;
        const contentBeforeOffsetToken = contentBefore ? `calc(${contentBeforeWidth} + var(${contentGapToken}))` : '0';
        const setTruncatedText = ()=>{
            if (!text || typeof text !== 'string' || !height) {
                setInnerText(text);
                return;
            }
            if (!_utils.canUseDOM || !contentWrapperRef?.current || !textBoxRef?.current || !textRenderHelperRef?.current) {
                return;
            }
            const rootEl = contentWrapperRef.current.parentElement;
            if (!rootEl) {
                return;
            }
            const rootStyle = window.getComputedStyle(rootEl);
            const paddingTop = parseFloat(rootStyle.paddingTop) || 0;
            const paddingBottom = parseFloat(rootStyle.paddingBottom) || 0;
            const contentHeight = rootEl.clientHeight - paddingTop - paddingBottom;
            const titleHeight = titleHelperRef.current?.offsetHeight || 0;
            const contentGap = titleHeight ? Number(window.getComputedStyle(textBoxRef.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);
        };
        const setTruncatedTextRef = (0, _react.useRef)(setTruncatedText);
        setTruncatedTextRef.current = setTruncatedText;
        (0, _hooks.useResizeObserver)(contentWrapperRef, (0, _react.useCallback)(()=>setTruncatedTextRef.current(), []));
        (0, _react.useLayoutEffect)(()=>{
            setTruncatedText();
        }, [
            text,
            title,
            contentBefore,
            contentBeforeSizing,
            stretch,
            orientation
        ]);
        (0, _react.useEffect)(()=>{
            if (!contentBeforeRef?.current) {
                return;
            }
            if (contentBeforeSizing === 'scalable') {
                setContentBeforeWidth(`${contentBeforeRef.current.offsetWidth}px`);
                return;
            }
            setContentBeforeWidth(`var(${_Notetokens.tokens.fixedContentBeforeWidth})`);
        }, [
            contentBefore,
            contentBeforeSizing
        ]);
        const isHorizontal = orientation === 'horizontal';
        return /*#__PURE__*/ _react.default.createElement(Root, {
            ref: outerRef,
            className: (0, _utils.cx)(className, stretch && _Notetokens.classes.stretch, contentBeforeSizing === 'scalable' && _Notetokens.classes.contentAlignCenter, isHorizontal && _Notetokens.classes.orientationHorizontal, Boolean(actionContent) && _Notetokens.classes.hasActionContent, hasClose && _Notetokens.classes.hasClose),
            view: view,
            size: size,
            orientation: orientation,
            style: {
                ...style,
                [_Notetokens.privateTokens.width]: innerWidth,
                [_Notetokens.privateTokens.height]: innerHeight,
                [_Notetokens.privateTokens.contentBeforeOffset]: contentBeforeOffsetToken
            },
            ...rest
        }, /*#__PURE__*/ _react.default.createElement(_Notestyles.ContentWrapper, {
            ref: contentWrapperRef
        }, contentBefore && /*#__PURE__*/ _react.default.createElement(_Notestyles.ContentBefore, {
            ref: contentBeforeRef
        }, contentBefore), /*#__PURE__*/ _react.default.createElement(_Notestyles.TextBox, {
            ref: textBoxRef
        }, title && /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement(_Notestyles.Title, {
            hasClose: !isHorizontal && hasClose
        }, title), /*#__PURE__*/ _react.default.createElement(_Notestyles.TitleHelper, {
            ref: titleHelperRef
        }, "C")), innerText && /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement(_Notestyles.Text, null, innerText), /*#__PURE__*/ _react.default.createElement(_Notestyles.TextHelper, {
            ref: textRenderHelperRef
        }, "C")))), actionContent && /*#__PURE__*/ _react.default.createElement(_Notestyles.ActionContentContainer, null, actionContent), hasClose && /*#__PURE__*/ _react.default.createElement(_Notestyles.CloseIconWrapper, {
            view: "clear",
            size: "s",
            onClick: onCloseButtonClick,
            className: _Notetokens.classes.closeIcon
        }, /*#__PURE__*/ _react.default.createElement(_Icon.IconCrossThin, {
            size: "s",
            sizeCustomProperty: _Notetokens.tokens.closeIconSize,
            color: "inherit"
        })));
    });
const noteConfig = {
    name: 'Note',
    tag: 'div',
    layout: noteRoot,
    base: _Notestyles.base,
    variations: {
        view: {
            css: _base.base
        },
        size: {
            css: _base1.base
        },
        orientation: {
            css: _base2.base
        }
    },
    defaults: {
        view: 'default',
        size: 'm',
        orientation: 'vertical'
    }
};
