import { defineComponent, withApiName, validateCustomApiName, type ComponentDefinition } from './component'; import { CustomObjectAction, type CustomObjectActionProps } from '../custom-object-action'; import { manifestObject, type ObjectReference } from './custom-object'; export type DefineActionProps = Omit & { apiName: `${string}__c`; }; export function defineAction(object: ObjectReference, props: DefineActionProps): CustomObjectAction { validateCustomApiName(props.apiName); const action = withApiName( new CustomObjectAction(manifestObject(object), { ...props, actionId: props.apiName }), props.apiName, ); currentFunction.set(action, props.function); const setFunction = action.setFunction.bind(action); action.setFunction = (definition) => { setFunction(definition); currentFunction.set(action, definition); return action; }; return actionComponent.register(action, object, props); } const currentFunction = new WeakMap(); const actionComponent: ComponentDefinition = defineComponent({ type: 'CUSTOM_OBJECT_ACTION', define: defineAction, references: (definition, object) => () => [object, currentFunction.get(definition)!], });