function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useCallback } from 'react';
import classNames from 'classnames';
import { UpOutlined } from '@ant-design/icons';
import "./Collapse.less";
var PREFIX_CLS = 'pisell-collapse';

/**
 * 单条可折叠项，与 Figma 14256:5103 Collapse 设计 1:1 一致。
 * 结构：Head（时间/日期/客户/渠道 + 金额 + 展开图标）+ Body（明细行 + Void 等操作）。
 */
export var Collapse = /*#__PURE__*/React.memo(function (_ref) {
  var _headMeta$segments, _bodyRows$length;
  var headMeta = _ref.headMeta,
    headAmount = _ref.headAmount,
    controlledExpanded = _ref.expanded,
    _ref$defaultExpanded = _ref.defaultExpanded,
    defaultExpanded = _ref$defaultExpanded === void 0 ? false : _ref$defaultExpanded,
    onExpandChange = _ref.onExpandChange,
    children = _ref.children,
    bodyRows = _ref.bodyRows,
    className = _ref.className,
    style = _ref.style;
  var _useState = useState(defaultExpanded),
    _useState2 = _slicedToArray(_useState, 2),
    internalExpanded = _useState2[0],
    setInternalExpanded = _useState2[1];
  var isControlled = controlledExpanded !== undefined;
  var expanded = isControlled ? controlledExpanded : internalExpanded;
  var toggle = useCallback(function () {
    var next = !expanded;
    if (!isControlled) setInternalExpanded(next);
    onExpandChange === null || onExpandChange === void 0 || onExpandChange(next);
  }, [expanded, isControlled, onExpandChange]);
  var segments = (_headMeta$segments = headMeta.segments) !== null && _headMeta$segments !== void 0 ? _headMeta$segments : [];
  if (!segments.length && (headMeta.time != null || headMeta.date != null || headMeta.customer != null || headMeta.channel != null)) {
    if (headMeta.time != null || headMeta.date != null) {
      segments.push( /*#__PURE__*/React.createElement("span", {
        key: "time",
        className: "".concat(PREFIX_CLS, "-head-segment")
      }, [headMeta.time, headMeta.date].filter(Boolean).join(' ')));
    }
    if (headMeta.customer != null) {
      segments.push( /*#__PURE__*/React.createElement("span", {
        key: "customer",
        className: "".concat(PREFIX_CLS, "-head-segment")
      }, headMeta.customer));
    }
    if (headMeta.channel != null) {
      segments.push( /*#__PURE__*/React.createElement("span", {
        key: "channel",
        className: "".concat(PREFIX_CLS, "-head-segment")
      }, headMeta.channel));
    }
  }
  var hasBody = ((_bodyRows$length = bodyRows === null || bodyRows === void 0 ? void 0 : bodyRows.length) !== null && _bodyRows$length !== void 0 ? _bodyRows$length : 0) > 0 || children != null;
  return /*#__PURE__*/React.createElement("div", {
    className: classNames(PREFIX_CLS, className),
    style: style,
    "data-node-id": "14256:5103"
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX_CLS, "-item")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX_CLS, "-head"),
    onClick: hasBody ? toggle : undefined,
    role: hasBody ? 'button' : undefined,
    tabIndex: hasBody ? 0 : undefined,
    onKeyDown: function onKeyDown(e) {
      if (hasBody && (e.key === 'Enter' || e.key === ' ')) {
        e.preventDefault();
        toggle();
      }
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX_CLS, "-head-left")
  }, segments.map(function (seg, i) {
    return /*#__PURE__*/React.createElement("span", {
      key: i,
      className: "".concat(PREFIX_CLS, "-head-segment")
    }, seg);
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX_CLS, "-head-right")
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(PREFIX_CLS, "-head-amount")
  }, headAmount), hasBody && /*#__PURE__*/React.createElement("span", {
    className: classNames("".concat(PREFIX_CLS, "-head-chevron"), expanded && "".concat(PREFIX_CLS, "-head-chevron-expanded")),
    "aria-label": expanded ? 'collapse' : 'expand'
  }, /*#__PURE__*/React.createElement(UpOutlined, {
    style: {
      fontSize: 12
    }
  })))), hasBody && expanded && /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX_CLS, "-body")
  }, bodyRows === null || bodyRows === void 0 ? void 0 : bodyRows.map(function (row, i) {
    return /*#__PURE__*/React.createElement("div", {
      key: i,
      className: "".concat(PREFIX_CLS, "-body-row")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX_CLS, "-body-left")
    }, /*#__PURE__*/React.createElement("ul", null, /*#__PURE__*/React.createElement("li", null, row.label))), row.amount != null && /*#__PURE__*/React.createElement("span", {
      className: "".concat(PREFIX_CLS, "-body-amount")
    }, row.amount), row.action != null && /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: "".concat(PREFIX_CLS, "-body-action"),
      onClick: function onClick(e) {
        var _row$action, _row$action$onClick;
        e.stopPropagation();
        (_row$action = row.action) === null || _row$action === void 0 || (_row$action$onClick = _row$action.onClick) === null || _row$action$onClick === void 0 || _row$action$onClick.call(_row$action);
      }
    }, row.action.label));
  }), children)));
});
Collapse.displayName = 'Collapse';