const AUCTION_TIMEOUT = 1500;
const FAILSAFE_TIMEOUT = 3000;
const requestManager = {
    adserverRequestSent: false,
    otherLibrary: false,
    prebid: false,
};
const slots = [
    {
        id: 'div-gpt-ad-123-0',
        tid: crypto.randomUUID(),
        sizes: [
            [300, 250]
        ]
    }
]
anotherLibraryTag.fetchBids({
    slots: slots.map((slot) => ({
        // Please confirm precise syntax for this next line with other library documentation
        slotID: slot.id,
        sizes: slot.sizes,
        timeout: AUCTION_TIMEOUT
    })),
    function() {
        googletag.cmd.push(function() {
            anotherLibraryTag.setDisplayBids();
            requestManager.otherLibrary = true;
            sendBidsToAdServer();
        });
    }
});
pbjs.que.push(function() {
    pbjs.setConfig({
        debug: true,
    });
    pbjs.requestBids({
        bidsBackHandler: prebidBidsBack,
        timeout: AUCTION_TIMEOUT,
        adUnits: slots.map((slot) => ({
            code: slot.id,
            mediaTypes: {
                banner: {
                    sizes: slot.sizes,
                },
            },
            ortb2Imp: {
                ext: {
                    tid: slot.tid
                }
            },
            bids: [
                {
                    bidder: 'appnexus',
                    params: {
                        placementId: 13144370,
                    },
                },
            ],
        }))
    });
});
// prebid bids returned
function prebidBidsBack() {
    pbjs.initAdserverSet = true;
    googletag.cmd.push(function() {
        if (pbjs.libLoaded) {
            pbjs.que.push(function() {
                pbjs.setTargetingForGPTAsync();
                requestManager.prebid = true;
                sendBidsToAdServer();
            });
        } else {
            googletag.pubads().refresh();
            requestManager.adserverRequestSent = true;
        }
    });
}
// send all bids to GAM
function sendBidsToAdServer(failsafeTimoutReached) {
    if (requestManager.adserverRequestSent === true) return;
    if (failsafeTimoutReached) {
        console.warn("AUCTION FAILSAFE TIMEOUT REACHED");
        googletag.cmd.push(function() {
            googletag.pubads().refresh();
        });
        requestManager.adserverRequestSent = true;
        return;
    }
    if (requestManager.otherLibrary && requestManager.prebid) {
        googletag.cmd.push(function() {
            googletag.pubads().refresh();
        });
        requestManager.adserverRequestSent = true;
    }
}
setTimeout(function() {
    sendBidsToAdServer(true);
}, FAILSAFE_TIMEOUT);