/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

import { __extends, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import { autobind, themeable } from 'amis-core';
import isEqual from 'lodash/isEqual';

/**
 * @file CollapseGroup
 * @description 折叠面板group
 * @author hongyang03
 */
var CollapseGroup = /** @class */ (function (_super) {
    __extends(CollapseGroup, _super);
    function CollapseGroup(props) {
        var _this = _super.call(this, props) || this;
        _this.getItems = function (children) {
            if (!Array.isArray(children)) {
                return children;
            }
            return children.map(function (child, index) {
                var props = child.props;
                var collapseId = props.propKey || String(index);
                // 判断是否折叠
                var collapsed = _this.state.activeKeys.indexOf(collapseId) === -1;
                return React__default.cloneElement(child, __assign(__assign({}, props), { key: collapseId, collapseId: collapseId, collapsed: collapsed, expandIcon: _this.props.expandIcon, propsUpdate: true, onCollapse: function (collapsed) {
                        return _this.collapseChange(collapseId, collapsed);
                    } }));
            });
        };
        // 传入的activeKey会被自动转换为defaultActiveKey
        _this.updateActiveKey(props.defaultActiveKey, true);
        return _this;
    }
    CollapseGroup.prototype.UNSAFE_componentWillReceiveProps = function (nextProps) {
        var props = this.props;
        if (!isEqual(props.defaultActiveKey, nextProps.defaultActiveKey)) {
            this.updateActiveKey(nextProps.defaultActiveKey);
        }
    };
    CollapseGroup.prototype.updateActiveKey = function (propsActiveKey, isInit) {
        var props = this.props;
        var curActiveKey = propsActiveKey;
        if (!Array.isArray(curActiveKey)) {
            curActiveKey = curActiveKey ? [curActiveKey] : [];
        }
        if (props.accordion) {
            // 手风琴模式下只展开第一个元素
            curActiveKey = curActiveKey.length ? [curActiveKey[0]] : [];
        }
        if (isInit) {
            this.state = {
                activeKeys: curActiveKey.map(function (key) { return String(key); })
            };
        }
        else {
            this.setState({
                activeKeys: curActiveKey.map(function (key) { return String(key); })
            });
        }
    };
    CollapseGroup.prototype.collapseChange = function (collapseId, collapsed) {
        var _a, _b;
        var activeKeys = this.state.activeKeys.concat();
        if (collapsed) {
            // 设置成折叠状态
            if (this.props.accordion) {
                activeKeys = [];
            }
            else {
                var activeKeyIndex = activeKeys.indexOf(collapseId);
                if (activeKeyIndex !== -1) {
                    activeKeys.splice(activeKeyIndex, 1); // 剔除开启状态
                }
            }
        }
        else {
            // 展开折叠器
            if (this.props.accordion) {
                activeKeys = [collapseId];
            }
            else if (activeKeys.indexOf(collapseId) === -1) {
                activeKeys.push(collapseId);
            }
        }
        (_b = (_a = this.props).onCollapseChange) === null || _b === void 0 ? void 0 : _b.call(_a, activeKeys, collapseId, activeKeys.indexOf(collapseId) === -1);
        this.setState({
            activeKeys: activeKeys
        });
    };
    CollapseGroup.prototype.render = function () {
        var _a = this.props, cx = _a.classnames, className = _a.className, style = _a.style, expandIconPosition = _a.expandIconPosition, children = _a.children, mobileUI = _a.mobileUI;
        return (React__default.createElement("div", { className: cx("CollapseGroup", {
                'icon-position-right': expandIconPosition === 'right'
            }, {
                'is-mobile': mobileUI
            }, className), style: style, "data-role": "container" }, this.getItems(children)));
    };
    CollapseGroup.defaultProps = {
        className: '',
        accordion: false,
        expandIconPosition: 'left'
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object, Boolean]),
        __metadata("design:returntype", void 0)
    ], CollapseGroup.prototype, "updateActiveKey", null);
    return CollapseGroup;
}(React__default.Component));
var CollapseGroup$1 = themeable(CollapseGroup);

export { CollapseGroup$1 as default };
