Source: helpers.js

import SKU, { ScentColorPattern, Category } from "@earth-breeze/sku";

/**
 * Wrapper for dataLayer.push that clears the ecommerce object before pushing new data.
 * This is to prevent ecommerce data from leaking between events.
 * @param {Object} data - event data to push to dataLayer
 */
export function dataLayerPush(data) {
    dataLayer.push({ ecommerce: null });
    dataLayer.push(data);
}

export function getItemCategories({ sku: skuStr, shippingIntervalFrequency, shippingIntervalUnitType }) {
    const categories = {
        item_category: "All",
        item_category2: "onetime",
        item_category3: "1 pack",
        item_category4: skuStr
    };

    const isDonation = skuStr && skuStr.slice(0, 3) === "900";
    if (isDonation) {
        categories.item_category = "Donation";
    } else {
        try {
            const sku = new SKU(skuStr);
            categories.item_category = productCategoryFromSKU(sku);
            categories.item_category3 = `${sku.packsOfIndividualSKU} pack`;
        } catch (error) {
            console.error(error);
        }
    }

    const isSubscription = !isNaN(shippingIntervalFrequency) && shippingIntervalUnitType;
    if (isSubscription) {
        categories.item_category2 = "subscription";
        categories.item_category3 = `${shippingIntervalFrequency} ${shippingIntervalUnitType}`;
    }

    return categories;
}

function productCategoryFromSKU({ sheetType, category, scentColorPattern }) {
    if (category === Category.accessories) {
        return "Accessories";
    }
    if (category === Category.bundles) {
        return "Bundles/Kits";
    }
    if (sheetType !== null) {
        return "Eco Sheets";
    }
    if (category === Category.homeCleaning && [ScentColorPattern.lemonCitrus, ScentColorPattern.fragranceFree].includes(scentColorPattern)) {
        return "Dishwasher Tablets";
    }
    return "All";
}

export function getListIdentifiers(id) {
    const storedValue = sessionStorage.getItem("itemListIdentifiers");
    const itemListIdentifiers = storedValue ? JSON.parse(storedValue) : {};
    const defaultIdentifiers = { item_list_id: "", item_list_name: "", index: 0 };
    return itemListIdentifiers[id] || defaultIdentifiers;
}

export function setListIdentifiers({ item_id, item_list_id, item_list_name, index }) {
    const storedValue = sessionStorage.getItem("itemListIdentifiers");
    const itemListIdentifiers = storedValue ? JSON.parse(storedValue) : {};
    itemListIdentifiers[item_id] = { item_list_id, item_list_name, index };
    sessionStorage.setItem("itemListIdentifiers", JSON.stringify(itemListIdentifiers));
}