import {OfferData, useNodesStore} from "./store";
import {Offers} from "./Offers";

export const useOffer = <Options extends object = {}>(id: OfferData['id']): OfferData<Options> | undefined => {
    const offers: { current: Offers | undefined } = {
        current: undefined
    }

    useNodesStore(store => {
        const _offers = new Offers(store)

        offers.current = _offers

        // this will trigger a re-render when the testable data changes
        return _offers.get(id)
    })

    if (!offers.current) {
        console.error('useOffer: offer not found', id);
        return;
    }

    return offers.current.get(id) as OfferData<Options>;
}