import type { IntegrationTriggerType, TriggerConfig, TriggerEvent, } from "@automate.ax/integration-contracts/triggers" import type { IntegrationAccountRequirement, TriggerDefinition, } from "@automate.ax/catalog/authoring" import type { IntegrationAccountReference } from "./integrations" import type { Signal } from "./signal-protocol" import { subscribe } from "./subscribe" type IntegrationTriggerDefinition = TriggerDefinition /** Adds the SDK-owned account reference type without coupling contracts to it. */ type TriggerAccountOptions = TDefinition extends { readonly account: IntegrationAccountRequirement } ? { /** Static project account binding selected for this trigger. */ account?: string | IntegrationAccountReference } : object /** Authoring options inferred from one catalog definition and shared contract. */ export type TriggerOptions = TriggerConfig & TriggerAccountOptions /** Makes factory options optional only when the canonical config has no fields. */ type TriggerArguments = object extends TOptions ? [options?: TOptions] : [options: TOptions] /** Typed trigger function produced from one catalog definition. */ export type TriggerFactory = ( ...args: TriggerArguments> ) => Signal> /** * Creates a public trigger function from its lightweight catalog definition. * * Event and canonical configuration types are resolved by the definition's * stable event type. Pass a mapper only when the public options intentionally * differ from the canonical subscription configuration. * * @param trigger - Client-safe trigger definition from the catalog. */ export function createTrigger< const TDefinition extends IntegrationTriggerDefinition, >(trigger: TDefinition): TriggerFactory /** * Creates a public trigger function with an authoring-to-canonical config map. * * @param trigger - Client-safe trigger definition from the catalog. * @param getConfig - Converts public arguments into canonical configuration. */ export function createTrigger< const TDefinition extends IntegrationTriggerDefinition, TArguments extends unknown[], >( trigger: TDefinition, getConfig: (...args: TArguments) => TriggerOptions, ): (...args: TArguments) => Signal> export function createTrigger( trigger: IntegrationTriggerDefinition, getConfig?: (...args: never[]) => unknown, ) { return (...args: never[]) => { if (getConfig) return subscribe(trigger, getConfig(...args)) return subscribe(trigger, args.length === 0 ? {} : args[0]) } }