"use strict";

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

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

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

var _icons = require("@react-vant/icons");

var _image = _interopRequireDefault(require("../../image"));

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

var _utils = require("../utils");

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

var SkuRowItem = function SkuRowItem(props) {
  var _props$skuValue, _props$skuValue2;

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

  var _createNamespace = createNamespace('sku-row', prefixCls),
      bem = _createNamespace[0];

  var classPrefix = props.largeImageMode ? 'image-item' : 'item';
  var imgUrl = (0, _react.useMemo)(function () {
    var url = props.skuValue.imgUrl || props.skuValue.img_url;
    return props.largeImageMode ? url || 'https://img01.yzcdn.cn/upload_files/2020/06/24/FmKWDg0bN9rMcTp9ne8MXiQWGtLn.png' : url;
  }, [props.skuValue]);
  var choosable = (0, _react.useMemo)(function () {
    if (!props.disableSoldoutSku) {
      return true;
    }

    return (0, _utils.isSkuChoosable)(props.skuList, props.selectedSku, {
      key: props.skuKeyStr,
      valueId: props.skuValue.id
    });
  }, [JSON.stringify(props.skuList), JSON.stringify(props.selectedSku), props.skuKeyStr]);

  var imageRender = function imageRender() {
    if (imgUrl) {
      return /*#__PURE__*/_react.default.createElement(_image.default, {
        fit: "cover",
        src: imgUrl,
        lazyload: props.lazyload,
        className: (0, _clsx.default)(bem(classPrefix + "-img"))
      });
    }

    return null;
  };

  var onSelect = function onSelect() {
    if (choosable) {
      props.onSkuSelected(_extends({}, props.skuValue, {
        skuKeyStr: props.skuKeyStr
      }));
    }
  };

  var onPreviewImg = function onPreviewImg(event) {
    event.stopPropagation();
    var skuValue = props.skuValue,
        skuKeyStr = props.skuKeyStr;
    props.onSkuPreviewImage(_extends({}, skuValue, {
      ks: skuKeyStr,
      imgUrl: skuValue.imgUrl || skuValue.img_url
    }));
  };

  var choosed = props.skuValue.id === props.selectedSku[props.skuKeyStr];
  return /*#__PURE__*/_react.default.createElement("span", {
    className: (0, _clsx.default)(bem(classPrefix, {
      active: choosed,
      disabled: !choosable
    })),
    onClick: onSelect
  }, imageRender(), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem(classPrefix + "-name"))
  }, props.largeImageMode ? /*#__PURE__*/_react.default.createElement("span", {
    className: (0, _clsx.default)('rv-multi-ellipsis--l2')
  }, (_props$skuValue = props.skuValue) == null ? void 0 : _props$skuValue.name) : (_props$skuValue2 = props.skuValue) == null ? void 0 : _props$skuValue2.name), props.largeImageMode && (props.previewIcon || /*#__PURE__*/_react.default.createElement(_icons.ExpandO, {
    className: (0, _clsx.default)(bem(classPrefix + "-img-icon")),
    onClick: onPreviewImg
  })));
};

var _default = SkuRowItem;
exports.default = _default;