function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
import React, { useContext } from 'react';
import { CardItem, MediaItem } from "./components";
import usePresetItemProps from "./hooks/usePresetItemProps";
import { SelectorContext } from "./Selector";
var Item = function Item(props) {
  var actions = props.actions,
    dataSource = props.dataSource,
    indicator = props.indicator,
    numberSelector = props.numberSelector,
    optionItemValue = props.optionItemValue,
    quantityInfo = props.quantityInfo,
    values = props.values,
    children = props.children;
  var _useContext = useContext(SelectorContext),
    disabled = _useContext.disabled,
    mode = _useContext.mode,
    variant = _useContext.variant,
    itemProps = _useContext.itemProps,
    preset = _useContext.preset,
    itemLayout = _useContext.itemLayout;

  // 使用 hook 计算预设属性
  var _usePresetItemProps = usePresetItemProps({
      variant: variant,
      itemLayout: itemLayout,
      preset: preset,
      itemProps: itemProps,
      indicator: indicator,
      numberSelector: numberSelector,
      mode: mode,
      disabled: disabled || dataSource.disabled,
      selected: dataSource.selected
    }),
    mediaProps = _usePresetItemProps.mediaProps,
    cardProps = _usePresetItemProps.cardProps;
  var handleClick = function handleClick() {
    if (disabled || dataSource.disabled) return;
    if (dataSource.selected) {
      // 已选中，反选
      actions.update(dataSource._key, {
        quantity: 0
      });
    } else {
      // 未选中，选中时考虑 effectiveMin
      var quantity = dataSource.effectiveMin || 1;
      actions.update(dataSource._key, {
        quantity: quantity
      });
    }
  };
  if (variant === 'media') {
    return /*#__PURE__*/React.createElement(MediaItem, _extends({}, mediaProps, {
      cover: dataSource.cover,
      label: dataSource._label,
      extra: numberSelector,
      indicator: indicator,
      selected: dataSource.selected,
      disabled: disabled || dataSource.disabled,
      onClick: handleClick
    }));
  }
  var renderChildren = function renderChildren() {
    if (typeof (cardProps === null || cardProps === void 0 ? void 0 : cardProps.renderContent) === 'function') {
      var _cardProps$renderCont;
      return cardProps === null || cardProps === void 0 || (_cardProps$renderCont = cardProps.renderContent) === null || _cardProps$renderCont === void 0 ? void 0 : _cardProps$renderCont.call(cardProps, {
        dataSource: dataSource
      });
    }

    // 方便低代码模式下拖拽
    // @ts-ignore
    if ('design' === props.__designMode) {
      if (children == null || Array.isArray(children) && !children.length) {
        return /*#__PURE__*/React.createElement("div", {
          className: "lc-container-placeholder"
        }, "\u62D6\u62FD\u7EC4\u4EF6\u6216\u6A21\u677F\u5230\u8FD9\u91CC");
      }
    }
    return children ? children : /*#__PURE__*/React.createElement("span", null, dataSource._label);
  };
  return /*#__PURE__*/React.createElement(CardItem, _extends({}, cardProps, {
    selected: dataSource.selected,
    disabled: disabled || dataSource.disabled,
    onClick: handleClick
  }), renderChildren());
};
export default Item;