import { GetGiftWrappingConfigurationComposer, GiftWrappingConfigurationEvent } from '@nitrots/nitro-renderer';
import { UseQueryResult } from '@tanstack/react-query';
import { GiftWrappingConfiguration } from '../../api';
import { useNitroQuery } from '../../api/nitro-query';

/**
 * Wraps the GetGiftWrappingConfigurationComposer / GiftWrappingConfigurationEvent
 * request-response pair as a TanStack query. The configuration is
 * session-stable (box types, ribbon types, colour palette) so the data
 * is cached with `staleTime: Infinity` — the server is asked at most
 * once per session.
 *
 * Replaces the previous pattern in useCatalog where the composer was
 * dispatched eagerly inside an `isVisible` effect and the result was
 * stored in `catalogOptions.giftConfiguration`. Consumers now read the
 * query directly and benefit from the standard TanStack loading/error
 * states.
 */
export const useGiftConfiguration = (
    options: { enabled?: boolean } = {}
): UseQueryResult<GiftWrappingConfiguration> =>
    useNitroQuery<GiftWrappingConfigurationEvent, GiftWrappingConfiguration>({
        key: [ 'nitro', 'catalog', 'giftConfiguration' ],
        request: () => new GetGiftWrappingConfigurationComposer(),
        parser: GiftWrappingConfigurationEvent,
        select: event => new GiftWrappingConfiguration(event.getParser()),
        enabled: options.enabled,
        staleTime: Infinity
    });
