import React, { createContext, useMemo } from 'react';
import { useCart } from "../Cart";
import { calculatePriceDetails } from "./utils";
import { useInit } from "../Init";

// 定义上下文接口

// 默认步骤状态
var defaultState = {};

// 创建步骤上下文
export var SummaryContext = /*#__PURE__*/createContext(undefined);
export var SummaryProvider = function SummaryProvider(_ref) {
  var children = _ref.children;
  var _useCart = useCart(),
    cartData = _useCart.cartData;
  var _useInit = useInit(),
    initData = _useInit.initData;
  var summaryData = useMemo(function () {
    return calculatePriceDetails((initData === null || initData === void 0 ? void 0 : initData.shop) || {}, cartData.cartItems);
  }, [cartData.cartItems, initData === null || initData === void 0 ? void 0 : initData.shop]);
  var contextValue = {
    // 商品总价
    subtotal: summaryData.subtotal,
    // 总金额
    total: summaryData.total,
    taxTitle: summaryData.taxTitle,
    totalTaxFee: summaryData.totalTaxFee,
    isPriceIncludeTax: summaryData.isPriceIncludeTax
  };
  return /*#__PURE__*/React.createElement(SummaryContext.Provider, {
    value: contextValue
  }, children);
};

/**
 * 使用步骤管理的Hook
 * @throws {Error} 如果在StepProvider外部使用会抛出错误
 * @returns {StepContextType} 步骤管理上下文
 */
export var useSummary = function useSummary() {
  var context = React.useContext(SummaryContext);
  if (!context) {
    throw new Error('useSummary must be used within a SummaryProvider');
  }
  return context;
};
export default {
  defaultState: defaultState
};