import { __assign, __read, __rest } from "tslib";
import React, { forwardRef } from "react";
import ListHeader from "../ListHeader";
import ListItem from "../ListItem";
import { useStateProps } from "../../../hooks/useStateProps";
import { ListStyled, ListItemsWrapper } from "./styles";
var List = forwardRef(function (_a, ref) {
    var header = _a.header, dataSource = _a.dataSource, viewType = _a.viewType, className = _a.className, classNameItem = _a.classNameItem, onClickItem = _a.onClickItem, style = _a.style, styleItem = _a.styleItem, renderItem = _a.renderItem, renderSelectAllItems = _a.renderSelectAllItems, _b = _a.isSelected, isSelected = _b === void 0 ? false : _b, _c = _a.isDisabled, isDisabled = _c === void 0 ? false : _c, _d = _a.isShowSelectAll, isShowSelectAll = _d === void 0 ? false : _d, onDoubleClickItem = _a.onDoubleClickItem, width = _a.width, height = _a.height, _e = _a.size, size = _e === void 0 ? "l" : _e, children = _a.children, props = __rest(_a, ["header", "dataSource", "viewType", "className", "classNameItem", "onClickItem", "style", "styleItem", "renderItem", "renderSelectAllItems", "isSelected", "isDisabled", "isShowSelectAll", "onDoubleClickItem", "width", "height", "size", "children"]);
    var _f = __read(useStateProps(isSelected), 2), isExpand = _f[0], setIsExpand = _f[1];
    var handleClickHeader = function (event) {
        var _a;
        setIsExpand(!isExpand);
        (_a = header === null || header === void 0 ? void 0 : header.onClick) === null || _a === void 0 ? void 0 : _a.call(header, event);
    };
    var renderItems = function () {
        if (!children) {
            if (renderItem) {
                return dataSource === null || dataSource === void 0 ? void 0 : dataSource.map(function (item, index) { return renderItem(item, index); });
            }
            return dataSource === null || dataSource === void 0 ? void 0 : dataSource.map(function (item) { return (React.createElement(ListItem, __assign({}, item, { size: size, key: item.label, className: classNameItem, style: styleItem, isSelected: item.isSelected, isDisabled: isDisabled || item.isDisabled, onDoubleClick: onDoubleClickItem, onClick: onClickItem }), item.label)); });
        }
        return children;
    };
    return (React.createElement(ListStyled, __assign({ className: className, style: style, ref: ref, width: width, height: height }, props),
        header && (React.createElement(ListHeader, { size: size, viewType: viewType, isDisabled: isDisabled, onClick: handleClickHeader, isSelectedDefault: isExpand, leadIcon: header.leadIcon, trailIcon: header.trailIcon, className: header.className, style: header.style, avatar: header.avatar }, header.content)),
        React.createElement(ListItemsWrapper, { isExpand: isExpand, isHeader: Boolean(header), type: viewType },
            React.createElement(React.Fragment, null,
                isShowSelectAll && renderSelectAllItems && renderSelectAllItems(),
                renderItems()))));
});
List.displayName = "List";
export default List;
