import { __assign, __read, __rest, __spreadArray } from "tslib";
import React, { forwardRef } from "react";
import ListItem from "../ListItem";
import { useStateProps } from "../../../hooks/useStateProps";
import Switch from "../../../Switch";
import List from "../List";
var DEFAULT_VALUE = [];
var ListSwitch = forwardRef(function (_a, ref) {
    var dataSource = _a.dataSource, onChange = _a.onChange, _b = _a.defaultValue, defaultValue = _b === void 0 ? DEFAULT_VALUE : _b, className = _a.className, style = _a.style, isDisabled = _a.isDisabled, header = _a.header, height = _a.height, width = _a.width, viewType = _a.viewType, isSelected = _a.isSelected, isShowSelectAll = _a.isShowSelectAll, styleItem = _a.styleItem, classNameItem = _a.classNameItem, _c = _a.size, size = _c === void 0 ? "l" : _c, onClickItem = _a.onClickItem, props = __rest(_a, ["dataSource", "onChange", "defaultValue", "className", "style", "isDisabled", "header", "height", "width", "viewType", "isSelected", "isShowSelectAll", "styleItem", "classNameItem", "size", "onClickItem"]);
    var _d = __read(useStateProps(defaultValue), 2), value = _d[0], setValue = _d[1];
    var handleCLick = function (event, val) {
        var values = __spreadArray([], __read(value), false);
        if (values.includes(val)) {
            values.splice(values.indexOf(val), 1);
        }
        else {
            values.push(val);
        }
        setValue(values);
        onChange === null || onChange === void 0 ? void 0 : onChange(event, values);
        onClickItem === null || onClickItem === void 0 ? void 0 : onClickItem(event, val);
    };
    var handleSelectAll = function (event) {
        var values = __spreadArray([], __read(value), false);
        if (value.length === (dataSource === null || dataSource === void 0 ? void 0 : dataSource.length)) {
            values = [];
        }
        else {
            values = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.map(function (item) { return item.value; })) || [];
        }
        setValue(values);
        onChange === null || onChange === void 0 ? void 0 : onChange(event, values);
    };
    var renderItem = function (item) { return (React.createElement(ListItem, __assign({ size: size }, item, { key: item.label, className: classNameItem, style: styleItem, isDisabled: isDisabled || item.isDisabled, onClick: function (event) { return handleCLick(event, item.value); }, isSelected: value.indexOf(item.value) !== -1, trailContent: React.createElement(Switch, { isDisabled: isDisabled || item.isDisabled, isChecked: value.indexOf(item.value) !== -1 }) }), item.label)); };
    var renderSelectAllItems = function () { return (React.createElement(ListItem, { size: size, key: "selectAll", className: classNameItem, style: styleItem, isDisabled: isDisabled, onClick: function (event) { return handleSelectAll(event); }, isSelected: Boolean(value.length === (dataSource === null || dataSource === void 0 ? void 0 : dataSource.length)), trailContent: React.createElement(Switch, { isDisabled: isDisabled, isChecked: Boolean(value.length === (dataSource === null || dataSource === void 0 ? void 0 : dataSource.length)) }) }, "\u0412\u044B\u0431\u0440\u0430\u0442\u044C \u0432\u0441\u0435")); };
    return (React.createElement(List, __assign({ ref: ref, header: header, height: height, width: width, viewType: viewType, isSelected: isSelected, isShowSelectAll: isShowSelectAll, className: className, style: style, size: size, dataSource: dataSource, renderItem: renderItem, renderSelectAllItems: renderSelectAllItems, isDisabled: isDisabled }, props)));
});
ListSwitch.displayName = "ListSwitch";
export default ListSwitch;
