import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React, { useCallback } from "react";
import classNames from "classnames";
import { View } from "@tarojs/components";
import { ComponentDefaults } from "../../utils/typings";
import CollapseItem from "../collapseitem/index";
import CollapseContext from "./context";
import { usePropsValue } from "../../hooks/use-props-value";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    defaultActiveName: [],
    accordion: false,
    expandIcon: null,
    rotate: 180
});
export var Collapse = function(props) {
    var _$_object_spread = _object_spread({}, defaultProps, props), className = _$_object_spread.className, style = _$_object_spread.style, children = _$_object_spread.children, activeName = _$_object_spread.activeName, defaultActiveName = _$_object_spread.defaultActiveName, accordion = _$_object_spread.accordion, expandIcon = _$_object_spread.expandIcon, rotate = _$_object_spread.rotate, onChange = _$_object_spread.onChange;
    var classPrefix = 'nut-collapse';
    var _usePropsValue = _sliced_to_array(usePropsValue({
        value: activeName,
        defaultValue: defaultActiveName,
        finalValue: []
    }), 2), value = _usePropsValue[0], setValue = _usePropsValue[1];
    var changeVal = function(newValue, name, isOpen) {
        setValue(newValue);
        onChange && onChange(newValue, name, isOpen);
    };
    var updateValue = function(name) {
        if (accordion) {
            if (value === name) {
                changeVal('', name, false);
            } else {
                changeVal(name, name, true);
            }
        } else if (Array.isArray(value)) {
            if (value.includes(name)) {
                var newValue = value.filter(function(v) {
                    return v !== name;
                });
                changeVal(newValue, name, false);
            } else {
                var newValue1 = value.concat([
                    name
                ]);
                changeVal(newValue1, name, true);
            }
        } else {
            console.warn('[NutUI] <Collapse> 未开启手风琴模式时 activeName 应为数组');
        }
    };
    var isOpen = useCallback(function(name) {
        if (accordion) {
            return value === name;
        }
        if (Array.isArray(value)) {
            return value.includes(name);
        }
        return false;
    }, [
        accordion,
        value
    ]);
    return /*#__PURE__*/ React.createElement(CollapseContext.Provider, {
        value: {
            isOpen: isOpen,
            updateValue: updateValue,
            expandIcon: expandIcon,
            rotate: rotate
        }
    }, /*#__PURE__*/ React.createElement(View, {
        className: classNames(classPrefix, className),
        style: style
    }, children));
};
Collapse.displayName = 'NutCollapse';
Collapse.Item = CollapseItem;
