function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["dataSource", "isShowImage", "isShowHolder", "isShowNote", "isShowDelete", "isShowPackageNote", "isShowEditProduct", "onEditProduct", "onAction", "onDelete", "onNote", "onLike", "onCard", "onChangeResource", "onChangeHolder", "type"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
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; }
import React, { useMemo, useRef } from 'react';
import classNames from 'classnames';
import { SwipeAction } from 'antd-mobile';
import { image as imageAli, locales } from '@pisell/utils';
import localeTexts from "./locales";
import BasicInfo from "./components/basicInfo";
import DiscountReason from "./components/discountReason";
import Holders from "./components/holders";
import TimeRange from "./components/timeRange";
import Resources from "./components/resources";
import Packages from "./components/packages";
import Sales from "./components/sales";
import "./index.less";
import Iconfont from "../../iconfont";
import useEngineContext from "../../../hooks/useEngineContext";
import Items from "../components/MultiDay";
import AmountFooter from "../components/AmountFooter";
import UseOpenNote from "../hooks/useOpenNote";
import Note from "../components/Note";
import Action from "../components/Action";
import { defaultValue } from "../status";
export var PREFIX = 'pisell-cart-sku-card';
var CartSkuCard = function CartSkuCard(props) {
  var _context$appHelper$ut;
  var _props$dataSource = props.dataSource,
    dataSource = _props$dataSource === void 0 ? defaultValue.dataSource : _props$dataSource,
    _props$isShowImage = props.isShowImage,
    isShowImage = _props$isShowImage === void 0 ? defaultValue.isShowImage : _props$isShowImage,
    _props$isShowHolder = props.isShowHolder,
    isShowHolder = _props$isShowHolder === void 0 ? defaultValue.isShowHolder : _props$isShowHolder,
    _props$isShowNote = props.isShowNote,
    isShowNote = _props$isShowNote === void 0 ? defaultValue.isShowNote : _props$isShowNote,
    _props$isShowDelete = props.isShowDelete,
    isShowDelete = _props$isShowDelete === void 0 ? defaultValue.isShowDelete : _props$isShowDelete,
    _props$isShowPackageN = props.isShowPackageNote,
    isShowPackageNote = _props$isShowPackageN === void 0 ? defaultValue.isShowPackageNote : _props$isShowPackageN,
    _props$isShowEditProd = props.isShowEditProduct,
    isShowEditProduct = _props$isShowEditProd === void 0 ? defaultValue.isShowEditProduct : _props$isShowEditProd,
    onEditProduct = props.onEditProduct,
    _onAction = props.onAction,
    onDelete = props.onDelete,
    onNote = props.onNote,
    onLike = props.onLike,
    onCard = props.onCard,
    onChangeResource = props.onChangeResource,
    onChangeHolder = props.onChangeHolder,
    type = props.type,
    other = _objectWithoutProperties(props, _excluded);
  var useOpenNoteRef = useRef();
  var context = useEngineContext();
  locales.init(localeTexts, ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut = _context$appHelper$ut.storage) === null || _context$appHelper$ut === void 0 ? void 0 : _context$appHelper$ut.get('umi_locale')) || 'en');
  console.log('prodcut props', props);
  var isShowAction = dataSource.isShowAction,
    actionText = dataSource.actionText;
  var rightActions = useMemo(function () {
    var deleteAction = {
      key: 'delete',
      text: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Iconfont, {
        type: "pisell2-trash-01",
        style: {
          fontSize: 20,
          marginRight: 4
        }
      }), /*#__PURE__*/React.createElement("span", {
        style: {
          fontSize: 12
        }
      }, locales.getText('pisell2.cart.sku-card.actions.delete'))),
      color: 'danger',
      onClick: function onClick(e) {
        e.preventDefault();
        e.stopPropagation();
        onDelete === null || onDelete === void 0 || onDelete(dataSource);
      }
    };
    var actions = [];
    if (isShowDelete) {
      actions.push(deleteAction);
    }
    return actions;
  }, [dataSource, onDelete, isShowDelete]);
  var hideDivider = useMemo(function () {
    var bundle = dataSource.bundle,
      relation_products = dataSource.relation_products;
    return (bundle === null || bundle === void 0 ? void 0 : bundle.length) || (relation_products === null || relation_products === void 0 ? void 0 : relation_products.length);
  }, [dataSource]);
  var renderA2 = function renderA2() {
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(BasicInfo, {
      isShowImage: isShowImage,
      dataSource: dataSource,
      isMainProduct: true
    }), /*#__PURE__*/React.createElement(DiscountReason, {
      dataSource: dataSource
    }), isShowNote ? /*#__PURE__*/React.createElement(Note, _extends({}, other, {
      item: dataSource,
      openNote: function openNote() {
        useOpenNoteRef.current.open({
          item: dataSource
        });
      }
    })) : null);
  };
  var renderA5 = function renderA5() {
    var _dataSource$bundle, _dataSource$items;
    var image = dataSource.image,
      name = dataSource.name;
    return /*#__PURE__*/React.createElement(React.Fragment, null, isShowDelete ? /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__delete-btn"),
      onClick: function onClick(e) {
        e.preventDefault();
        e.stopPropagation();
        onDelete === null || onDelete === void 0 || onDelete(dataSource);
      }
    }, /*#__PURE__*/React.createElement(Iconfont, {
      type: "pisell2-trash-01"
    })) : null, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__content")
    }, isShowImage && image && /*#__PURE__*/React.createElement("img", {
      src: imageAli.ali(image, 64),
      className: "product-cover"
    }), /*#__PURE__*/React.createElement("div", {
      className: "product-info"
    }, /*#__PURE__*/React.createElement("div", {
      className: "product-name"
    }, name), /*#__PURE__*/React.createElement(Holders, {
      dataSource: dataSource
    }))), /*#__PURE__*/React.createElement(TimeRange, {
      dataSource: dataSource
    }), /*#__PURE__*/React.createElement(Resources, {
      dataSource: dataSource
    }), /*#__PURE__*/React.createElement(Packages, {
      dataSource: dataSource,
      isShowImage: isShowImage
    }), /*#__PURE__*/React.createElement(Sales, {
      dataSource: dataSource,
      hideDivider: !!(dataSource !== null && dataSource !== void 0 && (_dataSource$bundle = dataSource.bundle) !== null && _dataSource$bundle !== void 0 && _dataSource$bundle.length)
    }), (_dataSource$items = dataSource.items) !== null && _dataSource$items !== void 0 && _dataSource$items.length ? /*#__PURE__*/React.createElement(Items, {
      dataSource: dataSource,
      hideDivider: hideDivider
    }) : /*#__PURE__*/React.createElement(AmountFooter, {
      item: dataSource,
      hideDivider: hideDivider,
      type: "total"
    }), /*#__PURE__*/React.createElement(DiscountReason, {
      dataSource: dataSource
    }), isShowNote ? /*#__PURE__*/React.createElement(Note, _extends({}, other, {
      item: dataSource,
      openNote: function openNote() {
        useOpenNoteRef.current.open({
          item: dataSource
        });
      }
    })) : null, isShowAction && actionText ? /*#__PURE__*/React.createElement(Action, {
      item: dataSource,
      onAction: function onAction() {
        return _onAction === null || _onAction === void 0 ? void 0 : _onAction(dataSource);
      }
    }) : null);
  };
  var renderContent = function renderContent() {
    if (type === 'a5') {
      return renderA5();
    }
    return renderA2();
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SwipeAction, {
    className: classNames("".concat(PREFIX, "-wrapper")),
    rightActions: rightActions
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames(PREFIX, "".concat(PREFIX, "--").concat(type)),
    onClick: function onClick(e) {
      e.preventDefault();
      e.stopPropagation();
      onCard === null || onCard === void 0 || onCard(dataSource);
    }
  }, renderContent())), /*#__PURE__*/React.createElement(UseOpenNote, {
    ref: useOpenNoteRef,
    onNoteChange: function onNoteChange(e) {
      onNote === null || onNote === void 0 || onNote(_objectSpread(_objectSpread({}, dataSource), {}, {
        note: e || ''
      }));
    }
  }));
};
export default CartSkuCard;