/* eslint-disable no-console */
import React, { useContext, useRef, useState } from 'react';
import clsx from 'clsx';
import CollapseContext from './CollapseContext';
import { BORDER_TOP_BOTTOM } from '../utils/constant';
import { useUpdateEffect } from '../hooks';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

function validateModelValue(modelValue, accordion) {
  if (accordion && Array.isArray(modelValue)) {
    console.error('[React Vant] 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 = useContext(ConfigProviderContext),
      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 = useRef(false);

  var _useState = 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);
  };

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

    setExpanded(props.value);
  }, [props.value]);
  return /*#__PURE__*/React.createElement(CollapseContext.Provider, {
    value: {
      isExpanded: isExpanded,
      toggle: toggle
    }
  }, /*#__PURE__*/React.createElement("div", {
    style: props.style,
    ref: props.nativeRef,
    className: clsx(bem(), props.className, (_clsx = {}, _clsx[BORDER_TOP_BOTTOM] = props.border, _clsx))
  }, React.Children.toArray(props.children).filter(Boolean).map(function (child, index) {
    return /*#__PURE__*/React.cloneElement(child, {
      index: index
    });
  })));
};

Collapse.defaultProps = {
  border: true
};
export default Collapse;