import { defineComponent, type ComponentDefinition } from './component'; import { manifestObject, type ObjectReference, type ObjectScoped } from './custom-object'; import type { CustomObjectPageLayout } from '../custom-object-page-layout'; export interface ObjectSettingsProps { allowAttachments?: boolean; allowComments?: boolean; isSearchable?: boolean; defaultLayout?: ObjectScoped< TObject, Pick >; } /** Settings are identified by their parent object, rather than an independent API name. */ export interface DefinedObjectSettings { toDict(): { type: 'CUSTOM_OBJECT_SETTINGS'; setting_id: string; object_rql_name: string; allow_attachments: boolean; allow_comments: boolean; is_searchable: boolean; layout_config: { default_layout: string } | null; }; } export function defineObjectSettings( reference: TObject, props: ObjectSettingsProps>, ): DefinedObjectSettings { const { allowAttachments = false, allowComments = false, isSearchable = false, defaultLayout } = props; const object = manifestObject(reference); if (defaultLayout != null && defaultLayout.getCustomObjectApiName() !== object.getApiName()) { throw new Error('The default layout must belong to the object being configured.'); } return objectSettingsComponent.register( { toDict: () => ({ type: 'CUSTOM_OBJECT_SETTINGS' as const, setting_id: `${object.getApiName()}_settings`, object_rql_name: object.getApiName(), allow_attachments: allowAttachments, allow_comments: allowComments, is_searchable: isSearchable, layout_config: defaultLayout == null ? null : { default_layout: defaultLayout.getLayoutId() }, }), }, reference, props, ); } const objectSettingsComponent: ComponentDefinition = defineComponent({ type: 'CUSTOM_OBJECT_SETTINGS', define: defineObjectSettings, references: (_definition, object, props) => [ object, ...(props.defaultLayout == null ? [] : [props.defaultLayout]), ], });