"use strict";

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

var _react = require("react");

var _lib = require("@sentecacommerce-theme/lib");

/* eslint-disable @typescript-eslint/no-unused-vars */

/* eslint-disable react-hooks/exhaustive-deps */
var UpdateProductPrice = function UpdateProductPrice(_ref) {
  var _selectedVariant$offe, _selectedVariant$offe2, _selectedVariant$offe3, _selectedVariant$offe4;

  var updatePrice = _ref.updatePrice,
      priceState = _ref.priceState;

  var _useProductState = (0, _lib.useProductState)(),
      selectedVariant = _useProductState.selectedVariant;

  var _usePrice = (0, _lib.usePrice)({
    price: selectedVariant == null ? void 0 : (_selectedVariant$offe = selectedVariant.offers) == null ? void 0 : (_selectedVariant$offe2 = _selectedVariant$offe[0]) == null ? void 0 : _selectedVariant$offe2.originalPrice,
    discountedPrice: selectedVariant == null ? void 0 : (_selectedVariant$offe3 = selectedVariant.offers) == null ? void 0 : (_selectedVariant$offe4 = _selectedVariant$offe3[0]) == null ? void 0 : _selectedVariant$offe4.discountedPrice
  }),
      price = _usePrice.price,
      discountPrice = _usePrice.discountPrice;

  var priceValue = price.value;
  var discountPriceValue = discountPrice ? discountPrice == null ? void 0 : discountPrice.value : undefined;
  var statePriceValue = priceState.price;
  var stateDiscountValue = priceState.discountedPrice;
  (0, _react.useEffect)(function () {
    if (!priceValue) return;

    if (priceValue && statePriceValue === priceValue && stateDiscountValue === discountPriceValue) {
      return;
    }

    updatePrice({
      price: priceValue,
      discountedPrice: discountPriceValue
    });
  }, [priceValue, discountPriceValue, stateDiscountValue, statePriceValue]);
  return null;
};

exports.UpdateProductPrice = UpdateProductPrice;
//# sourceMappingURL=index.js.map