import * as tslib_1 from "tslib";
import * as React from 'react';
import { useState, useEffect } from 'react';
import classNames from 'classnames';
import CollapsePanel from './collapse-panel';
var defaultProps = {
    prefixCls: 'ty-collapse',
    showArrow: true,
    bordered: true,
    deletable: false,
    accordion: false,
    defaultActiveKey: [],
    onChange: function () {
    },
};
var toArray = function (activeKey) {
    return Array.isArray(activeKey) ? activeKey : [activeKey];
};
var Collapse = function (props) {
    var _a;
    var defaultActiveKey = props.defaultActiveKey, activeKey = props.activeKey, accordion = props.accordion, bordered = props.bordered, onChange = props.onChange, deletable = props.deletable, showArrow = props.showArrow, prefixCls = props.prefixCls, className = props.className, style = props.style, children = props.children;
    var currentActiveKey = defaultActiveKey;
    if (activeKey) {
        currentActiveKey = activeKey;
    }
    var _b = useState(toArray(currentActiveKey)), activeItems = _b[0], setActiveItems = _b[1];
    var cls = classNames(prefixCls, className, (_a = {}, _a[prefixCls + "_borderless"] = !bordered, _a));
    var _itemClickCallback = function (itemKey) {
        var items = activeItems;
        if (accordion) {
            items = items[0] === itemKey ? [] : [itemKey];
        }
        else {
            items = activeItems.slice();
            var index = items.indexOf(itemKey);
            var isActive = index > -1;
            if (isActive) {
                items.splice(index, 1);
            }
            else {
                items.push(itemKey);
            }
        }
        _updateActiveItems(items);
    };
    var _updateActiveItems = function (items) {
        if (!('activeKey' in props)) {
            setActiveItems(items);
        }
        onChange(items);
    };
    useEffect(function () {
        activeKey && setActiveItems(toArray(activeKey));
    });
    return (React.createElement("div", { className: cls, style: style }, React.Children.map(children, function (child) {
        var itemProps = tslib_1.__assign({}, child.props, { deletable: deletable,
            showArrow: showArrow, isActive: activeItems.includes(child.props.itemKey), onItemClick: _itemClickCallback });
        return React.cloneElement(child, itemProps);
    })));
};
Collapse.defaultProps = defaultProps;
Collapse.Panel = CollapsePanel;
export default Collapse;
//# sourceMappingURL=index.js.map