"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

exports.__esModule = true;
exports.useVoucherForm = exports.useCardsContext = exports.useVoucherStyle = exports.CheckoutVoucherContextProvider = void 0;

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

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

var _reactHookForm = require("react-hook-form");

var CheckoutVoucherContext = /*#__PURE__*/(0, _react.createContext)({});

var CheckoutVoucherContextProvider = function CheckoutVoucherContextProvider(props) {
  var defaultSelectedStyle = props.defaultSelectedStyle,
      defaultSelectedCardId = props.defaultSelectedCardId,
      cardStyles = props.cardStyles,
      children = props.children,
      cards = props.cards;
  var labels = (0, _lib.useStrings)('checkoutVoucher');

  var _myCartApi$useAddCust = _lib.myCartApi.useAddCustom(),
      mutate = _myCartApi$useAddCust[0];

  var _useForm = (0, _reactHookForm.useForm)({
    defaultValues: {
      style: defaultSelectedStyle,
      cardId: '',
      firstName: '',
      lastName: '',
      email: '',
      additionalInfo: ''
    }
  }),
      control = _useForm.control,
      errors = _useForm.errors,
      setValue = _useForm.setValue,
      register = _useForm.register,
      handleSubmit = _useForm.handleSubmit;

  var onSubmit = function onSubmit(data) {
    var _data$cardId;

    var recipientData = {
      email: data == null ? void 0 : data.email,
      firstName: data == null ? void 0 : data.firstName,
      lastName: data == null ? void 0 : data.lastName
    };
    var centAmount = Number(data == null ? void 0 : data.cardId) || Number(data == null ? void 0 : data.customAmount) || 0;
    mutate({
      name: [{
        lang: 'bg',
        value: labels.title
      }],
      money: {
        centAmount: centAmount * 100,
        currencyCode: 'BGN',
        fractionDigits: 2
      },
      quantity: 1,
      slug: 'checkout-voucher',
      taxCategory: {
        resourceKey: 'vouchers',
        resourceType: _lib.KeyReferenceDTOResourceTypeEnum.TaxCategory
      },
      fields: {
        productId: 'online-gift-card',
        variantId: (_data$cardId = data == null ? void 0 : data.cardId) != null ? _data$cardId : data.customAmount,
        image: {
          thumbnail: data.style.smallImage,
          original: data.style.image
        },
        quantity: 1,
        recipientData: recipientData,
        additionalInfo: data == null ? void 0 : data.additionalInfo
      }
    });
    setValue('customAmount', '');
    setValue('cardId', '');
    setValue('firstName', '');
    setValue('lastName', '');
    setValue('additionalInfo', '');
    setValue('email', '');
  };

  var handleFormSubmit = handleSubmit(onSubmit);
  register('cardId', {
    required: false
  });
  var cardId = (0, _reactHookForm.useWatch)({
    control: control,
    name: 'cardId',
    defaultValue: defaultSelectedCardId
  });
  register('customAmount', {
    required: false
  });
  var customAmount = (0, _reactHookForm.useWatch)({
    control: control,
    name: 'customAmount',
    defaultValue: undefined
  });
  register('style', {
    required: true
  });
  var style = (0, _reactHookForm.useWatch)({
    control: control,
    name: 'style',
    defaultValue: defaultSelectedStyle
  });

  var updateStyle = function updateStyle(s) {
    setValue('style', s);
  };

  var updateCard = function updateCard(id) {
    setValue('cardId', id);
    setValue('customAmount', '');
  };

  var updateCustomAmount = function updateCustomAmount(value) {
    setValue('cardId', '');
    setValue('customAmount', value);
  };

  return /*#__PURE__*/_react["default"].createElement(CheckoutVoucherContext.Provider, {
    value: {
      style: style,
      updateStyle: updateStyle,
      cardStyles: cardStyles,
      formControl: control,
      errors: errors,
      cardId: cardId,
      cards: cards,
      customAmount: customAmount,
      updateCard: updateCard,
      updateCustomAmount: updateCustomAmount,
      handleFormSubmit: handleFormSubmit
    }
  }, children);
};

exports.CheckoutVoucherContextProvider = CheckoutVoucherContextProvider;

var useVoucherStyle = function useVoucherStyle() {
  var data = (0, _react.useContext)(CheckoutVoucherContext);
  return {
    selectedStyle: data.style,
    updateStyle: data.updateStyle,
    styleList: data.cardStyles
  };
};

exports.useVoucherStyle = useVoucherStyle;

var useCardsContext = function useCardsContext() {
  return (0, _react.useContext)(CheckoutVoucherContext);
};

exports.useCardsContext = useCardsContext;

var useVoucherForm = function useVoucherForm() {
  var _useContext = (0, _react.useContext)(CheckoutVoucherContext),
      errors = _useContext.errors,
      formControl = _useContext.formControl,
      handleFormSubmit = _useContext.handleFormSubmit;

  return {
    errors: errors,
    formControl: formControl,
    handleFormSubmit: handleFormSubmit
  };
};

exports.useVoucherForm = useVoucherForm;

function uuidv4() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0,
        v = c == 'x' ? r : r & 0x3 | 0x8;
    return v.toString(16);
  });
}
//# sourceMappingURL=checkoutVoucher.context.js.map