"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _clsx2 = _interopRequireDefault(require("clsx"));

var _CollapseContext = _interopRequireDefault(require("./CollapseContext"));

var _constant = require("../utils/constant");

var _hooks = require("../hooks");

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/* eslint-disable no-console */
function validateModelValue(modelValue, accordion) {
  if (accordion && Array.isArray(modelValue)) {
    console.error('[ReactVant] Collapse: "value" should not be Array in accordion mode');
    return false;
  }

  if (!accordion && !Array.isArray(modelValue)) {
    console.error('[React Vant] Collapse: "value" should be Array in non-accordion mode');
    return false;
  }

  return true;
}

var Collapse = function Collapse(props) {
  var _clsx;

  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('collapse', prefixCls),
      bem = _createNamespace[0];

  var accordion = props.accordion;
  var initExpandedDefault = accordion ? '' : [];
  var _props$initExpanded = props.initExpanded,
      initExpanded = _props$initExpanded === void 0 ? initExpandedDefault : _props$initExpanded;
  var innerEffect = (0, _react.useRef)(false);

  var _useState = (0, _react.useState)(function () {
    var _props$value;

    return (_props$value = props.value) != null ? _props$value : initExpanded;
  }),
      expanded = _useState[0],
      setExpanded = _useState[1];

  var updateName = function updateName(name) {
    innerEffect.current = true;
    setExpanded(name);
    props.onChange == null ? void 0 : props.onChange(name);
  };

  var toggle = function toggle(name, isExpanded) {
    if (accordion) {
      if (name === expanded) {
        name = '';
      }
    } else if (isExpanded) {
      name = expanded.concat(name);
    } else {
      name = expanded.filter(function (activeName) {
        return activeName !== name;
      });
    }

    updateName(name);
  };

  var isExpanded = function isExpanded(name) {
    if (process.env.NODE_ENV !== 'production' && !validateModelValue(expanded, accordion)) {
      return false;
    }

    return accordion ? expanded === name : expanded.includes(name);
  };

  (0, _hooks.useUpdateEffect)(function () {
    if (innerEffect.current) {
      innerEffect.current = false;
      return;
    }

    setExpanded(props.value);
  }, [props.value]);
  return /*#__PURE__*/_react.default.createElement(_CollapseContext.default.Provider, {
    value: {
      isExpanded: isExpanded,
      toggle: toggle
    }
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx2.default)(bem(), (_clsx = {}, _clsx[_constant.BORDER_TOP_BOTTOM] = props.border, _clsx))
  }, _react.default.Children.toArray(props.children).filter(Boolean).map(function (child, index) {
    return /*#__PURE__*/_react.default.cloneElement(child, {
      index: index
    });
  })));
};

Collapse.defaultProps = {
  border: true
};
var _default = Collapse;
exports.default = _default;