"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _jsxRuntime = require("react/jsx-runtime");
var _ahooks = require("ahooks");
var _material = require("@mui/material");
var _context = require("@arcblock/ux/lib/Locale/context");
var _react = require("react");
var _Toast = _interopRequireDefault(require("@arcblock/ux/lib/Toast"));
var _system = require("@mui/system");
var _api = _interopRequireDefault(require("../libs/api"));
var _util = require("../libs/util");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const fetchProduct = productId => {
  return _api.default.get(`/api/products/${productId}`).then(res => res?.data);
};
function PricingItem({
  productId,
  quantity,
  children = void 0,
  priceId
}) {
  const {
    locale
  } = (0, _context.useLocaleContext)();
  const {
    data: productInfo,
    loading = false
  } = (0, _ahooks.useRequest)(() => fetchProduct(productId), {
    refreshDeps: [productId],
    onSuccess: res => {
      if (!res) {
        _Toast.default.error((0, _util.formatError)(new Error("Product not found")));
      }
    },
    onError: err => {
      _Toast.default.error((0, _util.formatError)(err));
    }
  });
  const pricing = (0, _react.useMemo)(() => (0, _util.formatTotalPrice)({
    product: productInfo,
    quantity,
    priceId,
    locale
  }), [productInfo, quantity, priceId, locale]);
  if (loading) {
    return /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.CircularProgress, {});
  }
  return /* @__PURE__ */(0, _jsxRuntime.jsx)(_system.Box, {
    children: children ? children(pricing, productInfo) : /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
      children: pricing?.totalPrice
    })
  });
}
module.exports = PricingItem;