"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "AccordionItem", {
    enumerable: true,
    get: function() {
        return AccordionItem;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _roundness = require("../../../../utils/roundness");
var _Accordiontokens = require("../../Accordion.tokens");
var _utils = require("../../../../utils");
var _AccordionItemstyles = require("./AccordionItem.styles");
function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
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;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
var AccordionItem = /*#__PURE__*/ (0, _react.forwardRef)(function(param, outerRef) {
    var value = param.value, contentRight = param.contentRight, contentLeft = param.contentLeft, title = param.title, _param_pin = param.pin, pin = _param_pin === void 0 ? 'square-square' : _param_pin, children = param.children, _param_type = param.type, type = _param_type === void 0 ? 'sign' : _param_type, index = param.index, className = param.className, style = param.style, eventKey = param.eventKey, disabled = param.disabled, _param_alignWithTitle = param.alignWithTitle, alignWithTitle = _param_alignWithTitle === void 0 ? true : _param_alignWithTitle, opened = param.opened, view = param.view, onChange = param.onChange, onClick = param.onClick;
    var _ref;
    var key = (_ref = eventKey !== null && eventKey !== void 0 ? eventKey : index) !== null && _ref !== void 0 ? _ref : 0;
    var _useState = _sliced_to_array((0, _react.useState)(), 2), leftPadding = _useState[0], setLeftPadding = _useState[1];
    var handleOpen = function handleOpen() {
        if (disabled) {
            return;
        }
        if (onChange) {
            onChange(key, !value);
        }
        if (onClick) {
            onClick(key, !value);
        }
    };
    var leftContentRef = (0, _react.useRef)(null);
    (0, _react.useEffect)(function() {
        var _ref;
        var _leftContentRef_current;
        var leftContentWidth = (_ref = leftContentRef === null || leftContentRef === void 0 ? void 0 : (_leftContentRef_current = leftContentRef.current) === null || _leftContentRef_current === void 0 ? void 0 : _leftContentRef_current.offsetWidth) !== null && _ref !== void 0 ? _ref : 0;
        var leftPaddingBody = leftContentWidth && (alignWithTitle || view === 'clear') ? "calc(".concat(leftContentWidth, "px + var(").concat(_Accordiontokens.tokens.accordionItemGap, "))") : 0;
        setLeftPadding(leftPaddingBody);
    }, [
        value,
        type,
        leftContentRef,
        setLeftPadding
    ]);
    var openedBodyClass = (opened !== null && opened !== void 0 ? opened : value) ? _Accordiontokens.classes.accordionItemShowBody : undefined;
    var StyledAnimationPlus = function StyledAnimationPlus() {
        return /*#__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 !== null && openedBodyClass !== void 0 ? openedBodyClass : _Accordiontokens.classes.accordionPlusAnimationElement,
            sizeCustomProperty: _Accordiontokens.tokens.accordionItemIconSize
        }));
    };
    var accordionBorderRadius = (0, _roundness.convertRoundnessMatrix)(pin, "var(".concat(_Accordiontokens.tokens.accordionItemBorderRadius, ")"), '1.5rem');
    var openedClass = (opened !== null && opened !== void 0 ? opened : value) ? _Accordiontokens.classes.accordionItemOpened : '';
    var disabledClass = disabled ? _Accordiontokens.classes.accordionDisabled : '';
    var leftContent = contentLeft !== null && contentLeft !== void 0 ? contentLeft : type === 'arrow' ? /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledArrow, {
        size: "xs",
        color: "inherit",
        sizeCustomProperty: _Accordiontokens.tokens.accordionItemIconSize
    }) : undefined;
    var leftContentRotate = type === 'arrow' && (opened !== null && opened !== void 0 ? opened : value) ? _Accordiontokens.classes.accordionItemShowBody : undefined;
    var rightContent = contentRight !== null && contentRight !== void 0 ? contentRight : type === 'sign' ? /*#__PURE__*/ _react.default.createElement(StyledAnimationPlus, null) : undefined;
    var rightContentRotate = type === 'sign' && (opened !== null && opened !== void 0 ? 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: _object_spread({
            borderRadius: accordionBorderRadius
        }, style)
    }, /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledAccordionHeader, {
        role: "tab",
        onKeyDown: function onKeyDown(e) {
            if (e.key === 'Enter') {
                handleOpen();
            }
        },
        tabIndex: 0,
        onClick: handleOpen,
        "aria-expanded": opened !== null && opened !== void 0 ? opened : value,
        "aria-controls": "accordion-item-section".concat(key),
        id: "accordion-item-".concat(key)
    }, /*#__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, null, title)), contentRight || rightContent && /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledAccordionContentRight, {
        className: rightContentRotate
    }, rightContent && rightContent)), /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledAccordionBodyAnimate, {
        "aria-labelledby": "accordion-item-".concat(key),
        "aria-hidden": !(opened !== null && opened !== void 0 ? opened : value),
        id: "accordion-item-section".concat(key),
        className: (0, _utils.cx)(openedBodyClass),
        style: {
            paddingLeft: "".concat(leftPadding)
        }
    }, /*#__PURE__*/ _react.default.createElement(_AccordionItemstyles.StyledAccordionBody, {
        className: _Accordiontokens.classes.accordionItemBody
    }, children)));
});
