function _await(value, then, direct) {
  if (direct) {
    return then ? then(value) : value;
  }

  if (!value || !value.then) {
    value = Promise.resolve(value);
  }

  return then ? value.then(then) : value;
}

function _invoke(body, then) {
  var result = body();

  if (result && result.then) {
    return result.then(then);
  }

  return then(result);
}

function _async(f) {
  return function () {
    for (var args = [], i = 0; i < arguments.length; i++) {
      args[i] = arguments[i];
    }

    try {
      return Promise.resolve(f.apply(this, args));
    } catch (e) {
      return Promise.reject(e);
    }
  };
}

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

/* eslint-disable react/no-array-index-key */
import React, { forwardRef, useCallback, useContext, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
import cls from 'clsx';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
import ActionBar from '../action-bar';
import { Image } from '../image';
import { useSetState } from '../hooks';
import { BORDER_BOTTOM } from '../utils/constant';
import ImagePreview from '../image-preview';
import { Popup } from '../popup';
import { getSelectedProperties, getSelectedPropValues, getSelectedSkuValues, getSkuComb, getSkuImgValue, isAllSelected } from './utils';
import { LIMIT_TYPE, UNSELECTED_SKU_VALUE_ID } from './constants';
import SkuRow from './components/SkuRow';
import SkuRowItem from './components/SkuRowItem';
import SkuRowPropItem from './components/SkuRowPropItem';
import SkuStepper from './components/SkuStepper';
import Toast from '../toast';
var QUOTA_LIMIT = LIMIT_TYPE.QUOTA_LIMIT;
var Sku = /*#__PURE__*/forwardRef(function (props, ref) {
  var _props$goods, _props$popupProps;

  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

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

  var stepperError = useRef(false);

  var _useState = useState(false),
      visible = _useState[0],
      setVisible = _useState[1];

  var _useSetState = useSetState({
    selectedSku: {},
    selectedProp: {},
    selectedNum: props.startSaleNum
  }),
      state = _useSetState[0],
      updateState = _useSetState[1];

  var sku = props.sku,
      _props$properties = props.properties,
      properties = _props$properties === void 0 ? [] : _props$properties;
  var _sku$tree = sku.tree,
      tree = _sku$tree === void 0 ? [] : _sku$tree;
  var bodyStyle = useMemo(function () {
    var maxHeight = window.innerHeight - props.bodyOffsetTop;
    return {
      maxHeight: maxHeight + "px"
    };
  }, [props.bodyOffsetTop]);
  var imageList = useMemo(function () {
    var goods = props.goods;
    var rs = [goods == null ? void 0 : goods.picture];

    if (sku.tree.length > 0) {
      sku.tree.forEach(function (treeItem) {
        if (!treeItem.v) return;
        treeItem.v.forEach(function (vItem) {
          var img = vItem.previewImgUrl || vItem.imgUrl || vItem.img_url;

          if (img && rs.indexOf(img) === -1) {
            rs.push(img);
          }
        });
      });
    }

    return rs;
  }, [(_props$goods = props.goods) == null ? void 0 : _props$goods.picture, sku.tree]);
  var hasSku = useMemo(function () {
    return !sku.none_sku;
  }, [sku.none_sku]);
  var hasSkuOrAttr = useMemo(function () {
    return hasSku || properties.length > 0;
  }, [hasSku, properties]);
  var isSkuCombSelected = useMemo(function () {
    // SKU 未选完
    if (hasSku && !isAllSelected(tree, state.selectedSku)) {
      return false;
    } // 属性未全选


    return !properties.filter(function (i) {
      return i.is_necessary !== false;
    }).some(function (i) {
      return (state.selectedProp[i.k_id] || []).length === 0;
    });
  }, [hasSku, state]);
  var selectedSkuValues = useMemo(function () {
    return getSelectedSkuValues(tree, state.selectedSku);
  }, [tree, state.selectedSku]);
  var selectedPropValues = useMemo(function () {
    return getSelectedPropValues(properties, state.selectedProp);
  }, [properties, state.selectedProp]);
  var selectedSkuComb = useMemo(function () {
    var skuComb = null;

    if (isSkuCombSelected) {
      if (hasSku) {
        skuComb = getSkuComb(sku.list, state.selectedSku);
      } else {
        skuComb = {
          id: sku.collection_id,
          price: Math.round(+sku.price * 100),
          stock_num: sku.stock_num
        };
      }

      if (skuComb) {
        skuComb.properties = getSelectedProperties(properties, state.selectedProp);
        skuComb.property_price = selectedPropValues.reduce(function (acc, cur) {
          return acc + (cur.price || 0);
        }, 0);
      }
    }

    return skuComb;
  }, [isSkuCombSelected, hasSku, JSON.stringify(sku), JSON.stringify(state), properties, selectedPropValues]);
  var unselectedSku = useMemo(function () {
    return tree.filter(function (item) {
      return !state.selectedSku[item.k_s];
    }).map(function (item) {
      return item.k;
    });
  }, [tree, state.selectedSku]);
  var getUnselectedProp = useCallback(function (isNecessary) {
    return properties.filter(function (item) {
      return isNecessary ? item.is_necessary !== false : true;
    }).filter(function (item) {
      return (state.selectedProp[item.k_id] || []).length < 1;
    }).map(function (item) {
      return item.k;
    });
  }, [properties, state.selectedProp]);
  var selectedText = useMemo(function () {
    if (selectedSkuComb) {
      var values = selectedSkuValues.concat(selectedPropValues);
      return "\u5DF2\u9009 " + values.map(function (item) {
        return item.name;
      }).join(' ');
    }

    return "\u8BF7\u9009\u62E9 " + unselectedSku.concat(getUnselectedProp()).join(' ');
  }, [unselectedSku, getUnselectedProp, selectedSkuComb, selectedSkuValues, selectedPropValues]);
  var price = useMemo(function () {
    if (selectedSkuComb) {
      return ((selectedSkuComb.price + selectedSkuComb.property_price) / 100).toFixed(2);
    } // sku.price是一个格式化好的价格区间


    return sku.price;
  }, [JSON.stringify(selectedSkuComb), sku.price]);
  var stock = useMemo(function () {
    var stockNum = props.customStepperConfig.stockNum;

    if (stockNum !== undefined) {
      return stockNum;
    }

    if (selectedSkuComb) {
      return selectedSkuComb.stock_num;
    }

    return sku.stock_num;
  }, [sku.stock_num, JSON.stringify(selectedSkuComb)]);
  var stockContent = useMemo(function () {
    if (props.stockRender) {
      return props.stockRender(stock);
    }

    return /*#__PURE__*/React.createElement(React.Fragment, null, "\u5269\u4F59", /*#__PURE__*/React.createElement("span", {
      className: cls(bem('stock-num', {
        highlight: stock < props.stockThreshold
      }))
    }, stock), "\u4EF6");
  }, [stock]);

  var onSelect = function onSelect(skuValue) {
    var _extends2, _extends3;

    // 点击已选中的sku时则取消选中
    var selectedSku = state.selectedSku[skuValue.skuKeyStr] === skuValue.id ? _extends({}, state.selectedSku, (_extends2 = {}, _extends2[skuValue.skuKeyStr] = UNSELECTED_SKU_VALUE_ID, _extends2)) : _extends({}, state.selectedSku, (_extends3 = {}, _extends3[skuValue.skuKeyStr] = skuValue.id, _extends3));
    updateState({
      selectedSku: selectedSku
    });

    if (props.onSkuSelected) {
      props.onSkuSelected({
        skuValue: skuValue,
        selectedSku: state.selectedSku,
        selectedSkuComb: selectedSkuComb
      });
    }
  };

  var onPropSelect = function onPropSelect(propValue) {
    var _extends4;

    var arr = state.selectedProp[propValue.skuKeyStr] || [];
    var pos = arr.indexOf(propValue.id);

    if (pos > -1) {
      arr.splice(pos, 1);
    } else if (propValue.multiple) {
      arr.push(propValue.id);
    } else {
      arr.splice(0, 1, propValue.id);
    }

    var selectedProp = _extends({}, state.selectedProp, (_extends4 = {}, _extends4[propValue.skuKeyStr] = arr, _extends4));

    updateState({
      selectedProp: selectedProp
    });

    if (props.onSkuPropSelected) {
      props.onSkuPropSelected({
        propValue: propValue,
        selectedProp: state.selectedProp,
        selectedSkuComb: selectedSkuComb
      });
    }
  };

  var onOverLimit = function onOverLimit(data) {
    var action = data.action,
        limitType = data.limitType,
        quota = data.quota,
        quotaUsed = data.quotaUsed;
    var handleOverLimit = props.customStepperConfig.handleOverLimit;

    if (handleOverLimit) {
      handleOverLimit(data);
      return;
    }

    if (action === 'minus') {
      if (props.startSaleNum > 1) {
        Toast(props.startSaleNum + "\u4EF6\u8D77\u552E");
      } else {
        Toast('至少选择一件');
      }
    } else if (action === 'plus') {
      if (limitType === QUOTA_LIMIT) {
        if (quotaUsed > 0) {
          Toast("\u6BCF\u4EBA\u9650\u8D2D" + quota + "\u4EF6\uFF0C\u4F60\u5DF2\u8D2D\u4E70" + quotaUsed + "\u4EF6");
        } else {
          Toast("\u6BCF\u4EBA\u9650\u8D2D" + quota + "\u4EF6");
        }
      } else {
        Toast('库存不足');
      }
    }
  };

  var onStepperState = function onStepperState(data) {
    stepperError.current = data.valid ? null : _extends({}, data, {
      action: 'plus'
    });
  };

  var validateSku = function validateSku() {
    if (state.selectedNum === 0) {
      return '商品已经无法购买啦';
    }

    if (isSkuCombSelected) {
      return '';
    }

    return "\u8BF7\u9009\u62E9 " + unselectedSku.concat(getUnselectedProp(true)).join(' ');
  };

  var getSkuData = function getSkuData() {
    return {
      goodsId: props.goodsId,
      selectedNum: state.selectedNum,
      selectedSkuComb: selectedSkuComb
    };
  };

  var onAddCart = function onAddCart(data) {
    props.onAddCart == null ? void 0 : props.onAddCart(data);
  };

  var onBuyClicked = function onBuyClicked(data) {
    props.onBuyClicked == null ? void 0 : props.onBuyClicked(data);
  };

  var onBuyOrAddCart = _async(function (type) {
    var _exit = false;

    // sku 不符合购买条件
    if (stepperError.current) {
      onOverLimit(stepperError.current);
      return;
    }

    return _invoke(function () {
      if (props.customSkuValidator) {
        return _await(props.customSkuValidator(type, _extends({}, state.selectedSku, state.selectedProp)), function (_props$customSkuValid) {
          if (!_props$customSkuValid) {
            _exit = true;
          }
        });
      } else {
        var error = validateSku();

        if (error) {
          Toast(error);
          _exit = true;
        }
      }
    }, function (_result) {
      if (_exit) return _result;
      var data = getSkuData();

      if (type === 'add-cart') {
        onAddCart(data);
      } else {
        onBuyClicked(data);
      }
    });
  });

  var show = function show(initialValue) {
    setVisible(true);

    if (initialValue) {
      updateState(initialValue);
    }
  };

  var reset = function reset() {
    updateState({
      selectedSku: {},
      selectedProp: {},
      selectedNum: props.startSaleNum
    });
  };

  var onPopupClose = function onPopupClose() {
    setVisible(false);
    if (props.popupProps && props.popupProps.onClose) props.popupProps.onClose();
  };

  var onPopupClosed = function onPopupClosed() {
    if (props.resetOnHide) {
      reset();
    }

    if (props.popupProps && props.popupProps.onClosed) props.popupProps.onClosed();
  };

  var onPreviewImage = function onPreviewImage(selectedValue) {
    var index = 0;
    var indexImage = imageList[0];

    if (selectedValue && selectedValue.imgUrl) {
      imageList.some(function (image, pos) {
        if (image === selectedValue.imgUrl) {
          index = pos;
          return true;
        }

        return false;
      });
      indexImage = selectedValue.imgUrl;
    }

    var params = _extends({}, selectedValue, {
      index: index,
      imageList: imageList,
      indexImage: indexImage
    });

    if (!props.previewOnClickImage) return;
    ImagePreview.open({
      images: imageList,
      startPosition: index,
      onClose: function onClose() {
        if (props.onClosePreview) props.onClosePreview(params);
      }
    });

    if (props.onOpenPreview) {
      props.onOpenPreview(params);
    }
  };

  var renderHeaderInfo = function renderHeaderInfo() {
    return /*#__PURE__*/React.createElement(React.Fragment, null, (props.skuHeaderPriceRender == null ? void 0 : props.skuHeaderPriceRender(price)) || /*#__PURE__*/React.createElement("div", {
      className: cls(bem('goods-price'))
    }, /*#__PURE__*/React.createElement("span", {
      className: cls(bem('price-symbol'))
    }, "\uFFE5"), /*#__PURE__*/React.createElement("span", {
      className: cls(bem('price-num'))
    }, price), props.priceTag && /*#__PURE__*/React.createElement("span", {
      className: cls(bem('price-tag'))
    }, props.priceTag)), props.skuHeaderOriginPrice && /*#__PURE__*/React.createElement("div", {
      className: cls(bem('header-item'))
    }, props.skuHeaderOriginPrice), !props.hideStock && /*#__PURE__*/React.createElement("div", {
      className: cls(bem('header-item'))
    }, /*#__PURE__*/React.createElement("span", {
      className: cls(bem('stock'))
    }, stockContent)), !props.hideSelectedText && /*#__PURE__*/React.createElement("div", {
      className: cls(bem('header-item'))
    }, selectedText));
  };

  var renderHeader = function renderHeader() {
    if (props.skuHeader) return props.skuHeader;
    var selectedValue = getSkuImgValue(sku, state.selectedSku);
    var imgUrl = selectedValue ? selectedValue.imgUrl : props.goods.picture;
    return /*#__PURE__*/React.createElement("div", {
      className: cls(bem('header'), BORDER_BOTTOM)
    }, props.showHeaderImage && /*#__PURE__*/React.createElement(Image, {
      fit: "cover",
      src: imgUrl,
      className: cls(bem('header__img-wrap')),
      onClick: function onClick() {
        return onPreviewImage(selectedValue);
      }
    }, props.skuHeaderImageExtra), /*#__PURE__*/React.createElement("div", {
      className: cls(bem('header__goods-info'))
    }, renderHeaderInfo(), props.skuHeaderExtra));
  };

  var renderGroup = function renderGroup() {
    return props.skuGroup || hasSkuOrAttr && /*#__PURE__*/React.createElement("div", {
      className: cls(bem('group-container', {
        'hide-soldout': !props.showSoldoutSku
      }))
    }, tree.map(function (skuTreeItem, i) {
      return /*#__PURE__*/React.createElement(SkuRow, {
        key: i,
        skuRow: skuTreeItem
      }, skuTreeItem.v.map(function (skuValue, idx) {
        return /*#__PURE__*/React.createElement(SkuRowItem, {
          key: idx,
          skuList: sku.list,
          skuValue: skuValue,
          skuKeyStr: "" + skuTreeItem.k_s,
          selectedSku: state.selectedSku,
          disableSoldoutSku: props.disableSoldoutSku,
          largeImageMode: skuTreeItem.largeImageMode,
          previewIcon: props.previewIcon,
          onSkuSelected: onSelect,
          onSkuPreviewImage: function onSkuPreviewImage(selectedValue) {
            return onPreviewImage(selectedValue);
          }
        });
      }));
    }), properties.map(function (skuTreeItem, i) {
      return /*#__PURE__*/React.createElement(SkuRow, {
        key: i,
        skuRow: skuTreeItem
      }, skuTreeItem.v.map(function (skuValue, idx) {
        return /*#__PURE__*/React.createElement(SkuRowPropItem, {
          key: idx,
          skuValue: skuValue,
          skuKeyStr: "" + skuTreeItem.k_id,
          selectedProp: state.selectedProp,
          multiple: skuTreeItem.is_multiple,
          onSkuPropSelected: onPropSelect
        });
      }));
    }));
  };

  var renderStepper = function renderStepper() {
    return props.skuStepper || /*#__PURE__*/React.createElement(SkuStepper, {
      currentNum: state.selectedNum,
      onChange: function onChange(currentValue) {
        updateState({
          selectedNum: parseInt("" + currentValue, 10)
        });
        if (props.onStepperChange) props.onStepperChange(currentValue);
      },
      stock: stock,
      quota: props.quota,
      quotaUsed: props.quotaUsed,
      startSaleNum: props.startSaleNum,
      disableStepperInput: props.disableStepperInput,
      customStepperConfig: props.customStepperConfig,
      stepperTitle: props.stepperTitle,
      hideQuotaText: props.hideQuotaText,
      onSkuStepperState: onStepperState,
      onSkuOverLimit: onOverLimit
    });
  };

  var renderBody = function renderBody() {
    return /*#__PURE__*/React.createElement("div", {
      className: cls(bem('body')),
      style: bodyStyle
    }, props.skuBodyTop, renderGroup(), props.skuGroupExtra, renderStepper());
  };

  var renderActions = function renderActions() {
    return props.skuActions || /*#__PURE__*/React.createElement("div", {
      className: cls(bem('actions'))
    }, /*#__PURE__*/React.createElement(ActionBar, null, props.showAddCartBtn && /*#__PURE__*/React.createElement(ActionBar.Button, {
      type: "warning",
      text: props.addCartText || '加入购物车',
      onClick: function onClick() {
        return onBuyOrAddCart('add-cart');
      }
    }), /*#__PURE__*/React.createElement(ActionBar.Button, {
      type: "danger",
      text: props.buyText || '立即购买',
      onClick: function onClick() {
        return onBuyOrAddCart('buy-clicked');
      }
    })));
  };

  useEffect(function () {
    if (props.initialSku) {
      updateState(props.initialSku);
    }
  }, [JSON.stringify(props.initialSku)]);
  useImperativeHandle(ref, function () {
    return {
      reset: reset,
      getSkuData: getSkuData,
      show: show,
      update: updateState
    };
  });
  return /*#__PURE__*/React.createElement(Popup, _extends({
    round: true,
    closeable: true,
    position: "bottom"
  }, props.popupProps, {
    visible: visible,
    onClose: onPopupClose,
    onClosed: onPopupClosed,
    className: cls((_props$popupProps = props.popupProps) == null ? void 0 : _props$popupProps.className, bem('container'))
  }), renderHeader(), renderBody(), props.skuActionsTop, renderActions(), props.skuActionsBottom);
}); // defaultProps defined if need

Sku.defaultProps = {
  stepperTitle: '购买数量',
  properties: [],
  showAddCartBtn: true,
  disableSoldoutSku: true,
  showHeaderImage: true,
  previewOnClickImage: true,
  showSoldoutSku: true,
  resetOnHide: true,
  safeAreaInsetBottom: true,
  quota: 0,
  quotaUsed: 0,
  startSaleNum: 1,
  stockThreshold: 50,
  bodyOffsetTop: 200,
  customStepperConfig: {}
};
export default Sku;