/**
 * Interface for a quicklook handler that can export and open a USDZ file.
 * Used as an abstraction layer to break the circular dependency between
 * WebXRButtons and USDZExporter.
 */
export interface IQuicklookHandler {
    objectToExport: any;
    exportAndOpen(): Promise<any>;
}

type QuicklookHandlerFactory = {
    /** Find an existing quicklook handler in the scene */
    find(): IQuicklookHandler | null;
    /** Create a new quicklook handler instance */
    create(): IQuicklookHandler;
};

let _factory: QuicklookHandlerFactory | undefined;

/**
 * Register a factory for creating quicklook handlers.
 * Called by USDZExporter to register itself as the handler.
 */
export function setQuicklookHandlerFactory(factory: QuicklookHandlerFactory) {
    _factory = factory;
}

/**
 * Find an existing quicklook handler in the scene, or create a new one if none exists.
 * @returns A quicklook handler, or null if no factory has been registered.
 */
export function getOrCreateQuicklookHandler(): { readonly handler: IQuicklookHandler, readonly created: boolean } | null {
    if (!_factory) return null;
    const existing = _factory.find();
    if (existing) return { handler: existing, created: false };
    return { handler: _factory.create(), created: true };
}
