import type { Factory, ObjectShape, CallExpressionShape, Record } from '@servicenow/sdk-build-core' import { recordPage } from './templates' import { macroponentConfiguration } from './screen' import { DEFAULTS, TABLES } from './constants' import { getExplicitIdGenerator } from './fluent-utils' const { extendedFrom, category, layoutModel, properties, internalEventMappings, handledEvents, data, composition } = recordPage export async function createPage({ factory, type, arg, callExpressionShape, appConfig, }: { factory: Factory type: 'list' | 'record' | 'home' | 'simple-list' arg: ObjectShape callExpressionShape: CallExpressionShape appConfig: Record }) { const { NAME, DESCRIPTION = '', MACROPONENT, PARENT_MACROPONENT, ORDER = 0, SCREEN_CONDITION = '', ROUTE_TYPE, FIELDS = '', OPTIONAL_PARAMETERS = '', } = DEFAULTS[type] const explicitIdGenerator = getExplicitIdGenerator(callExpressionShape, arg.get('$id').getValue() as string) const screenType = await factory.createRecord({ source: callExpressionShape, table: TABLES.SCREEN_COLLECTION, explicitId: explicitIdGenerator(TABLES.SCREEN_COLLECTION, type), properties: arg.transform(({ $ }) => ({ name: $.val(NAME), description: $.val(DESCRIPTION), })), }) const route = await factory.createRecord({ source: callExpressionShape, table: TABLES.ROUTE, explicitId: explicitIdGenerator(TABLES.ROUTE, type), properties: arg.transform(({ $ }) => ({ name: $.val(NAME), description: $.val(DESCRIPTION), order: $.val(ORDER), fields: $.val(FIELDS), optional_parameters: $.val(OPTIONAL_PARAMETERS), route_type: $.val(ROUTE_TYPE), parent_macroponent: $.val(PARENT_MACROPONENT), app_config: $.val(appConfig), screen_type: $.val(screenType), parent_macroponent_composition_element_id: $.val(''), extension_point: $.val(''), interoperable: $.val(false), icon: $.val(''), })), }) const records = [screenType, route] let macroponent: Record if (DEFAULTS.record.ROUTE_TYPE === type) { macroponent = await factory.createRecord({ source: callExpressionShape, table: TABLES.MACROPONENT, explicitId: explicitIdGenerator(TABLES.MACROPONENT, type), properties: arg.transform(({ $ }) => ({ name: $.val(NAME), extends: $.val(extendedFrom), category: $.val(category), layout: $.val(layoutModel), props: $.val(properties), internal_event_mappings: $.val(internalEventMappings), handled_events: $.val(handledEvents), data: $.val(data), composition: $.val(composition), associated_types: $.val(''), bundles: $.val(''), component_dependencies: $.val(''), da_relay_models: $.val(''), description: $.val(''), disable_auto_reflow: $.val(false), dispatched_events: $.val(''), ext_controller_dep: $.val(''), form_factors: $.val(''), interactions: $.val(''), interfaces: $.val(''), macroponent_dependencies: $.val(''), output_prop_mapping: $.val(''), required_translations: $.val(''), root_component: $.val(''), root_component_config: $.val(''), root_component_definition: $.val(''), schema_version: $.val(''), state_persistence_config: $.val(''), state_properties: $.val(''), style_config: $.val(''), })), }) records.push(macroponent) } const screen = await factory.createRecord({ source: callExpressionShape, table: TABLES.SCREEN, explicitId: explicitIdGenerator(TABLES.SCREEN, type), properties: arg.transform(({ $ }) => ({ active: $.val(true), name: $.val(NAME), description: $.val(DESCRIPTION), order: $.val(ORDER), macroponent: $.val(macroponent).def(MACROPONENT), parent_macroponent: $.val(PARENT_MACROPONENT), screen_condition: $.val(SCREEN_CONDITION), screen_type: $.val(screenType), app_config: $.val(appConfig), macroponent_config: $.val(macroponentConfiguration), disable_auto_reflow: $.val(false), disable_interoperable: $.val(false), event_mappings: $.val(''), required_translations: $.val(''), rollback_screen: $.val(''), })), }) records.push(screen) return records }