import { INTERACTION_CONTAINER_ID, type MotiPressableInteractionIds, useMotiPressableContext } from './context'; type Id = MotiPressableInteractionIds['id']; type Deps = unknown[] | null | undefined; type Returns = { id: Id; factory: Factory; deps?: Deps }; type HookName = 'useMotiPressableAnimatedProps' | 'useMotiPressable' | 'useMotiPressableTransition'; // biome-ignore lint/suspicious/noExplicitAny: Factory must use any[] — unknown[] breaks the constraint due to parameter contravariance export function useFactory any>( hookName: HookName, factoryOrId: Factory | MotiPressableInteractionIds['id'], maybeFactoryOrDeps?: Factory | Deps, maybeDeps?: Deps, ): Returns { const context = useMotiPressableContext(); let factory: Factory; let id: Id = INTERACTION_CONTAINER_ID; let deps: Deps; if (typeof factoryOrId === 'function') { factory = factoryOrId; // biome-ignore lint/plugin: in this branch the 2nd arg is deps by contract, but its type is still Factory | Deps — a function isn't assignable to Deps and a typeof guard would drop a mis-passed value instead of preserving current behavior deps = maybeFactoryOrDeps as Deps; } else if (typeof maybeFactoryOrDeps === 'function') { id = factoryOrId; factory = maybeFactoryOrDeps; deps = maybeDeps; } else throw new Error( `[${hookName}] Invalid arguments. If the first argument is a unique ID string, the second must be a worklet function. Alternatively, the first argument can be a function, without an ID argument. Received ${String(factoryOrId)} as first argument, and ${String(maybeFactoryOrDeps)} as the second.`, ); if (!context) console.error(`[${hookName}] Missing context. Is this component a child of a component?`); else if (!context.containers[id]) { let error = `[${hookName}] Received id "${id}", but there is no wrapping it.`; const containerKeys = Object.keys(context.containers); if (containerKeys.length) { if (containerKeys.length === 1 && containerKeys[0] === INTERACTION_CONTAINER_ID) error += ` A is present as a parent, but it does not have id="${id}" set.`; else { const possibleIds = containerKeys.filter((k) => k !== INTERACTION_CONTAINER_ID); if (possibleIds.length) error += ` Did you mean one of: ${possibleIds.join(', ')}?`; } } console.error(error); } return { factory, id, deps }; }