"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Collapse", {
    enumerable: true,
    get: function() {
        return Collapse;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _components = require("@tarojs/components");
var _typings = require("../../utils/typings");
var _index = /*#__PURE__*/ _interop_require_default._(require("../collapseitem/index"));
var _context = /*#__PURE__*/ _interop_require_default._(require("./context"));
var _usepropsvalue = require("../../hooks/use-props-value");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    defaultActiveName: [],
    accordion: false,
    expandIcon: null,
    rotate: 180
});
var Collapse = function Collapse(props) {
    var _$_object_spread = (0, _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 = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: activeName,
        defaultValue: defaultActiveName,
        finalValue: []
    }), 2), value = _usePropsValue[0], setValue = _usePropsValue[1];
    var changeVal = function changeVal(newValue, name, isOpen) {
        setValue(newValue);
        onChange && onChange(newValue, name, isOpen);
    };
    var updateValue = function updateValue(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 = (0, _react.useCallback)(function(name) {
        if (accordion) {
            return value === name;
        }
        if (Array.isArray(value)) {
            return value.includes(name);
        }
        return false;
    }, [
        accordion,
        value
    ]);
    return /*#__PURE__*/ _react.default.createElement(_context.default.Provider, {
        value: {
            isOpen: isOpen,
            updateValue: updateValue,
            expandIcon: expandIcon,
            rotate: rotate
        }
    }, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: (0, _classnames.default)(classPrefix, className),
        style: style
    }, children));
};
Collapse.displayName = 'NutCollapse';
Collapse.Item = _index.default;
