import {
BuildingBlockConfiguration,
GlobalSidebarTabWithTransformedDynamicForms
} from '@allianz/taly-core/schemas';
import { formatFiles, generateFiles, joinPathFragments, type Tree } from '@nx/devkit';
import { classify } from '../../../../utils/string-utils';
import uniqWith from 'lodash-es/uniqWith';
import { tsNodeText, turnObjectIntoObjectLiteral } from '../shared/ts-utils';
interface BuildingBlockImport {
importName: string;
package: string;
}
export async function addGlobalSidebarContent(
tree: Tree,
options: {
directory?: string;
tabs?: GlobalSidebarTabWithTransformedDynamicForms[];
runtimeMode?: boolean;
showDebugPanel?: boolean;
showDfDebugger?: boolean;
respectDebugToolsService?: boolean;
}
) {
const tabs = options.tabs ?? [];
const allBlocks: BuildingBlockConfiguration[] = tabs.flatMap((tab) => tab.blocks ?? []);
if (!options.runtimeMode && !allBlocks.length) {
return;
}
const blockModuleImports: BuildingBlockImport[] = allBlocks.map((block) => ({
importName: block.importName,
package: block.package
}));
const runtimeModeDefaultImports: BuildingBlockImport[] = options.runtimeMode
? [
{
importName: 'DynamicFormBuildingBlockModule',
package: '@allianz/taly-core/building-blocks'
}
]
: [];
const moduleImports: BuildingBlockImport[] = uniqWith(
[...runtimeModeDefaultImports, ...blockModuleImports],
(a, b) => a.package === b.package && a.importName === b.importName
);
// Render the blocks as a TS array literal,same treatment as page blocks and dynamic-form resources.
const allBlocksLiteral = allBlocks
.map((block) =>
tsNodeText(turnObjectIntoObjectLiteral(block, [`frame-global-sidebar`, block.id]))
)
.join(',\n');
const runtimeMode = options.runtimeMode ?? false;
const tabsWithTemplateRefs = tabs.map((tab) => ({
...tab,
blocks: (tab.blocks ?? []).map((block) => ({
...block,
templateReferenceID: `bb${classify(block.id)}`
}))
}));
generateFiles(tree, joinPathFragments(__dirname, './files'), options.directory ?? './', {
tabs: tabsWithTemplateRefs,
moduleImports,
allBlocksLiteral: `[${allBlocksLiteral}]`,
runtimeMode,
showDebugPanel: !runtimeMode && (options.showDebugPanel ?? false),
showDfDebugger: !runtimeMode && (options.showDfDebugger ?? false),
respectDebugToolsService: !runtimeMode && (options.respectDebugToolsService ?? false)
});
await formatFiles(tree);
}