/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	// The require scope
/******/ 	var __webpack_require__ = {};
/******/ 	
/************************************************************************/
/******/ 	/* webpack/runtime/define property getters */
/******/ 	(() => {
/******/ 		// define getter functions for harmony exports
/******/ 		__webpack_require__.d = (exports, definition) => {
/******/ 			for(var key in definition) {
/******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ 				}
/******/ 			}
/******/ 		};
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/hasOwnProperty shorthand */
/******/ 	(() => {
/******/ 		__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/make namespace object */
/******/ 	(() => {
/******/ 		// define __esModule on exports
/******/ 		__webpack_require__.r = (exports) => {
/******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 			}
/******/ 			Object.defineProperty(exports, '__esModule', { value: true });
/******/ 		};
/******/ 	})();
/******/ 	
/************************************************************************/
var __webpack_exports__ = {};
/*!******************************************************!*\
  !*** ./js/event-providers/easy-digital-downloads.js ***!
  \******************************************************/
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   parseCartItemHTML: () => (/* binding */ parseCartItemHTML)
/* harmony export */ });
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
 * Site Kit by Google, Copyright 2024 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

((jQuery, _global$_googlesiteki3) => {
  // eslint-disable-next-line no-undef
  if (!jQuery) {
    return;
  }
  const body = jQuery('body');
  const currency = globalThis._googlesitekit.easyDigitalDownloadsCurrency;
  body.on('edd_cart_item_added', (event, details) => {
    var _global$_googlesiteki, _global$_googlesiteki2;
    const value = parseAmount(details.total);
    const _parseCartItemHTML = parseCartItemHTML(details.cart_item),
      id = _parseCartItemHTML.id,
      name = _parseCartItemHTML.name,
      price = _parseCartItemHTML.price;
    (_global$_googlesiteki = globalThis._googlesitekit) === null || _global$_googlesiteki === void 0 || (_global$_googlesiteki2 = _global$_googlesiteki.gtagEvent) === null || _global$_googlesiteki2 === void 0 || _global$_googlesiteki2.call(_global$_googlesiteki, 'add_to_cart', {
      currency,
      value,
      items: [{
        item_id: id,
        item_name: name,
        price
      }],
      googlesitekit_event_provider: 'easy-digital-downloads'
    });
  });
  if ((_global$_googlesiteki3 = globalThis._googlesitekit) !== null && _global$_googlesiteki3 !== void 0 && (_global$_googlesiteki3 = _global$_googlesiteki3.edddata) !== null && _global$_googlesiteki3 !== void 0 && _global$_googlesiteki3.purchase) {
    var _global$_googlesiteki4, _global$_googlesiteki5;
    (_global$_googlesiteki4 = globalThis._googlesitekit) === null || _global$_googlesiteki4 === void 0 || (_global$_googlesiteki5 = _global$_googlesiteki4.gtagEvent) === null || _global$_googlesiteki5 === void 0 || _global$_googlesiteki5.call(_global$_googlesiteki4, 'purchase', _objectSpread(_objectSpread({
      currency
    }, globalThis._googlesitekit.edddata.purchase), {}, {
      googlesitekit_event_provider: 'easy-digital-downloads'
    }));
  }
})(globalThis.jQuery);

/**
 * Parses the provided cart item HTML to extract product details.
 *
 * @since 1.153.0
 *
 * @param {string} cartItemHTML The HTML string for the cart item.
 * @return {Object} An object containing the item's `id`, `name` and `price`.
 */
function parseCartItemHTML(cartItemHTML) {
  var _doc$querySelector, _doc$querySelector2, _doc$querySelector3;
  const parser = new DOMParser();
  const doc = parser.parseFromString(cartItemHTML, 'text/html');
  const id =
  // eslint-disable-next-line sitekit/acronym-case
  ((_doc$querySelector = doc.querySelector('.edd-remove-from-cart')) === null || _doc$querySelector === void 0 ? void 0 : _doc$querySelector.dataset.downloadId) || '';
  const name = ((_doc$querySelector2 = doc.querySelector('.edd-cart-item-title')) === null || _doc$querySelector2 === void 0 ? void 0 : _doc$querySelector2.textContent.trim()) || '';
  const price = ((_doc$querySelector3 = doc.querySelector('.edd-cart-item-price')) === null || _doc$querySelector3 === void 0 ? void 0 : _doc$querySelector3.textContent.trim()) || '';
  return {
    id,
    name,
    price: parseAmount(price)
  };
}

/**
 * Parses the provided amount string to a number.
 *
 * @since 1.178.0
 *
 * @param {string} amount The amount string.
 * @return {number} The amount as a number.
 */
function parseAmount(amount) {
  let normalizedNumericPrice = amount.replace(/[^\d.,]/g, '').trim();
  const lastComma = normalizedNumericPrice.lastIndexOf(',');
  const lastDot = normalizedNumericPrice.lastIndexOf('.');
  if (lastComma > -1 && lastDot > -1) {
    // If both comma and dot are present and the last comma is after the last dot,
    // we assume the last comma is the decimal separator - and we remove dots and replace comma with dot.
    // Otherwise, we assume the last dot is the decimal separator and we remove commas.
    if (lastComma > lastDot) {
      normalizedNumericPrice = normalizedNumericPrice.replace(/\./g, '').replace(',', '.');
    } else {
      normalizedNumericPrice = normalizedNumericPrice.replace(/,/g, '');
    }
  } else if (lastComma > -1) {
    const commaDistanceFromEnd = normalizedNumericPrice.length - lastComma;
    // Assume comma is decimal separator if exactly 2 decimal digits follow.
    // Otherwise, remove comma.
    if (commaDistanceFromEnd === 3) {
      normalizedNumericPrice = normalizedNumericPrice.replace(',', '.');
    } else {
      normalizedNumericPrice = normalizedNumericPrice.replace(/,/g, '');
    }
  } else if (lastDot > -1) {
    const dotDistanceFromEnd = normalizedNumericPrice.length - lastDot;
    if (dotDistanceFromEnd !== 3) {
      normalizedNumericPrice = normalizedNumericPrice.replace(/\./g, '');
    }
  }
  return parseFloat(normalizedNumericPrice) || 0;
}
/******/ })()
;
//# sourceMappingURL=googlesitekit-events-provider-easy-digital-downloads.js.map