{
  "version": 3,
  "sources": ["../../../src/template-transforms/globals.ts"],
  "sourcesContent": ["import {assert} from '@code-like-a-carpenter/assert';\n\nimport {isDispatcherStack, isHandlerStack} from '../stacks/index.ts';\nimport type {ServerlessApplicationModel} from '../types.ts';\n\ntype NestedStackType = 'cdc' | 'dispatcher';\ntype NestedStackTemplates = Record<NestedStackType, ServerlessApplicationModel>;\ntype Transform = (\n  template: ServerlessApplicationModel,\n  nestedTemplates: NestedStackTemplates\n) => void | Promise<void>;\n\nexport const globalsTransform: Transform = (template, nestedTemplates) => {\n  Object.entries(template.Resources)\n    .filter(\n      ([resourceName, resource]) =>\n        resource.Type === 'AWS::Serverless::Application' &&\n        (isDispatcherStack(resourceName) || isHandlerStack(resourceName))\n    )\n    .forEach(([, resource]) => {\n      assert(\n        resource.Type === 'AWS::Serverless::Application',\n        'Resource must be a Serverless Application'\n      );\n      // Find any Refs in Globals...\n      const refs = findRefs(template.Globals ?? {});\n\n      // ...and add them to the Parameters of the nested stack\n      Object.values(nestedTemplates).forEach((tpl) => {\n        tpl.Parameters = tpl.Parameters ?? {};\n        for (const ref of refs) {\n          tpl.Parameters[ref] = {\n            Type: 'String',\n          };\n        }\n\n        // Copy the Globals into the nested stack\n        tpl.Globals = template.Globals;\n      });\n\n      // Add the parameters to the resource\n      assert(resource.Properties, 'Resource must have Properties');\n      resource.Properties.Parameters = resource.Properties.Parameters ?? {};\n      const Parameters = resource.Properties.Parameters as Record<\n        string,\n        string\n      >;\n\n      for (const ref of refs) {\n        Parameters[ref] = `\\${${ref}}`;\n      }\n    });\n};\n\nfunction findRefs(obj: object): readonly string[] {\n  return Object.entries(obj).reduce((acc, [key, value]) => {\n    if (typeof value === 'object' && value !== null && !Array.isArray(value)) {\n      return acc.concat(findRefs(value));\n    }\n    if (key === 'Ref') {\n      assert(typeof value === 'string', 'Ref value must be a string');\n      return acc.concat([value]);\n    }\n    return acc;\n  }, [] as string[]);\n}\n"],
  "mappings": ";AAAA,SAAQ,cAAa;AAEr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