"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "AccordionItem", {
    enumerable: true,
    get: function() {
        return AccordionItem;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _roundness = require("../../../../utils/roundness");
const _Accordiontokens = require("../../Accordion.tokens");
const _utils = require("../../../../utils");
const _AccordionItemstyles = require("./AccordionItem.styles");
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 AccordionItem = /*#__PURE__*/ (0, _react.forwardRef)(({ value, contentRight, contentLeft, title, pin = 'square-square', children, type = 'sign', index, className, style, eventKey, disabled, alignWithTitle = true, opened, view, onChange, onClick, titleProps, bodyProps, ...rest }, outerRef)=>{
    const key = eventKey ?? index ?? 0;
    const [leftPadding, setLeftPadding] = (0, _react.useState)();
    const handleOpen = ()=>{
        if (disabled) {
            return;
        }
        if (onChange) {
            onChange(key, !value);
        }
        if (onClick) {
            onClick(key, !value);
        }
    };
    const leftContentRef = (0, _react.useRef)(null);
    (0, _react.useEffect)(()=>{
        const leftContentWidth = leftContentRef?.current?.offsetWidth ?? 0;
        const leftPaddingBody = leftContentWidth && (alignWithTitle || view === 'clear') ? `calc(${leftContentWidth}px + var(${_Accordiontokens.tokens.accordionItemGap}))` : 0;
        setLeftPadding(leftPaddingBody);
    }, [
        value,
        type,
        leftContentRef,
        setLeftPadding
    ]);
    const openedBodyClass = opened ?? value ? _Accordiontokens.classes.accordionItemShowBody : undefined;
    const StyledAnimationPlus = ()=>/*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledPlus, null, /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledMinus, {
            size: "xs",
            color: "inherit",
            sizeCustomProperty: _Accordiontokens.tokens.accordionItemIconSize
        }), /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledMinus, {
            size: "xs",
            color: "inherit",
            className: openedBodyClass ?? _Accordiontokens.classes.accordionPlusAnimationElement,
            sizeCustomProperty: _Accordiontokens.tokens.accordionItemIconSize
        }));
    const accordionBorderRadius = (0, _roundness.convertRoundnessMatrix)(pin, `var(${_Accordiontokens.tokens.accordionItemViewBorderRadius}, var(${_Accordiontokens.tokens.accordionItemBorderRadius}))`, '1.5rem');
    const openedClass = opened ?? value ? _Accordiontokens.classes.accordionItemOpened : '';
    const disabledClass = disabled ? _Accordiontokens.classes.accordionDisabled : '';
    const leftContent = contentLeft ?? (type === 'arrow' ? /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledArrow, {
        size: "xs",
        color: "inherit",
        sizeCustomProperty: _Accordiontokens.tokens.accordionItemIconSize
    }) : undefined);
    const leftContentRotate = type === 'arrow' && (opened ?? value) ? _Accordiontokens.classes.accordionItemShowBody : undefined;
    const rightContent = contentRight ?? (type === 'sign' ? /*#__PURE__*/ _react.default.createElement(StyledAnimationPlus, null) : undefined);
    const rightContentRotate = type === 'sign' && (opened ?? value) ? _Accordiontokens.classes.accordionItemShowBody : undefined;
    return /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledAccordionItem, {
        className: (0, _utils.cx)(_Accordiontokens.classes.accordionItem, className, openedClass, disabledClass),
        key: key,
        ref: outerRef,
        style: {
            borderRadius: accordionBorderRadius,
            ...style
        },
        ...rest
    }, /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledAccordionHeader, {
        role: "tab",
        onKeyDown: (e)=>{
            if (e.key === 'Enter') {
                handleOpen();
            }
        },
        tabIndex: 0,
        onClick: handleOpen,
        "aria-expanded": opened ?? value,
        "aria-controls": `accordion-item-section${key}`,
        id: `accordion-item-${key}`,
        style: {
            [_Accordiontokens.privateTokens.calculatedBorderRadius]: accordionBorderRadius
        }
    }, /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledAccordionHeaderLeft, null, leftContent && /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledAccordionContentLeft, {
        ref: leftContentRef,
        className: leftContentRotate
    }, leftContent), /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledAccordionTitle, titleProps, title)), contentRight || rightContent && /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledAccordionContentRight, {
        className: rightContentRotate
    }, rightContent && rightContent)), /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledAccordionBodyAnimate, {
        "aria-labelledby": `accordion-item-${key}`,
        "aria-hidden": !(opened ?? value),
        id: `accordion-item-section${key}`,
        className: (0, _utils.cx)(openedBodyClass),
        style: {
            paddingLeft: `${leftPadding}`
        },
        ...bodyProps
    }, /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledAccordionBody, {
        className: _Accordiontokens.classes.accordionItemBody
    }, children)));
});
