import { getSubgraphSdk } from "../utils/graphql";
import { ItemMetadataType } from "../subgraph";
export async function getBaseMetadataEntityByOfferId(subgraphUrl, offerId, queryVars = {}) {
    const subgraphSdk = getSubgraphSdk(subgraphUrl);
    const { baseMetadataEntity } = await subgraphSdk.getBaseMetadataEntityByIdQuery({
        metadataId: `${offerId}-metadata`,
        ...queryVars
    });
    return baseMetadataEntity;
}
export async function getBaseMetadataEntities(subgraphUrl, queryVars = {}) {
    const subgraphSdk = getSubgraphSdk(subgraphUrl);
    const { baseMetadataEntities = [] } = await subgraphSdk.getBaseMetadataEntitiesQuery({
        ...queryVars
    });
    return baseMetadataEntities;
}
export async function getProductV1MetadataEntityByOfferId(subgraphUrl, offerId, queryVars = {}) {
    const subgraphSdk = getSubgraphSdk(subgraphUrl);
    const { productV1MetadataEntity } = await subgraphSdk.getProductV1MetadataEntityByIdQuery({
        metadataId: `${offerId}-metadata`,
        ...queryVars
    });
    return productV1MetadataEntity;
}
export async function getProductV1MetadataEntities(subgraphUrl, queryVars = {}) {
    const subgraphSdk = getSubgraphSdk(subgraphUrl);
    const { productV1MetadataEntities = [] } = await subgraphSdk.getProductV1MetadataEntitiesQuery({
        ...queryVars
    });
    return productV1MetadataEntities;
}
export async function getBundleMetadataEntityByOfferId(subgraphUrl, offerId, queryVars = {}) {
    const subgraphSdk = getSubgraphSdk(subgraphUrl);
    const { bundleMetadataEntity } = await subgraphSdk.getBundleMetadataEntityByIdQuery({
        metadataId: `${offerId}-metadata`,
        ...queryVars
    });
    return bundleMetadataEntity;
}
export async function getBundleMetadataEntities(subgraphUrl, queryVars = {}) {
    const subgraphSdk = getSubgraphSdk(subgraphUrl);
    const { bundleMetadataEntities = [] } = await subgraphSdk.getBundleMetadataEntitiesQuery({
        ...queryVars
    });
    return bundleMetadataEntities;
}
export async function getProductV1Brands(subgraphUrl, queryVars = {}) {
    const subgraphSdk = getSubgraphSdk(subgraphUrl);
    const { productV1Brands = [] } = await subgraphSdk.getProductV1BrandsQuery({
        ...queryVars
    });
    return productV1Brands;
}
export async function getProductV1Categories(subgraphUrl, queryVars = {}) {
    const subgraphSdk = getSubgraphSdk(subgraphUrl);
    const { productV1Categories = [] } = await subgraphSdk.getProductV1CategoriesQuery({
        ...queryVars
    });
    return productV1Categories;
}
export async function getProductV1Products(subgraphUrl, queryVars = {}) {
    const subgraphSdk = getSubgraphSdk(subgraphUrl);
    const { productV1Products = [] } = await subgraphSdk.getProductV1ProductsQuery({
        ...queryVars
    });
    return productV1Products;
}
export async function getAllProductsWithVariants(subgraphUrl, queryVars = {}) {
    const subgraphSdk = getSubgraphSdk(subgraphUrl);
    const { productV1Products = [] } = await subgraphSdk.getProductV1ProductsWithVariantsQuery({
        ...queryVars
    });
    return productV1Products;
}
export async function getAllProductsWithNotVoidedVariants(subgraphUrl, queryVars = {}) {
    const subgraphSdk = getSubgraphSdk(subgraphUrl);
    const { productV1Products = [] } = await subgraphSdk.getAllProductsWithNotVoidedVariantsQuery({
        ...queryVars
    });
    return productV1Products;
}
export async function getProductWithVariants(subgraphUrl, sellerId, productUuid) {
    // Look for ProductV1MetadataEntities, filtered per productUuid
    const productV1MetadataEntities = await getProductV1MetadataEntities(subgraphUrl, {
        metadataFilter: {
            productUuid,
            offer_: {
                sellerId
            }
        }
    });
    // Look for BundleMetadataEntities, filtered per productUuid
    const bundleMetadataEntities = await getBundleMetadataEntities(subgraphUrl, {
        metadataFilter: {
            productUuids_contains: [productUuid],
            offer_: {
                sellerId
            }
        }
    });
    if (productV1MetadataEntities.length + bundleMetadataEntities.length === 0) {
        return null;
    }
    const { itemsFromBundles, bundleSets } = bundleMetadataEntities.reduce((prev, bundle) => {
        const itemsFromBundle = getProductV1ItemFromBundle(bundle, productUuid);
        if (!prev.bundleSets.has(bundle.bundleUuid)) {
            prev.bundleSets.set(bundle.bundleUuid, []);
        }
        prev.bundleSets.get(bundle.bundleUuid).push({
            bundle,
            variations: itemsFromBundle[0].variations // LIMITATION: in case the same bundle contains several times the same product (same variation or different), only the first variation is returned here
        });
        return {
            itemsFromBundles: [...prev.itemsFromBundles, ...itemsFromBundle],
            bundleSets: prev.bundleSets
        };
    }, {
        itemsFromBundles: [],
        bundleSets: new Map()
    });
    const product = productV1MetadataEntities.length
        ? productV1MetadataEntities[0].product
        : itemsFromBundles[0]?.productV1Item.product;
    const variants = productV1MetadataEntities.map((m) => {
        return {
            offer: m.offer,
            variations: m.variations
        };
    });
    return {
        product, // return the product
        bundleSets, // return all bundles that contain this product, mapped by bundleUuid
        variants // return all variants for this product
    };
}
/** returns the productV1ItemMetadataEntities in a bundle for a given productUuid */
// Note: it may returns several items, in case the BUNDLE contains different variants of
// the same product (or even the same variant several times),
function getProductV1ItemFromBundle(bundle, productUuid) {
    return bundle.items
        .filter((item) => item.type === ItemMetadataType.ITEM_PRODUCT_V1)
        .filter((item) => item.productUuid === productUuid)
        .map((item) => {
        return {
            productV1Item: item,
            offer: bundle.offer,
            variations: item.variations
        };
    });
}
export async function getProductWithVariantsFromOfferId(subgraphUrl, offerId) {
    // Look for ProductV1MetadataEntity, filtered by offerId
    const metadataEntity = await getProductV1MetadataEntityByOfferId(subgraphUrl, offerId);
    if (!metadataEntity) {
        return null;
    }
    return getProductWithVariants(subgraphUrl, metadataEntity.offer.seller.id, metadataEntity.product.uuid);
}
//# sourceMappingURL=subgraph.js.map