var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { get } from './requests';
import state from '../state';
import querystring from 'querystring';
export default function (sku) {
    return __awaiter(this, void 0, void 0, function* () {
        if (state.products[sku])
            return Promise.resolve(state.products[sku]);
        const query = querystring.encode({
            'product.sku': sku,
            'asset.types': ['GLB', 'USDZ'].join(','),
            'product_review.status': 'RUNNING',
        });
        const [customerProduct] = (yield get(`/customers/${state.customer._id}/products/assets?${query}`));
        if (!customerProduct)
            throw new Error('R2U - SKU not found');
        const assets = customerProduct.assets || [];
        const productReviews = customerProduct.product_reviews || [];
        const glbAsset = assets.find((asset) => asset.type === 'GLB');
        const usdzAsset = assets.find((asset) => asset.type === 'USDZ');
        const product = {
            glb: glbAsset ? glbAsset.url : '',
            usdz: usdzAsset ? usdzAsset.url : '',
            isActive: productReviews.length > 0,
            name: customerProduct.name,
            placement: customerProduct.placement || 'floor',
            resize: customerProduct.resize || false,
            sound: customerProduct.soundUrl,
            modelId: customerProduct.modelId,
            manufacturerId: customerProduct.manufacturerId,
            pdpUrl: customerProduct.pdpUrl,
            hdri: customerProduct.hdri,
            exposure: customerProduct.exposure,
            bloom: customerProduct.bloom,
            viewerOptions: customerProduct.viewerOptions || {},
        };
        state.products[sku] = product;
        return product;
    });
}
//# sourceMappingURL=product.js.map