"use strict";

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

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

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

var _cell = _interopRequireDefault(require("../cell"));

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

var _useLazyRender = _interopRequireDefault(require("../hooks/use-lazy-render"));

var _raf = require("../utils/raf");

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

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

var _excluded = ["border", "disabled", "children", "readonly"];

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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var CollapseItem = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var index = props.index;
  var parent = (0, _react.useContext)(_CollapseContext.default);

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

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

  var wrapperRef = (0, _react.useRef)(null);
  var contentRef = (0, _react.useRef)(null);
  var name = (0, _react.useMemo)(function () {
    var _props$name;

    return (_props$name = props.name) != null ? _props$name : index;
  }, [index, props.name]);
  var expanded = (0, _react.useMemo)(function () {
    if (parent) {
      return parent.isExpanded(name);
    }

    return null;
  }, [parent, name]);

  var _useState = (0, _react.useState)(function () {
    return expanded;
  }),
      show = _useState[0],
      setShow = _useState[1];

  var lazyRender = (0, _useLazyRender.default)(show);

  var onTransitionEnd = function onTransitionEnd() {
    if (!expanded) {
      setShow(false);
    } else {
      wrapperRef.current.style.height = '';
    }
  };

  (0, _hooks.useUpdateEffect)(function () {
    if (expanded) {
      setShow(true);
    }

    (0, _raf.raf)(function () {
      if (!contentRef.current || !wrapperRef.current) {
        return;
      }

      var offsetHeight = contentRef.current.offsetHeight;

      if (offsetHeight) {
        var contentHeight = offsetHeight + "px";
        wrapperRef.current.style.height = expanded ? 0 : contentHeight; // use double raf to ensure animation can start

        (0, _raf.doubleRaf)(function () {
          wrapperRef.current.style.height = expanded ? contentHeight : 0;
        });
      } else {
        onTransitionEnd();
      }
    });
  }, [expanded]);

  var toggle = function toggle(value) {
    if (value === void 0) {
      value = !expanded;
    }

    parent.toggle(name, value);
  };

  var onClickTitle = function onClickTitle() {
    if (!props.disabled && !props.readonly) {
      toggle();
    }
  };

  var renderTitle = function renderTitle() {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    var border = props.border,
        disabled = props.disabled,
        children = props.children,
        readonly = props.readonly,
        others = _objectWithoutPropertiesLoose(props, _excluded);

    return /*#__PURE__*/_react.default.createElement(_cell.default, _extends({
      className: (0, _clsx.default)(bem('title', {
        disabled: disabled,
        expanded: expanded,
        borderless: !border
      })),
      "aria-expanded": String(expanded),
      onClick: onClickTitle
    }, others, {
      isLink: readonly ? false : others.isLink,
      clickable: disabled || readonly ? false : others.clickable
    }));
  };

  var renderContent = lazyRender(function () {
    return /*#__PURE__*/_react.default.createElement("div", {
      ref: wrapperRef,
      className: (0, _clsx.default)(bem('wrapper')),
      onTransitionEnd: onTransitionEnd
    }, /*#__PURE__*/_react.default.createElement("div", {
      ref: contentRef,
      className: (0, _clsx.default)(bem('content'))
    }, props.children));
  });
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      toggle: toggle
    };
  });
  return /*#__PURE__*/_react.default.createElement("div", {
    style: props.style,
    className: (0, _clsx.default)(props.className, bem({
      border: index && props.border
    }))
  }, renderTitle(), renderContent());
});
CollapseItem.defaultProps = {
  isLink: true,
  border: true
};
var _default = CollapseItem;
exports.default = _default;