@atomic-ehr/codegen 0.0.4-canary.20251217082407.6dae422 → 0.0.4-canary.20251217132509.d94026d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/api/writer-generator/python/requirements.txt +5 -0
- package/assets/api/writer-generator/python/resource_family_validator.py +92 -0
- package/dist/cli/index.js +13 -13
- package/dist/index.d.ts +21 -12
- package/dist/index.js +874 -269
- package/dist/index.js.map +1 -1
- package/package.json +6 -3
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import importlib
|
|
3
|
+
import importlib.util
|
|
4
|
+
from typing import Any, Annotated, List
|
|
5
|
+
|
|
6
|
+
from pydantic import BeforeValidator, BaseModel, ValidationError
|
|
7
|
+
from pydantic_core import ValidationError as PydanticCoreValidationError
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def to_snake_case(name: str) -> str:
|
|
11
|
+
s = re.sub(r"(?<!^)(?=[A-Z])", "_", name)
|
|
12
|
+
return s.lower()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def module_exists(name: str) -> bool:
|
|
16
|
+
"""Checks if a module exists without importing it"""
|
|
17
|
+
return importlib.util.find_spec(name) is not None
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def import_and_create_module(module_name: str, class_name: str) -> Any:
|
|
21
|
+
"""
|
|
22
|
+
Dynamically import a module and create an instance of a specified class.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
module_name: String name of the module (e.g., 'aidbox.hl7_fhir_r4_core.patient')
|
|
26
|
+
class_name: String name of the class (e.g., 'Patient')
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
Instance of the specified class
|
|
30
|
+
"""
|
|
31
|
+
try:
|
|
32
|
+
module = importlib.import_module(module_name)
|
|
33
|
+
class_obj = getattr(module, class_name)
|
|
34
|
+
return class_obj
|
|
35
|
+
|
|
36
|
+
except (ImportError, AttributeError) as e:
|
|
37
|
+
raise ImportError(f"Could not import {class_name} from {module_name}: {e}")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def import_and_create_module_if_exists(package: str, class_name: str) -> Any:
|
|
41
|
+
"""
|
|
42
|
+
Dynamically import a module and create an instance of a specified class if the module exists.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
package: String name of the package (e.g., 'aidbox.hl7_fhir_r4_core')
|
|
46
|
+
class_name: String name of the class (e.g., 'Patient')
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
Instance of the specified class or None if the module does not exist
|
|
50
|
+
"""
|
|
51
|
+
module_name = package + "." + to_snake_case(class_name)
|
|
52
|
+
if module_exists(module_name):
|
|
53
|
+
return import_and_create_module(module_name, class_name)
|
|
54
|
+
else:
|
|
55
|
+
return None
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def validate_and_downcast(
|
|
59
|
+
resource_data: dict[str, Any], package_list: List[str], family: List[str]
|
|
60
|
+
) -> Any:
|
|
61
|
+
"""
|
|
62
|
+
Validates and downcasts ResourceFamily to the appropriate FHIR resource class
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
resource_data: Input value (dict)
|
|
66
|
+
package_list: List of package names to search for resource classes (e.g., ['aidbox.hl7_fhir_r4_core', 'aidbox.hl7_fhir_r4_extras'])
|
|
67
|
+
family: List of valid resource types (e.g., 'Group' or 'Patient')
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
Instance of the appropriate FHIR resource class
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
# Extract and validate resource type
|
|
74
|
+
resource_type = resource_data.get("resourceType")
|
|
75
|
+
if not resource_type:
|
|
76
|
+
raise ValueError("Missing 'resourceType' field in resource")
|
|
77
|
+
|
|
78
|
+
if resource_type not in family:
|
|
79
|
+
raise ValueError(f"Invalid resourceType '{resource_type}'. ")
|
|
80
|
+
|
|
81
|
+
# Dynamically import and instantiate the appropriate class
|
|
82
|
+
target_class = None
|
|
83
|
+
for package in package_list:
|
|
84
|
+
target_class = import_and_create_module_if_exists(package, resource_type)
|
|
85
|
+
if target_class is not None:
|
|
86
|
+
break
|
|
87
|
+
if target_class is None:
|
|
88
|
+
raise ImportError(
|
|
89
|
+
f"Could not find class for resourceType '{resource_type}' in packages {package_list}"
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
return target_class.model_validate(resource_data)
|
package/dist/cli/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import k from'picocolors';import
|
|
2
|
+
import k from'picocolors';import Jr from'yargs';import {hideBin}from'yargs/helpers';import*as R from'fs';import R__default,{existsSync}from'fs';import*as I from'path';import I__default,{dirname,resolve}from'path';import*as _ from'fs/promises';import {mkdir,writeFile,readFile}from'fs/promises';import {CanonicalManager}from'@atomic-ehr/fhir-canonical-manager';import Pt from'assert';import {fileURLToPath}from'url';import*as xt from'yaml';import*as Dt from'@atomic-ehr/fhirschema';import {isStructureDefinition}from'@atomic-ehr/fhirschema';var q=class i{options;dryWarnSet=new Set;constructor(e={}){this.options={timestamp:false,level:1,...e};}shouldLog(e){let t=this.options.level??1;return e>=t}static consoleLevelsMap={1:console.log,2:console.warn,3:console.error,0:console.log,4:()=>{}};formatMessage(e,t,r){let o=this.options.timestamp?`${k.gray(new Date().toLocaleTimeString())} `:"",n=this.options.prefix?`${k.cyan(`[${this.options.prefix}]`)} `:"";return `${o}${r(e)} ${n}${t}`}isSuppressed(e){return this.options.suppressLoggingLevel==="all"||this.options.suppressLoggingLevel?.includes(e)||false}tryWriteToConsole(e,t){if(this.isSuppressed(e)||!this.shouldLog(e))return;(i.consoleLevelsMap[e]||console.log)(t);}success(e){this.tryWriteToConsole(1,this.formatMessage("",e,k.green));}error(e,t){if(this.isSuppressed(3)||!this.shouldLog(3))return;console.error(this.formatMessage("X",e,k.red));let r=this.options.level===0;t&&r&&(console.error(k.red(` ${t.message}`)),t.stack&&console.error(k.gray(t.stack)));}warn(e){this.tryWriteToConsole(2,this.formatMessage("!",e,k.yellow));}dry_warn(e){this.dryWarnSet.has(e)||(this.warn(e),this.dryWarnSet.add(e));}info(e){this.tryWriteToConsole(1,this.formatMessage("i",e,k.blue));}debug(e){this.shouldLog(0)&&this.tryWriteToConsole(0,this.formatMessage("\u{1F41B}",e,k.magenta));}step(e){this.tryWriteToConsole(1,this.formatMessage("\u{1F680}",e,k.cyan));}progress(e){this.tryWriteToConsole(1,this.formatMessage("\u23F3",e,k.blue));}plain(e,t=r=>r){let r=this.options.timestamp?`${k.gray(new Date().toLocaleTimeString())} `:"",o=this.options.prefix?`${k.cyan(`[${this.options.prefix}]`)} `:"";this.tryWriteToConsole(1,`${r}${o}${t(e)}`);}dim(e){this.plain(e,k.gray);}child(e){return new i({...this.options,prefix:this.options.prefix?`${this.options.prefix}:${e}`:e})}configure(e){this.options={...this.options,...e};}getLevel(){return this.options.level??1}setLevel(e){this.options.level=e;}},V=new q;function $e(i){V.success(i);}function F(i,e){V.error(i,e);}function Xe(i){V.warn(i);}function we(i){V.info(i);}function Qe(i){V.step(i);}function Kt(i){V.dim(i);}function Ze(i){V.configure(i);}function O(i={}){return new q(i)}function et(i){console.log(),console.log(k.cyan(k.bold(`\u2501\u2501\u2501 ${i} \u2501\u2501\u2501`)));}function tt(i,e,t){let r=i;e&&(r+=` ${k.gray(`(${e}ms)`)}`),$e(r),t&&Object.entries(t).forEach(([o,n])=>{Kt(` ${o}: ${n}`);});}function K(i,e="\u2022"){i.forEach(t=>{console.log(k.gray(` ${e} ${t}`));});}var ae=i=>i.split(/(?<=[a-z])(?=[A-Z])|[-_.\s]/).filter(Boolean),rt=i=>ae(i).map(e=>e.toLowerCase()).join("-"),it=i=>{if(i.length===0)throw new Error("Empty string");return i[0]?.toUpperCase()+i.substring(1).toLowerCase()},ce=i=>{if(i.length===0)throw new Error("Empty string");let[e,...t]=ae(i);return [e?.toLowerCase(),...t.map(it)].join("")},P=i=>ae(i).map(it).join(""),$=i=>ae(i).map(e=>e.toLowerCase()).join("_"),w=i=>!i||i.length===0?i:i.charAt(0).toUpperCase()+i.slice(1),J=i=>i.map(e=>w(e));var nt=i=>`<${i.identifier.url}> from ${i.identifier.package}#${i.identifier.version}`;var Ee=class{opts;currentDir;currentFile;writtenFilesBuffer={};constructor(e){this.opts=e;}setOutputDir(e){if(this.currentDir)throw new Error("Can't change output dir while writing");this.opts.outputDir=e;}logger(){return this.opts.logger}onDiskMkDir(e){this.opts.inMemoryOnly||R.existsSync(e)||R.mkdirSync(e,{recursive:true});}onDiskOpenFile(e){return this.opts.inMemoryOnly?-1:R.openSync(e,"w")}onDiskCloseFile(e){this.opts.inMemoryOnly||(R.fsyncSync(e),R.closeSync(e));}onDiskWrite(e,t){this.opts.inMemoryOnly||R.writeSync(e,t);}cd(e,t){let r=this.currentDir;this.currentDir=e.startsWith("/")?I.join(this.opts.outputDir,e):I.join(this.currentDir??this.opts.outputDir,e),this.onDiskMkDir(this.currentDir),this.logger()?.debug(`cd '${this.currentDir}'`),t(),this.currentDir=r;}cat(e,t){if(this.currentFile)throw new Error("Can't open file when another file is open");if(e.includes("/"))throw new Error(`Change file path separatly: ${e}`);let r=I.normalize(`${this.currentDir}/${e}`);try{let o=this.onDiskOpenFile(r);this.logger()?.debug(`cat > '${r}'`),this.currentFile={descriptor:o,relPath:r},this.writtenFilesBuffer[this.currentFile.relPath]={relPath:r,absPath:I.resolve(r),tokens:[]},t();}finally{this.currentFile&&this.onDiskCloseFile(this.currentFile.descriptor),this.currentFile=void 0;}}write(e){if(!this.currentFile)throw new Error("No file opened");this.onDiskWrite(this.currentFile.descriptor,e);let t=this.writtenFilesBuffer[this.currentFile.relPath];if(!t)throw new Error("No buffer found");t.tokens.push(e);}writtenFiles(){return Object.values(this.writtenFilesBuffer).map(({relPath:e,absPath:t,tokens:r})=>({relPath:e,absPath:t,content:r.join()})).sort((e,t)=>e.relPath.localeCompare(t.relPath))}},L=class extends Ee{currentIndent=0;indent(){this.currentIndent+=this.opts.tabSize;}deindent(){this.currentIndent-=this.opts.tabSize;}writeIndent(){this.write(" ".repeat(this.currentIndent));}line(...e){e.length===0?this.write(`
|
|
3
3
|
`):(this.writeIndent(),this.write(`${e.join(" ")}
|
|
4
4
|
`));}lineSM(...e){this.writeIndent(),this.write(`${e.join(" ")};
|
|
5
5
|
`);}comment(...e){let t=e.join(" ").split(`
|
|
6
|
-
`);for(let n of t)this.line(this.opts.commentLinePrefix,n);}debugComment(...e){this.opts.withDebugComment&&(e=e.map(t=>typeof t=="string"?t:JSON.stringify(t,null,2)),this.comment(...e));}disclaimer(){return ["WARNING: This file is autogenerated by @atomic-ehr/codegen.","GitHub: https://github.com/atomic-ehr/codegen","Any manual changes made to this file may be overwritten."]}generateDisclaimer(){this.disclaimer().forEach(e=>{this.comment(e);}),this.line();}indentBlock(e){this.indent(),e(),this.deindent();}curlyBlock(e,t,n){this.line(`${e.filter(Boolean).join(" ")} {`),this.indent(),t(),this.deindent(),this.line(`}${n?.filter(Boolean).join(" ")??""}`);}squareBlock(e,t,n){this.line(`${e.filter(Boolean).join(" ")} [`),this.indent(),t(),this.deindent(),this.line(`]${n?.filter(Boolean).join(" ")??""}`);}};var ie=(r,e=true)=>{let t=r.split("/").pop();if(t&&(e&&t.includes("#")&&(t=t.split("#")[0]),!!t))return /^\d/.test(t)&&(t=`number_${t}`),t};var C=r=>`${r.name}#${r.version}`,oe=r=>`${r.name}@${r.version}`;var Ie=(r,e)=>(e||(e={name:"undefined",version:"undefined"}),{...r,package_meta:r.package_meta||e,name:r.name,url:r.url,base:r.base}),q=r=>r?.kind==="primitive-type",D=r=>r?.kind==="nested",et=r=>r?.kind==="complex-type",tt=r=>r?.kind==="profile";var j=r=>r?.identifier.kind==="resource"||r?.identifier.kind==="complex-type"||r?.identifier.kind==="logical",rt=r=>r?.identifier.kind==="complex-type",$=r=>r?.identifier.kind==="resource",nt=r=>r?.identifier.kind==="primitive-type",O=r=>r?.identifier.kind==="logical",se=r=>r?.identifier.kind==="profile";function it(r){return r?.identifier.kind==="binding"}function ot(r){return r?.identifier.kind==="value-set"}var J=r=>r?r.choices===void 0:false,U=r=>r?r.choices!==void 0:false,st=(r,e)=>{if(!r.url)throw new Error("ValueSet must have a URL");if(!r.name)throw new Error("ValueSet must have a name");return {...r,package_meta:r.package_meta||e,name:r.name,url:r.url}};var at={"!":"Not","<=":"LessOrEqual",">=":"GreaterOrEqual","<":"Less",">":"Greater","=":"Equal","-":"Dash","+":"Plus","*":"Asterisk","/":"Slash","%":"Percent","&":"And","|":"Or","^":"Xor","~":"Tilde","?":"Question",".":"Dot"};function jt(r){return r.split("-").map(e=>w(e)).join("-")}function Ut(r){let e=r;for(let t in at)e=e.replaceAll(t,at[t]??"");return e}function At(r){let e=Number(r[0]);return Number.isInteger(e)&&!Number.isNaN(e)?`_${r}`:r}function ct(r){let e=jt(r);return e=At(e),e=Ut(e),e=w(e),e}function P(r){return w(Ye(r.replaceAll(".","-")))}var Ht={boolean:"bool",instant:"string",time:"string",date:"string",dateTime:"string",decimal:"decimal",integer:"int",unsignedInt:"long",positiveInt:"long",integer64:"long",base64Binary:"string",uri:"string",url:"string",canonical:"string",oid:"string",uuid:"string",string:"string",code:"string",markdown:"string",id:"string",xhtml:"string"},_t=["Reference","Expression"],Gt=r=>r.required?["required"]:[],zt=r=>{let e=ut(dt(r.identifier));return w(e)},Wt=r=>r.base?`: ${r.base.name}`:"",qt=(r,e=true)=>{if(!r)return;let t=r.split("/").pop();if(t&&(e&&t.includes("#")&&(t=t.split("#")[0]),!!t))return /^\d/.test(t)&&(t=`number_${t}`),P(t)},dt=r=>{if(r.kind==="nested"){let e=r.url,t=qt(e,false);if(!t)return "";let[n,o]=t.split("#"),i=ne((o??"").split(".")).join("");return P([n,i].join(""))}return P(r.name)},Jt=r=>_t.includes(r),ut=r=>Jt(r)?`Resource${r}`:r,ae=class extends z{enums={};constructor(e){super({tabSize:4,withDebugComment:false,commentLinePrefix:"//",...e});}async generate(e){let t=e.collectComplexTypes(),n=e.collectResources(),o=Array.from(new Set(n.map(i=>P(i.identifier.package))));this.generateAllFiles(t,n,o),this.copyStaticFiles();}generateAllFiles(e,t,n){this.generateUsingFile(n),this.generateBaseTypes(e),this.generateResources(t),this.generateEnumFiles(n),this.generateResourceDictionaries(t,n),this.generateHelperFile();}generateType(e,t){let n=zt(e),o=Wt(e);this.curlyBlock(["public","class",n,o],()=>{this.generateFields(e,t),this.generateNestedTypes(e,t),this.line(),this.includeHelperMethods();}),this.line();}generateFields(e,t){if(!e.fields)return;let n=Object.entries(e.fields).sort(([o],[i])=>o.localeCompare(i));for(let[o,i]of n)this.generateField(o,i,t);}generateNestedTypes(e,t){if(!(!("nested"in e)||!e.nested)){this.line();for(let n of e.nested)this.generateType(n,t);}}generateField(e,t,n){try{if(U(t))return;let o=this.buildFieldDeclaration(e,t,n);this.line(...o);}catch(o){this.logger()?.error(`Error processing field ${e}: ${o.message}`);}}buildFieldDeclaration(e,t,n){let o=this.determineFieldType(e,t,n),i=Gt(t),s=re(e);return ["public",...i,o,s,"{ get; set; }"].filter(Boolean)}determineFieldType(e,t,n){let o=this.getBaseTypeName(t);"enum"in t&&t.enum&&(o=this.registerAndGetEnumType(e,t,n)),o=ut(o);let i="",s=t.required?"":"?",c=t.array?"[]":"";return `${i}${o}${c}${s}`}getBaseTypeName(e){if("type"in e){let t=e.type.name.toString();return e.type.kind==="nested"?t=dt(e.type):e.type.kind==="primitive-type"&&(t=Ht[e.type.name]??"string"),t}return ""}registerAndGetEnumType(e,t,n){let i=`${P(t.binding?.name??e)}Enum`;return this.enums[n]||(this.enums[n]={}),t.enum&&(this.enums[n][i]=t.enum),i}includeHelperMethods(){this.line("public override string ToString() => "),this.line(" JsonSerializer.Serialize(this, Helper.JsonSerializerOptions);"),this.line();}generateUsingFile(e){this.cd("/",async()=>{this.cat("Usings.cs",()=>{this.generateDisclaimer(),this.generateGlobalUsings(e);});});}generateGlobalUsings(e){let t=["CSharpSDK","System.Text.Json","System.Text.Json.Serialization",this.opts.targetNamespace,...e.map(n=>`${this.opts.targetNamespace}.${n}`)];for(let n of t)this.lineSM("global","using",n);}generateBaseTypes(e){this.cd("/",async()=>{this.cat("base.cs",()=>{this.generateDisclaimer(),this.line(),this.lineSM("namespace",this.opts.targetNamespace);for(let t of e){let n=P(t.identifier.package);this.generateType(t,n);}});});}generateResources(e){for(let t of e)this.generateResourceFile(t);}generateResourceFile(e){let t=P(e.identifier.package);this.cd(`/${t}`,async()=>{this.cat(`${e.identifier.name}.cs`,()=>{this.generateDisclaimer(),this.line(),this.lineSM("namespace",`${this.opts.targetNamespace}.${t}`),this.line(),this.generateType(e,t);});});}generateEnumFiles(e){for(let t of e)this.generatePackageEnums(t);}generatePackageEnums(e){let t=this.enums[e];!t||Object.keys(t).length===0||this.cd(`/${e}`,async()=>{this.cat(`${e}Enums.cs`,()=>{this.generateDisclaimer(),this.generateEnumFileContent(e,t);});});}generateEnumFileContent(e,t){this.lineSM("using","System.ComponentModel"),this.line(),this.lineSM(`namespace ${this.opts.targetNamespace}.${e}`);for(let[n,o]of Object.entries(t))this.generateEnum(n,o);}generateEnum(e,t){this.curlyBlock(["public","enum",e],()=>{for(let n of t)this.line(`[Description("${n}")]`),this.line(`${ct(n)},`);}),this.line();}generateResourceDictionaries(e,t){this.cd("/",async()=>{for(let n of t){let o=e.filter(i=>P(i.identifier.package)===n);if(o.length===0)return;this.cat(`${n}ResourceDictionary.cs`,()=>{this.generateDisclaimer(),this.line(),this.lineSM(`namespace ${this.opts.targetNamespace}`),this.generateResourceDictionaryClass(n,o);});}});}generateResourceDictionaryClass(e,t){this.curlyBlock(["public","static","class","ResourceDictionary"],()=>{this.curlyBlock(["public static readonly Dictionary<Type, string> Map = new()"],()=>{for(let n of t){let o=n.identifier.name;this.line(`{ typeof(${e}.${o}), "${o}" },`);}}),this.lineSM();});}copyStaticFiles(){if(!this.opts.staticSourceDir)return;let e=F__default.resolve(this.opts.staticSourceDir);T__default.cpSync(e,this.opts.outputDir,{recursive:true});}generateHelperFile(){let e="src/api/writer-generator/csharp/Helper.cs",t=F__default.join(this.opts.outputDir,"Helper.cs");T__default.copyFileSync(e,t);}};var Fe=r=>r!==null&&typeof r=="object"&&r.resourceType==="CodeSystem";var ce=r=>r!==null&&typeof r=="object"&&r.resourceType==="ValueSet";var Kt=async(r,e)=>{let n=(await r.packageJson(e.name)).dependencies;return n!==void 0?Object.entries(n).map(([o,i])=>({name:o,version:i})):[]},mt=r=>({pkg:r,canonicalResolution:{},fhirSchemas:{},valueSets:{}}),ht=async(r,e,t,n,o)=>{let i=C(e);if(o?.info(`${" ".repeat(t*2)}+ ${i}`),n[i])return n[i];let s=mt(e);for(let a of await r.search({package:e})){let l=a.url;if(!l||!(isStructureDefinition(a)||ce(a)||Fe(a)))continue;let p=l;s.canonicalResolution[p]&&o?.dry_warn(`Duplicate canonical URL: ${p} at ${i}.`),s.canonicalResolution[p]=[{deep:t,pkg:e,pkgId:i,resource:a}];}let c=await Kt(r,e);for(let a of c){let{canonicalResolution:l}=await ht(r,a,t+1,n,o);for(let[p,m]of Object.entries(l)){let y=p;s.canonicalResolution[y]=[...s.canonicalResolution[y]||[],...m];}}for(let a of Object.values(s.canonicalResolution))a.sort((l,p)=>l.deep-p.deep);return n[i]=s,s},ft=(r,e,t)=>{let n=Object.values(r).flatMap(o=>o.canonicalResolution[e]);if(!n)throw new Error(`No canonical resolution found for ${e} in any package`);return n[0]?.resource},K=async(r,{logger:e,fallbackPackageForNameResolution:t,focusedPackages:n})=>{let o=n??await r.packages(),i={};for(let d of o)await ht(r,d,0,i,e);for(let{pkg:d,canonicalResolution:g}of Object.values(i)){let h=C(d);if(!i[h])throw new Error(`Package ${h} not found`);let u=0;e?.info(`FHIR Schema conversion for '${C(d)}' begins...`);for(let[f,S]of Object.entries(g)){let v=S[0];if(!v)throw new Error("Resource not found");let b=v.resource,R=v.pkg;if(isStructureDefinition(b)){let M=Ie(gt.translate(b),R);u++,i[h].fhirSchemas[M.url]=M;}if(ce(b)){let M=st(b,R);i[h].valueSets[M.url]=M;}}e?.info(`FHIR Schema conversion for '${C(d)}' completed: ${u} successful`);}let s=(d,g)=>i[C(d)]?.fhirSchemas[g]||t&&i[C(t)]?.fhirSchemas[g],c=(d,g)=>i[C(d)]?.valueSets[g]||t&&i[C(t)]?.valueSets[g],a=d=>d.match(/^[a-zA-Z0-9]+$/)&&`http://hl7.org/fhir/StructureDefinition/${d}`||d,l=(d,g)=>{let h=s(d,g);if(h===void 0)throw new Error(`Failed to resolve FHIR Schema: '${g}'`);let u=[h];for(;h?.base;){let f=h.package_meta,S=a(h.base);if(h=s(f,S),h===void 0)throw new Error(`Failed to resolve FHIR Schema base for '${g}'. Problem: '${S}' from '${C(f)}'`);u.push(h);}return u};return {...r,testAppendFs(d){let g=Ie(d),h=C(g.package_meta);i[h]||(i[h]=mt(g.package_meta)),i[h].fhirSchemas[g.url]=g;},resolveFs:s,resolveFsGenealogy:l,resolveFsSpecializations:(d,g)=>l(d,g).filter(h=>h.derivation==="specialization"),ensureSpecializationCanonicalUrl:a,resolveSd:(d,g)=>{let h=ft(i,g);if(isStructureDefinition(h))return h},allFs:()=>Object.values(i).flatMap(d=>Object.values(d.fhirSchemas)),allVs:()=>Object.values(i).flatMap(d=>Object.values(d.valueSets)),resolveVs:c,resolveAny:d=>ft(i,d),resolveElementSnapshot:(d,g)=>{let h=l(d.package_meta,d.url),u=Qt(h,g);return Xt(u)},getAllElementKeys:d=>{let g=new Set;for(let[h,u]of Object.entries(d)){g.add(h);for(let f of u?.choices||[])d[f]||g.add(f);}return Array.from(g)},resolver:i,resolutionTree:()=>{let d={};for(let[g,h]of Object.entries(i)){let u=h.pkg.name;d[u]={};for(let[f,S]of Object.entries(h.canonicalResolution)){let v=f;d[u][v]=[];for(let b of S)d[u][v].push({deep:b.deep,pkg:b.pkg});}}return d}}};var Qt=(r,e)=>{let[t,...n]=e;return t===void 0?[]:r.map(o=>{if(!o.elements)return;let i=o.elements?.[t];for(let s of n)i=i?.elements?.[s];return i}).filter(o=>o!==void 0)};function Xt(r){let e=r.reverse(),t=Object.assign({},...e);return t.elements=void 0,t}function $e(r){let e=r.split("|")[0];return e||r}function Yt(r){return r.split("|")[1]}function Zt(r){return r.derivation==="constraint"?"profile":r.kind==="primitive-type"?"primitive-type":r.kind==="complex-type"?"complex-type":r.kind==="resource"?"resource":r.kind==="logical"?"logical":"resource"}function A(r){return {kind:Zt(r),package:r.package_meta.name,version:r.package_meta.version,name:r.name,url:r.url}}var er=r=>{let e=r.split("/"),t=e[e.length-1];return t&&t.length>0?t.split(/[-_]/).map(n=>n.charAt(0).toUpperCase()+n.slice(1).toLowerCase()).join(""):r};function le(r,e,t){let n=$e(t),o=er(n),i={package_meta:{name:"missing_valuesets",version:Yt(n)||"0.0.0"},id:t},s=r.resolveVs(e,n)||i,c=s?.id&&!/^[a-zA-Z0-9_-]{20,}$/.test(s.id)?s.id:o;return {kind:"value-set",package:s.package_meta.name,version:s.package_meta.version,name:c,url:n}}function pe(r,e,t){let n=e.join("."),[o,i,s]=t?[{name:"shared",version:"1.0.0"},t,`urn:fhir:binding:${t}`]:[r.package_meta,`${r.name}.${n}_binding`,`${r.url}#${n}_binding`];return {kind:"binding",package:o.name,version:o.version,name:i,url:s}}function de(r,e,t,n){let o={};if(e.derivation==="constraint"){let a=r.resolveFsSpecializations(e.package_meta,e.url).map(l=>Pe(r,l,n)).filter(l=>l!==void 0).flat();for(let l of a.reverse())o[l.identifier.name]=l.identifier.url;}let i=t.join("."),s=o[i]??`${e.url}#${i}`;return {kind:"nested",package:e.package_meta.name,version:e.package_meta.version,name:i,url:s}}function yt(r,e,t){let n=[];for(let[o,i]of Object.entries(t)){let s=[...e,o];Q(i)&&n.push([s,i]),i.elements&&n.push(...yt(r,s,i.elements));}return n}function tr(r,e,t,n,o){let i={};for(let[s,c]of Object.entries(n)){let a=[...t,s],l=r.resolveElementSnapshot(e,a);Q(l)?i[s]=fe(r,e,a,l,o):i[s]=ue(r,e,a,l,o);}return i}function Pe(r,e,t){if(!e.elements)return;let n=yt(e,[],e.elements).filter(([i,s])=>s.elements&&Object.keys(s.elements).length>0),o=[];for(let[i,s]of n){let c=de(r,e,i,t),a;s.type==="BackboneElement"||!s.type?a="BackboneElement":a=s.type;let l=r.ensureSpecializationCanonicalUrl(a),p=r.resolveFs(e.package_meta,l);if(!p)throw new Error(`Could not resolve base type ${a}`);let m={kind:"complex-type",package:p.package_meta.name,version:p.package_meta.version,name:a,url:l},y=tr(r,e,i,s.elements??{},t),d={identifier:c,base:m,fields:y};o.push(d);}return o.sort((i,s)=>i.identifier.url.localeCompare(s.identifier.url)),o.length===0?void 0:o}function St(r){let e=[];for(let t of r){t.base&&e.push(t.base);for(let n of Object.values(t.fields||{}))"type"in n&&n.type&&e.push(n.type),"binding"in n&&n.binding&&e.push(n.binding);}return e}function bt(r,e,t){let n=t[t.length-1];if(!n)throw new Error(`Internal error: fieldName is missing for path ${t.join("/")}`);let o=t.slice(0,-1),i=r.resolveFsGenealogy(e.package_meta,e.url).flatMap(s=>{if(o.length===0)return s.required||[];if(!s.elements)return [];let c=s;for(let a of o)c=c?.elements?.[a];return c?.required||[]});return new Set(i).has(n)}function Ct(r,e,t){let n=t[t.length-1];if(!n)throw new Error(`Internal error: fieldName is missing for path ${t.join("/")}`);let o=t.slice(0,-1),i=r.resolveFsGenealogy(e.package_meta,e.url).flatMap(s=>{if(o.length===0)return s.excluded||[];if(!s.elements)return [];let c=s;for(let a of o)c=c?.elements?.[a];return c?.excluded||[]});return new Set(i).has(n)}var rr=(r,e,t)=>{if(t.refers)return t.refers.map(n=>{let o=r.ensureSpecializationCanonicalUrl(n),i=r.resolveFs(e.package_meta,o);if(!i)throw new Error(`Failed to resolve fs for ${o}`);return A(i)})};function Ee(r,e,t,n,o){if(n.elementReference){let i=n.elementReference.slice(1).filter((s,c)=>c%2===1);return de(r,e,i,o)}else if(n.type){let i=r.ensureSpecializationCanonicalUrl(n.type),s=r.resolveFs(e.package_meta,i);if(!s)throw new Error(`Could not resolve field type: '${n.type}' (from '${e.url}' in '${C(e.package_meta)}')`);return A(s)}else {if(n.choices)return;if(e.derivation==="constraint")return;throw o?.error(`Can't recognize element type '${e.url}' (${e.derivation}) at '${t.join(".")}': ${JSON.stringify(n,void 0,2)}`),new Error("Unrecognized element type")}}var ue=(r,e,t,n,o)=>{let i,s;n.binding&&(i=pe(e,t,n.binding.bindingName),n.binding.strength==="required"&&n.type==="code"&&(s=Ne(r,e,n,o)));let c=Ee(r,e,t,n,o);return c||o?.warn(`Field type not found for '${e.url}#${t.join(".")}' (${e.derivation})`),{type:c,required:bt(r,e,t),excluded:Ct(r,e,t),reference:rr(r,e,n),array:n.array||false,min:n.min,max:n.max,choices:n.choices,choiceOf:n.choiceOf,binding:i,enum:s}};function Q(r){let e=r.type==="BackboneElement",t=r.type==="Element"&&r.elements!==void 0&&Object.keys(r.elements).length>0,n=r.type===void 0&&r.choiceOf===void 0&&r.elements!==void 0&&Object.keys(r.elements).length>0;return e||t||n}function fe(r,e,t,n,o){return {type:de(r,e,t,o),array:n.array||false,required:bt(r,e,t),excluded:Ct(r,e,t)}}function De(r,e,t,n){let o=$e(t)||t,i=r.resolveVs(e,o);if(i)return ir(r,i)}function ir(r,e,t){if(e.expansion?.contains)return e.expansion.contains.filter(o=>o.code!==void 0).map(o=>(nr(o.code),{code:o.code,display:o.display,system:o.system}));let n=[];if(e.compose?.include){for(let o of e.compose.include)if(o.concept)for(let i of o.concept)n.push({system:o.system,code:i.code,display:i.display});else if(o.system&&!o.filter)try{let i=r.resolveAny(o.system);if(i?.concept){let s=(c,a)=>{for(let l of c)n.push({system:a,code:l.code,display:l.display}),l.concept&&s(l.concept,a);};s(i.concept,o.system);}}catch{}}return n.length>0?n:void 0}var vt=100;function Ne(r,e,t,n){if(!t.binding)return;let o=t.binding.strength,i=t.binding.valueSet;if(!i||!(o==="required"||o==="extensible"&&(t.type==="code"||t.type==="Coding")||o==="preferred"&&(t.type==="code"||t.type==="Coding")))return;let c=De(r,e.package_meta,i);if(!c||c.length===0)return;let a=c.map(l=>l.code).filter(l=>l&&typeof l=="string"&&l.trim().length>0);if(a.length>vt){n?.dry_warn(`Value set ${i} has ${a.length} which is more than ${vt} codes, which may cause issues with code generation.`);return}return a.length>0?a:void 0}function or(r,e,t,n,o){if(!n.binding?.valueSet)return;let i=pe(e,t,n.binding.bindingName),s=Ee(r,e,t,n,o),c=le(r,e.package_meta,n.binding.valueSet),a=[];s&&a.push(s),a.push(c);let l=Ne(r,e,n,o);return {identifier:i,type:s,valueset:c,strength:n.binding.strength,enum:l,dependencies:a}}function kt(r,e,t){let n=new Set;if(!e.elements)return [];let o=[];function i(a,l){for(let[p,m]of Object.entries(a)){let y=[...l,p],d=y.join(".");if(!n.has(d)){if(n.add(d),m.binding){let g=or(r,e,y,m,t);g&&o.push(g);}m.elements&&i(m.elements,y);}}}i(e.elements,[]),o.sort((a,l)=>a.identifier.name.localeCompare(l.identifier.name));let s=[],c=new Set;for(let a of o)c.has(a.identifier.url)||(c.add(a.identifier.url),s.push(a));return s}function sr(r,e,t,n,o){if(!n)return;let i={};for(let s of r.getAllElementKeys(n)){let c=[...t,s],a=r.resolveElementSnapshot(e,c);Q(a)?i[s]=fe(r,e,c,a,o):i[s]=ue(r,e,c,a,o);}return i}function ar(r){let e=[];for(let t of Object.values(r))"type"in t&&t.type&&e.push(t.type),"binding"in t&&t.binding&&e.push(t.binding);return e}function cr(r,e){return !!(r.base==="Extension"||r.base==="http://hl7.org/fhir/StructureDefinition/Extension"||r.url?.includes("/extension/")||r.url?.includes("-extension")||r.name?.toLowerCase().includes("extension")||r.type==="Extension")}async function ge(r,e,t){if(!e.url)throw new Error("ValueSet URL is required");let n=le(r,e.package_meta,e.url),o=De(r,e.package_meta,e.url);return {identifier:n,description:e.description,concept:o,compose:o?void 0:e.compose}}function Oe(r,e,t,n){let o=[];e&&o.push(e),t&&o.push(...ar(t)),n&&o.push(...St(n));let i={};for(let a of o)a.url!==r.url&&(i[a.url]=a);let s=new Set(n?.map(a=>a.identifier.url)),c=Object.values(i).filter(a=>tt(r)||!D(a)?true:!s.has(a.url)).sort((a,l)=>a.url.localeCompare(l.url));return c.length>0?c:void 0}function lr(r,e,t){let n=A(e),o;if(e.base&&e.type!=="Element"){let p=r.resolveFs(e.package_meta,r.ensureSpecializationCanonicalUrl(e.base));if(!p)throw new Error(`Base resource not found '${e.base}' for <${e.url}> from ${C(e.package_meta)}`);o=A(p);}let i=sr(r,e,[],e.elements,t),s=Pe(r,e,t),c=Oe(n,o,i,s),a={identifier:n,base:o,fields:i,nested:s,description:e.description,dependencies:c},l=kt(r,e,t);return [a,...l]}async function me(r,e,t){let n=lr(r,e,t);if(cr(e,A(e))){let o=n[0];if(!o)throw new Error("Expected schema to be defined");o.metadata={isExtension:true};}return n}var Le=r=>{let e={};for(let t of r){let n=t.identifier.package;e[n]||(e[n]=[]),e[n].push(t);}for(let[t,n]of Object.entries(e)){let o={};for(let s of n)o[JSON.stringify(s.identifier)]=s;let i=Object.values(o);i.sort((s,c)=>s.identifier.name.localeCompare(c.identifier.name)),e[t]=i;}return e},pr=(r,e,t)=>{if(r=structuredClone(r),nt(r)||ot(r)||it(r))return r;for(let n of e.ignoreFields??[]){if(r.fields&&!r.fields[n])throw new Error(`Field ${n} not found`);r.fields&&delete r.fields[n];}return r.dependencies=Oe(r.identifier,r.base,r.fields,r.nested),r},Rt=(r,e,{resolutionTree:t,logger:n})=>{let o=[];for(let[c,a]of Object.entries(e))for(let[l,p]of Object.entries(a)){let m=r.resolveByUrl(c,l);if(!m)throw new Error(`Schema not found for ${c} ${l}`);let y=pr(m,p);o.push(y);}let i=(c,a)=>{if(c.length===0)return Object.values(a);for(let p of c)a[JSON.stringify(p.identifier)]=p;let l=[];for(let p of c)if(j(p)){if(!p.dependencies)continue;if(p.dependencies.forEach(m=>{let y=r.resolve(m);if(!y)throw new Error(`Schema not found for ${m}`);let d=JSON.stringify(y.identifier);a[d]||l.push(y);}),p.nested)for(let m of p.nested){if(D(m.identifier))continue;let y=JSON.stringify(m.identifier);a[y]||l.push(m);}}return i(l,a)},s=i(o,{});return Me(s,{resolutionTree:t,logger:n})},dr=r=>{let e=r.filter(i=>$(i)||O(i)),t=[];for(let i of e)i.base&&t.push({parent:i.base,child:i.identifier});let n=[...t],o=i=>{let s=t.filter(a=>a.parent.name===i.name).map(a=>a.child),c=[];for(let a of s)c.push(...o(a));return [...s,...c]};for(let i of t){let s=o(i.child);for(let c of s)t.some(a=>a.parent.name===i.parent.name&&a.child.name===c.name)||n.push({parent:i.parent,child:c});}return n},Me=(r,{resolutionTree:e,logger:t})=>{let n={},o=u=>{let f=u.identifier.url,S=u.identifier.package;if(n[f]||(n[f]={}),n[f][u.identifier.package]&&S!=="shared"){let v=JSON.stringify(u.identifier,void 0,2),b=JSON.stringify(n[f][S]?.identifier,void 0,2);if(v!==b)throw new Error(`Duplicate schema: ${v} and ${b}`);return}n[f][S]=u;};for(let u of r)o(u);let i=dr(r),s=u=>n[u.url]?.[u.package],c=(u,f)=>{if(e){let S=e[u]?.[f]?.[0];if(S)return n[f]?.[S.pkg.name]}return n[f]?.[u]},a=u=>i.filter(f=>f.parent.name===u.name).map(f=>f.child),l=u=>{let f=[],S=u;for(;S;){f.push(S);let v=S.base;if(v===void 0)break;let b=s(v);if(!b){t?.warn(`Failed to resolve base type: ${f.map(R=>`${R.identifier.url} (${R.identifier.kind})`).join(", ")}`);return}S=b;}return f},p=u=>{let f=l(u);if(f===void 0)throw new Error(`Failed to resolve base type: ${u.identifier.url} (${u.identifier.kind})`);return f},m=u=>{let f=p(u).find(S=>S.identifier.kind!=="profile");if(!f)throw new Error(`No non-constraint schema found in hierarchy for: ${u.identifier.name}`);return f};return {_schemaIndex:n,_relations:i,collectComplexTypes:()=>r.filter(rt),collectResources:()=>r.filter($),collectLogicalModels:()=>r.filter(O),collectProfiles:()=>r.filter(se),resolve:s,resolveByUrl:c,resourceChildren:a,tryHierarchy:l,hierarchy:p,findLastSpecialization:m,findLastSpecializationByIdentifier:u=>{let f=s(u);return f?m(f).identifier:u},flatProfile:u=>{let f=p(u),S=f.filter(E=>E.identifier.kind==="profile"),v=f.find(E=>E.identifier.kind!=="profile");if(!v)throw new Error(`No non-constraint schema found in hierarchy for ${u.identifier.name}`);let b={};for(let E of S.slice().reverse()){let ee=E;if(ee.fields)for(let[te,Ge]of Object.entries(ee.fields))b[te]?b[te]={...b[te],...Ge}:b[te]={...Ge};}let R={};for(let E of S.flatMap(ee=>ee.dependencies??[]))R[E.url]=E;let M=Object.values(R);return {...u,base:v.identifier,fields:b,dependencies:M}},isWithMetaField:u=>{let f=l(u);return f?f.filter(j).some(S=>S.fields?.meta!==void 0):false},exportTree:async u=>{let f={};for(let[v,b]of Object.entries(Le(r))){f[v]={"primitive-type":{},"complex-type":{},resource:{},"value-set":{},nested:{},binding:{},profile:{},logical:{}};for(let R of b)f[v][R.identifier.kind][R.identifier.url]={};}let S=u.endsWith(".yaml")?Tt.stringify(f):JSON.stringify(f,void 0,2);await L.mkdir(F.dirname(u),{recursive:true}),await L.writeFile(u,S);}}};var X=class{manager;options;logger;constructor(e={}){this.options={...e},this.manager=e.manager||CanonicalManager({packages:[],workingDir:"tmp/fhir",registry:e.registry||void 0}),this.logger=e.logger||N({prefix:"TypeSchema"});}async registerFromPackageMetas(e){let t=e.map(C);return this.logger?.step(`Loading FHIR packages: ${t.join(", ")}`),await this.manager.init(),K(this.manager,{focusedPackages:e})}generateFhirSchemas(e){this.logger?.progress(`Converting ${e.length} StructureDefinitions to FHIRSchemas`);let t=[],n=0,o=0;for(let i of e)try{let s=gt.translate(i);t.push(s),n++,this.logger?.debug(`Converted StructureDefinition: ${i.name||i.id} (${i.resourceType})`);}catch(s){o++,this.logger?.warn(`Failed to convert StructureDefinition ${i.name||i.id}: ${s instanceof Error?s.message:String(s)}`);}return this.logger?.success(`FHIR Schema conversion completed: ${n}/${e.length} successful, ${o} failed`),t}async generateValueSetSchemas(e,t){e.length>0&&this.logger?.debug(`${e.length} ValueSets available for enum extraction`);let n=await K(this.manager,{logger:this.logger}),o=[];if(e.length>0){this.logger?.progress(`Converting ${e.length} ValueSets to TypeSchema`);let i=0,s=0;for(let c of e)try{let a=await ge(n,c,t);a&&(o.push(a),i++,this.logger?.debug(`Converted ValueSet: ${c.name||c.id}`));}catch(a){s++,this.logger?.warn(`Failed to convert ValueSet ${c.name||c.id}: ${a instanceof Error?a.message:String(a)}`);}this.logger?.success(`ValueSet conversion completed: ${i}/${e.length} successful, ${s} failed`);}return o}async generateFromPackage(e,t,n){let o={name:e,version:t||"latest"},i=await this.registerFromPackageMetas([o]),s=await this.generateValueSetSchemas(i.allVs(),n);return [...(await Promise.all(i.allFs().map(async l=>await me(i,l,n)))).flat(),...s]}};var Y="Use CodeableReference which is not provided by FHIR R4.",fr="Use Availability which is not provided by FHIR R4.",Ft={"hl7.fhir.uv.extensions.r4#1.0.0":{"http://hl7.org/fhir/StructureDefinition/extended-contact-availability":fr,"http://hl7.org/fhir/StructureDefinition/immunization-procedure":Y,"http://hl7.org/fhir/StructureDefinition/specimen-additive":Y,"http://hl7.org/fhir/StructureDefinition/workflow-barrier":Y,"http://hl7.org/fhir/StructureDefinition/workflow-protectiveFactor":Y,"http://hl7.org/fhir/StructureDefinition/workflow-reason":Y},"hl7.fhir.r5.core#5.0.0":{"http://hl7.org/fhir/StructureDefinition/shareablecodesystem":"FIXME: CodeSystem.concept.concept defined by ElementReference. FHIR Schema generator output broken value in it, so we just skip it for now."}},wt=async(r,e)=>{let t=[];for(let n of r.allFs()){let o=C(n.package_meta);if(Ft[o]?.[n.url]){e?.dry_warn(`Skip ${n.url} from ${o}. Reason: ${Ft[o]?.[n.url]}`);continue}t.push(...await me(r,n,e));}for(let n of r.allVs())t.push(await ge(r,n));return t};var gr={boolean:"boolean",instant:"string",time:"string",date:"string",dateTime:"string",decimal:"number",integer:"number",unsignedInt:"number",positiveInt:"number",integer64:"number",base64Binary:"string",uri:"string",url:"string",canonical:"string",oid:"string",uuid:"string",string:"string",code:"string",markdown:"string",id:"string",xhtml:"string"},$t=r=>{let e=gr[r];if(e===void 0)throw new Error(`Unknown primitive type ${r}`);return e},Et=r=>Qe(r),je=r=>r.kind==="profile"?`${x(r)}_profile`:re(r.name),mr=r=>`${je(r)}.ts`,ye=r=>`${Et(r.package)}/${je(r)}`,Be=(r,e=true)=>{if(!r)return;let t=ie(r,e);if(t)return Ve(t)},x=r=>{if(r.kind==="nested"){let e=r.url,t=Be(e,false);if(!t)return "";let[n,o]=t.split("#"),i=ne((o??"").split(".")).join("");return Ve([n,i].join(""))}return Ve(r.name)},Nt=new Set(["class","function","return","if","for","while","const","let","var","import","export","interface"]),W=r=>Nt.has(r)?`"${r}"`:r.includes(" ")||r.includes("-")?`"${r}"`:r,Ve=r=>(Nt.has(r)&&(r=`${r}_`),r.replace(/[- ]/g,"_")),H=(r,e)=>e.startsWith('"')?`${r}[${e}]`:`${r}.${e}`,Pt=r=>`(${r.map(e=>`"${e}"`).join(" | ")})`,Se=class extends z{tsImportType(e,...t){this.lineSM(`import type { ${t.join(", ")} } from "${e}"`);}generateFhirPackageIndexFile(e){this.cat("index.ts",()=>{let t=e.flatMap(n=>[{identifier:n.identifier,tsPackageName:je(n.identifier),resourceName:x(n.identifier),nestedTypes:$(n)&&n.nested||O(n)&&n.nested?n.nested.map(o=>x(o.identifier)):[],helpers:$(n)||O(n)?[`is${x(n.identifier)}`]:[]}]).sort((n,o)=>n.resourceName.localeCompare(o.resourceName));t=Array.from(new Map(t.map(n=>[n.resourceName.toLowerCase(),n])).values()).sort((n,o)=>n.resourceName.localeCompare(o.resourceName));for(let n of t)this.debugComment(n.identifier),this.lineSM(`export type { ${[n.resourceName,...n.nestedTypes].join(", ")} } from "./${n.tsPackageName}"`),n.helpers.length>0&&this.lineSM(`export { ${n.helpers.join(", ")} } from "./${n.tsPackageName}"`);});}generateDependenciesImports(e,t){if(t.dependencies){let n=[],o=[];for(let i of t.dependencies)if(["complex-type","resource","logical"].includes(i.kind))n.push({tsPackage:`../${ye(i)}`,name:w(i.name),dep:i});else if(D(i)){let s={...i};s.name=Be(i.url),n.push({tsPackage:`../${ye(s)}`,name:x(i),dep:i});}else o.push(i);n.sort((i,s)=>i.name.localeCompare(s.name));for(let i of n)this.debugComment(i.dep),this.tsImportType(i.tsPackage,i.name);for(let i of o)this.debugComment("skip:",i);if(this.line(),this.withPrimitiveTypeExtension(t)&&t.identifier.name!=="Element"&&t.dependencies.find(i=>i.name==="Element")===void 0){let i="http://hl7.org/fhir/StructureDefinition/Element",s=e.resolveByUrl(t.identifier.package,i);if(!s)throw new Error(`'${i}' not found for ${t.identifier.package}.`);this.tsImportType(`../${ye(s.identifier)}`,"Element");}}}generateComplexTypeReexports(e){let t=e.dependencies?.filter(et).map(n=>({tsPackage:`../${ye(n)}`,name:w(n.name)}));if(t&&t.length>0){for(let n of t)this.lineSM(`export type { ${n.name} } from "${n.tsPackage}"`);this.line();}}addFieldExtension(e){let t=W(`_${e}`);this.lineSM(`${t}?: Element`);}generateType(e,t){let n;t.identifier.name==="Reference"?n="Reference<T extends string = string>":(t.identifier.kind,n=x(t.identifier));let o;if(t.base&&(o=`extends ${Be(t.base.url)}`),this.debugComment(t.identifier),!t.fields&&!o&&!$(t)){this.lineSM(`export type ${n} = object`);return}this.curlyBlock(["export","interface",n,o],()=>{if($(t)||O(t)){let s=[t.identifier];s.push(...e.resourceChildren(t.identifier));let c=this.opts.openResourceTypeSet&&s.length>1?" | string":"";this.lineSM(`resourceType: ${s.sort((a,l)=>a.name.localeCompare(l.name)).map(a=>`"${a.name}"`).join(" | ")}${c}`),this.line();}if(!t.fields)return;let i=Object.entries(t.fields).sort((s,c)=>s[0].localeCompare(c[0]));for(let[s,c]of i){if(U(c))continue;this.debugComment(s,":",c);let a=W(s),l;c.enum?l=Pt(c.enum):t.identifier.name==="Reference"&&a==="reference"?l="`${T}/${string}`":c.reference&&c.reference.length>0?l=`Reference<${c.reference.map(d=>`"${d.name}"`).join(" | ")}>`:q(c.type)?l=$t(c.type.name):D(c.type)?l=x(c.type):l=c.type.name;let p=c.required?"":"?",m=c.array?"[]":"";this.lineSM(`${a}${p}: ${l}${m}`),this.withPrimitiveTypeExtension(t)&&q(c.type)&&this.addFieldExtension(s);}});}withPrimitiveTypeExtension(e){if(!this.opts.primitiveTypeExtension||!j(e))return false;for(let t of Object.values(e.fields??{}))if(!U(t)&&q(t.type))return true;return false}generateResourceTypePredicate(e){if(!$(e)&&!O(e))return;let t=x(e.identifier);this.curlyBlock(["export","const",`is${t}`,"=",`(resource: unknown): resource is ${t}`,"=>"],()=>{this.lineSM(`return resource !== null && typeof resource === "object" && (resource as {resourceType: string}).resourceType === "${e.identifier.name}"`);});}generateNestedTypes(e,t){if(t.nested)for(let n of t.nested)this.generateType(e,n),this.line();}generateProfileType(e,t){this.debugComment("flatProfile",t);let n=x(t.identifier);this.debugComment("identifier",t.identifier),this.debugComment("base",t.base),this.curlyBlock(["export","interface",n],()=>{this.lineSM(`__profileUrl: "${t.identifier.url}"`),this.line();for(let[o,i]of Object.entries(t.fields??{})){if(U(i))continue;this.debugComment(o,i);let s=W(o),c;if(i.enum)c=Pt(i.enum);else if(i.reference&&i.reference.length>0){let a=e.findLastSpecialization(t);if(!j(a))throw new Error(`Invalid specialization for ${t.identifier}`);let l=a.fields?.[o];if(l===void 0||U(l)||l.reference===void 0)throw new Error(`Invalid field declaration for ${o}`);let p=l.reference.map(y=>y.name),m=i.reference.map(y=>{let d=e.findLastSpecializationByIdentifier(y);return d.name!==y.name?`"${d.name}" /*${y.name}*/`:`'${y.name}'`}).join(" | ");p.length===1&&p[0]==="Resource"&&m!=='"Resource"'?c=`Reference<"Resource" /* ${m} */ >`:c=`Reference<${m}>`;}else if(D(i.type))c=x(i.type);else if(q(i.type))c=$t(i.type.name);else {if(i.type===void 0)throw new Error(`Undefined type for '${o}' field at ${Ze(t)}`);c=i.type.name;}this.lineSM(`${s}${i.required?"":"?"}: ${c}${i.array?"[]":""}`);}}),this.line();}generateAttachProfile(e){let t=x(e.base),n=x(e.identifier),o=Object.entries(e.fields||{}).filter(([i,s])=>s&&J(s)&&s.type!==void 0).map(([i])=>W(i));this.curlyBlock([`export const attach_${n}_to_${t} =`,`(resource: ${t}, profile: ${n}): ${t}`,"=>"],()=>{this.curlyBlock(["return"],()=>{this.line("...resource,"),this.curlyBlock(["meta:"],()=>{this.line(`profile: ['${e.identifier.url}']`);},[","]),o.forEach(i=>{this.line(`${i}: ${H("profile",i)},`);});});}),this.line();}generateExtractProfile(e,t){let n=x(t.base),o=x(t.identifier),i=Object.entries(t.fields||{}).filter(([a,l])=>J(l)&&l.type!==void 0).map(([a])=>a),s=e.findLastSpecialization(t);if(!j(s))throw new Error(`Specialization not found for ${t.identifier.url}`);let c={};this.curlyBlock([`export const extract_${o}_from_${n} =`,`(resource: ${n}): ${o}`,"=>"],()=>{i.forEach(a=>{let l=W(a),p=t.fields?.[a],m=s.fields?.[a];if(!J(p)||!J(m))return;p.required&&!m.required&&this.curlyBlock([`if (${H("resource",l)} === undefined)`],()=>this.lineSM(`throw new Error("'${l}' is required for ${t.identifier.url}")`));let y=p?.reference?.map(g=>g.name),d=m?.reference?.map(g=>g.name);if(y&&d&&y.length!==d.length){let g=`reference_is_valid_${l}`;this.curlyBlock(["const",g,"=","(ref?: Reference)","=>"],()=>{this.line("return !ref"),this.indentBlock(()=>{d.forEach(u=>{this.line(`|| ref.reference?.startsWith('${u}/')`);}),this.line(";");});});let h=p?.required?"":`!${H("resource",l)} || `;p.array?h+=`${H("resource",l)}.every( (ref) => ${g}(ref) )`:h+=`!${g}(${H("resource",l)})`,this.curlyBlock(["if (",h,")"],()=>{this.lineSM(`throw new Error("'${a}' has different references in profile and specialization")`);}),this.line(),c[a]=true;}}),this.curlyBlock(["return"],()=>{this.line(`__profileUrl: '${t.identifier.url}',`),i.forEach(a=>{let l=W(a);c[a]?this.line(`${l}:`,`${H("resource",l)} as ${o}['${l}'],`):this.line(`${l}:`,`${H("resource",l)},`);});});});}generateResourceModule(e,t){this.cat(`${mr(t.identifier)}`,()=>{if(this.generateDisclaimer(),["complex-type","resource","logical"].includes(t.identifier.kind))this.generateDependenciesImports(e,t),this.generateComplexTypeReexports(t),this.generateNestedTypes(e,t),this.comment("CanonicalURL:",t.identifier.url),this.generateType(e,t),this.generateResourceTypePredicate(t);else if(se(t)){let n=e.flatProfile(t);this.generateDependenciesImports(e,n),this.comment("CanonicalURL:",t.identifier.url),this.generateProfileType(e,n),this.generateAttachProfile(n),this.generateExtractProfile(e,n);}else throw new Error(`Profile generation not implemented for kind: ${t.identifier.kind}`)});}async generate(e){let t=[...e.collectComplexTypes(),...e.collectResources(),...e.collectLogicalModels(),...this.opts.generateProfile?e.collectProfiles().filter(o=>e.isWithMetaField(o)):[]],n=Le(t);this.cd("/",()=>{for(let[o,i]of Object.entries(n)){let s=Et(o);this.cd(s,()=>{for(let c of i)this.generateResourceModule(e,c);this.generateFhirPackageIndexFile(i);});}});}};var Dt=r=>{let e=r.replace(/[^a-zA-Z0-9\-_.@#()]/g,"");return e.length===0?"unknown":e},yr=async(r,e)=>{e.info("Cleaning outputs...");try{e.info(`Clean ${r.outputDir}`),T.rmSync(r.outputDir,{recursive:!0,force:!0}),r.typeSchemaOutputDir&&(e.info(`Clean ${r.typeSchemaOutputDir}`),T.rmSync(r.typeSchemaOutputDir,{recursive:!0,force:!0})),r.exportTypeTree&&(e.info(`Clean ${r.exportTypeTree}`),T.rmSync(r.exportTypeTree,{recursive:!0,force:!0}));}catch(t){e.warn(`Error cleaning output directory: ${t instanceof Error?t.message:String(t)}`);}},Sr=async(r,e,t)=>{await L.mkdir(e,{recursive:true}),t.info(`Writing TypeSchema files to ${e}/...`);let n={};for(let o of r){let i={name:o.identifier.package,version:o.identifier.version},s=Dt(C(i)),c=Dt(`${o.identifier.name}(${ie(o.identifier.url)})`),a=JSON.stringify(o,null,2),l=F.join(e,s,c);n[l]||(n[l]=[]),n[l]?.some(p=>p===a)||n[l].push(a);}for(let[o,i]of Object.entries(n))await Promise.all(i.map(async(s,c)=>{let a;c===0?a=`${o}.typeschema.json`:a=`${o}-${c}.typeschema.json`,await L.mkdir(F.dirname(a),{recursive:true}),await L.writeFile(a,s);}));},br=async(r,e,t)=>{t.info(`Writing TypeSchema files to: ${e}`),await L.mkdir(F.dirname(e),{recursive:true}),t.info(`Writing TypeSchemas to one file ${e}...`);for(let n of r){let o=JSON.stringify(n,null,2);await L.appendFile(e,`${o}
|
|
7
|
-
`);}},Cr=async(r,e,t)=>{if(e.typeSchemaOutputDir)try{F.extname(e.typeSchemaOutputDir)===".ndjson"?await br(r,e.typeSchemaOutputDir,t):await Sr(r,e.typeSchemaOutputDir,t),t.info("Writing TypeSchema - DONE");}catch(n){if(t.error("Failed to write TypeSchema output",n instanceof Error?n:new Error(String(n))),e.throwException)throw n}},Z=class{schemas=[];options;generators=new Map;logger;packages=[];localStructurePackages=[];localTgzArchives=[];progressCallback;typeSchemaConfig;constructor(e={}){this.options={outputDir:e.outputDir||"./generated",overwrite:e.overwrite??true,cache:e.cache??true,cleanOutput:e.cleanOutput??true,typeSchemaConfig:e.typeSchemaConfig,manager:e.manager||null,throwException:e.throwException||false,typeSchemaOutputDir:e.typeSchemaOutputDir,exportTypeTree:e.exportTypeTree,treeShake:e.treeShake,registry:e.registry},this.typeSchemaConfig=e.typeSchemaConfig,this.logger=e.logger||N({prefix:"API",level:e.logLevel});}fromPackage(e,t){let n=oe({name:e,version:t||"latest"});return this.packages.push(n),this}fromPackageRef(e){return this.packages.push(e),this}registry(e){return this.options.registry=e,this}localStructureDefinitions(e){return this.localStructurePackages.push(e),this}localTgzPackage(e){return this.localTgzArchives.push(F.resolve(e)),this}fromSchemas(e){return this.logger.debug(`Adding ${e.length} TypeSchemas to generation`),this.schemas=[...this.schemas,...e],this}typescript(e){let o={...{...{logger:this.logger,outputDir:F.join(this.options.outputDir,"/types"),tabSize:4,withDebugComment:false,commentLinePrefix:"//",generateProfile:true},openResourceTypeSet:false,primitiveTypeExtension:true},...Object.fromEntries(Object.entries(e).filter(([s,c])=>c!==void 0))},i=new Se(o);return this.generators.set("typescript",i),this.logger.debug(`Configured TypeScript generator (${JSON.stringify(o,void 0,2)})`),this}csharp(e,t){let n=new ae({outputDir:F.join(this.options.outputDir,"/types"),staticSourceDir:t??void 0,targetNamespace:e,logger:new _({prefix:"C#",timestamp:true,suppressLoggingLevel:[]})});return this.generators.set("C#",n),this.logger.debug("Configured C# generator"),this}onProgress(e){return this.progressCallback=e,this}outputTo(e){this.logger.debug(`Setting output directory: ${e}`),this.options.outputDir=e;for(let t of this.generators.values())t.setOutputDir(e);return this}setLogLevel(e){return this.logger?.setLevel(e),this}throwException(e=true){return this.options.throwException=e,this}cleanOutput(e=true){return this.options.cleanOutput=e,this}writeTypeTree(e){return this.options.exportTypeTree=e,this}treeShake(e){return this.options.treeShake=e,this}writeTypeSchemas(e){return this.options.typeSchemaOutputDir=e,this}async generate(){let e=performance.now(),t={success:false,outputDir:this.options.outputDir,filesGenerated:[],errors:[],warnings:[],duration:0};this.logger.debug(`Starting generation with ${this.generators.size} generators`);try{this.options.cleanOutput&&yr(this.options,this.logger),this.logger.info("Initialize Canonical Manager");let n=this.options.manager||CanonicalManager({packages:this.packages,workingDir:".codegen-cache/canonical-manager-cache",registry:this.options.registry||void 0});if(this.localStructurePackages.length>0)for(let p of this.localStructurePackages)this.logger.info(`Registering local StructureDefinitions for ${p.package.name}@${p.package.version}`),await n.addLocalPackage({name:p.package.name,version:p.package.version,path:p.path,dependencies:p.dependencies?.map(m=>oe(m))});for(let p of this.localTgzArchives)this.logger.info(`Registering local tgz package: ${p}`),await n.addTgzPackage({archivePath:p});let o=await n.init(),i=Object.values(o),s=await K(n,{logger:this.logger,focusedPackages:i}),c=await wt(s,this.logger);await Cr(c,this.options,this.logger);let a={resolutionTree:s.resolutionTree(),logger:this.logger},l=Me(c,a);this.options.treeShake&&(l=Rt(l,this.options.treeShake,a)),this.options.exportTypeTree&&await l.exportTree(this.options.exportTypeTree),this.logger.debug(`Executing ${this.generators.size} generators`),await this.executeGenerators(t,l),this.logger.info("Generation completed successfully"),t.success=t.errors.length===0,this.logger.debug(`Generation completed: ${t.filesGenerated.length} files`);}catch(n){if(this.logger.error("Code generation failed",n instanceof Error?n:new Error(String(n))),t.errors.push(n instanceof Error?n.message:String(n)),this.options.throwException)throw n}return {...t,success:t.errors.length===0,duration:performance.now()-e}}reset(){return this.schemas=[],this.generators.clear(),this.progressCallback=void 0,this.packages=[],this.localStructurePackages=[],this.localTgzArchives=[],this}getSchemas(){return [...this.schemas]}getGenerators(){return Array.from(this.generators.keys())}async executeGenerators(e,t){for(let[n,o]of this.generators.entries()){this.logger.info(`Generating ${n}...`);try{await o.generate(t);let i=o.writtenFiles();e.filesGenerated.push(...i.map(s=>s.absPath)),this.logger.info(`Generating ${n} finished successfully`);}catch(i){if(e.errors.push(`${n} generator failed: ${i instanceof Error?i.message:String(i)}`),this.options.throwException)throw i}}}};var Ue={outputDir:"./generated",verbose:false,overwrite:true,validate:true,cache:true,cleanOutput:true,registry:"",typescript:{moduleFormat:"esm",generateIndex:true,includeDocuments:false,namingConvention:"PascalCase",strictMode:true,includeProfiles:true,includeExtensions:false,includeCodeSystems:false,includeOperations:false,generateValueSets:false,valueSetDirectory:"valuesets",valueSetMode:"required-only",valueSetStrengths:["required"],includeValueSetHelpers:false,fhirVersion:"R4",resourceTypes:[],maxDepth:10,profileOptions:{generateKind:"interface",includeConstraints:true,includeDocumentation:true,strictMode:false,subfolder:"profiles"},generateBuilders:false,builderOptions:{includeValidation:true,includeFactoryMethods:true,includeInterfaces:true,generateNestedBuilders:true,includeHelperMethods:true,supportPartialBuild:true,includeJSDoc:true,generateFactories:true,includeTypeGuards:true,handleChoiceTypes:true,generateArrayHelpers:true},validatorOptions:{includeCardinality:true,includeTypes:true,includeConstraints:true,includeInvariants:false,validateRequired:true,allowAdditional:false,strictValidation:false,collectMetrics:false,generateAssertions:true,generatePartialValidators:true,optimizePerformance:true,includeJSDoc:true,generateCompositeValidators:true},guardOptions:{includeRuntimeValidation:true,includeErrorMessages:true,treeShakeable:true,targetTSVersion:"5.0",strictGuards:false,includeNullChecks:true,verbose:false}},typeSchema:{enablePersistence:true,cacheDir:".typeschema-cache",maxAge:1440*60*1e3,validateCached:true,forceRegenerate:false,shareCache:true,cacheKeyPrefix:"",treeshake:[],singleFile:false,profiles:{autoDetect:true}},packages:[],files:[],$schema:""},Ce=["atomic-codegen.config.ts","atomic-codegen.config","atomic-codegen.config.json",".atomic-codegenrc","atomic-codegen.json",".atomic-codegen.json","codegen.config.json","codegen.json"],Ae=class{validate(e){let t={valid:true,errors:[],warnings:[]};if(!e||typeof e!="object")return t.valid=false,t.errors.push({path:"root",message:"Configuration must be an object",value:e}),t;let n=e;n.outputDir!==void 0&&typeof n.outputDir!="string"&&t.errors.push({path:"outputDir",message:"outputDir must be a string",value:n.outputDir});let o=["verbose","overwrite","validate","cache"];for(let i of o)n[i]!==void 0&&typeof n[i]!="boolean"&&t.errors.push({path:i,message:`${i} must be a boolean`,value:n[i]});if(n.typescript!==void 0){let i=this.validateTypeScriptConfig(n.typescript);t.errors.push(...i);}if(n.typeSchema!==void 0){let i=this.validateTypeSchemaConfig(n.typeSchema);t.errors.push(...i);}return n.packages!==void 0&&(Array.isArray(n.packages)?n.packages.forEach((i,s)=>{typeof i!="string"&&t.errors.push({path:`packages[${s}]`,message:"package name must be a string",value:i});}):t.errors.push({path:"packages",message:"packages must be an array",value:n.packages})),n.files!==void 0&&(Array.isArray(n.files)?n.files.forEach((i,s)=>{typeof i!="string"&&t.errors.push({path:`files[${s}]`,message:"file path must be a string",value:i});}):t.errors.push({path:"files",message:"files must be an array",value:n.files})),t.valid=t.errors.length===0,t.valid&&(t.config=n),t}validateTypeScriptConfig(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript",message:"typescript config must be an object",value:e}),t;let n=e;n.moduleFormat!==void 0&&(["esm","cjs"].includes(n.moduleFormat)||t.push({path:"typescript.moduleFormat",message:'moduleFormat must be "esm" or "cjs"',value:n.moduleFormat})),n.namingConvention!==void 0&&(["PascalCase","camelCase"].includes(n.namingConvention)||t.push({path:"typescript.namingConvention",message:'namingConvention must be "PascalCase" or "camelCase"',value:n.namingConvention}));let o=["generateIndex","includeDocuments","strictMode","includeProfiles","includeExtensions","includeCodeSystems","includeOperations","generateValueSets","includeValueSetHelpers"];for(let i of o)n[i]!==void 0&&typeof n[i]!="boolean"&&t.push({path:`typescript.${i}`,message:`${i} must be a boolean`,value:n[i]});if(n.validatorOptions!==void 0){let i=this.validateValidatorOptions(n.validatorOptions);t.push(...i);}if(n.guardOptions!==void 0){let i=this.validateGuardOptions(n.guardOptions);t.push(...i);}if(n.profileOptions!==void 0){let i=this.validateProfileOptions(n.profileOptions);t.push(...i);}return t}validateValidatorOptions(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript.validatorOptions",message:"validatorOptions must be an object",value:e}),t;let n=e,o=["includeCardinality","includeTypes","includeConstraints","includeInvariants","validateRequired","allowAdditional","strictValidation","collectMetrics","generateAssertions","generatePartialValidators","optimizePerformance","includeJSDoc","generateCompositeValidators"];for(let i of o)n[i]!==void 0&&typeof n[i]!="boolean"&&t.push({path:`typescript.validatorOptions.${i}`,message:`${i} must be a boolean`,value:n[i]});return t}validateGuardOptions(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript.guardOptions",message:"guardOptions must be an object",value:e}),t;let n=e;n.targetTSVersion!==void 0&&(["3.8","4.0","4.5","5.0"].includes(n.targetTSVersion)||t.push({path:"typescript.guardOptions.targetTSVersion",message:'targetTSVersion must be one of: "3.8", "4.0", "4.5", "5.0"',value:n.targetTSVersion}));let o=["includeRuntimeValidation","includeErrorMessages","treeShakeable","strictGuards","includeNullChecks","verbose"];for(let i of o)n[i]!==void 0&&typeof n[i]!="boolean"&&t.push({path:`typescript.guardOptions.${i}`,message:`${i} must be a boolean`,value:n[i]});return t}validateProfileOptions(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript.profileOptions",message:"profileOptions must be an object",value:e}),t;let n=e;n.generateKind!==void 0&&(["interface","type","both"].includes(n.generateKind)||t.push({path:"typescript.profileOptions.generateKind",message:'generateKind must be "interface", "type", or "both"',value:n.generateKind})),n.subfolder!==void 0&&typeof n.subfolder!="string"&&t.push({path:"typescript.profileOptions.subfolder",message:"subfolder must be a string",value:n.subfolder});let o=["includeConstraints","includeDocumentation","strictMode"];for(let i of o)n[i]!==void 0&&typeof n[i]!="boolean"&&t.push({path:`typescript.profileOptions.${i}`,message:`${i} must be a boolean`,value:n[i]});return t}validateTypeSchemaConfig(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typeSchema",message:"typeSchema config must be an object",value:e}),t;let n=e,o=["enablePersistence","validateCached","forceRegenerate","shareCache"];for(let s of o)n[s]!==void 0&&typeof n[s]!="boolean"&&t.push({path:`typeSchema.${s}`,message:`${s} must be a boolean`,value:n[s]});let i=["cacheDir","cacheKeyPrefix"];for(let s of i)n[s]!==void 0&&typeof n[s]!="string"&&t.push({path:`typeSchema.${s}`,message:`${s} must be a string`,value:n[s]});if(n.maxAge!==void 0&&(typeof n.maxAge!="number"||n.maxAge<=0)&&t.push({path:"typeSchema.maxAge",message:"maxAge must be a positive number",value:n.maxAge}),n.profiles!==void 0)if(typeof n.profiles!="object"||n.profiles===null)t.push({path:"typeSchema.profiles",message:"profiles must be an object",value:n.profiles});else {let s=n.profiles;s.autoDetect!==void 0&&typeof s.autoDetect!="boolean"&&t.push({path:"typeSchema.profiles.autoDetect",message:"autoDetect must be a boolean",value:s.autoDetect});}return t}},He=class{validator=new Ae;async autoload(e=process.cwd()){let t=await this.findConfigFile(e);return t?this.loadFromFile(t):{...Ue}}async loadFromFile(e){try{let t;if(e.endsWith(".ts")||e.endsWith("")){let i=await import(resolve(e));t=i.default||i;}else {let o=await readFile(e,"utf-8");t=JSON.parse(o);}let n=this.validator.validate(t);if(!n.valid){let o=n.errors.map(i=>`${i.path}: ${i.message}`).join(`
|
|
6
|
+
`);for(let r of t)this.line(this.opts.commentLinePrefix,r);}debugComment(...e){this.opts.withDebugComment&&(e=e.map(t=>typeof t=="string"?t:JSON.stringify(t,null,2)),this.comment(...e));}disclaimer(){return ["WARNING: This file is autogenerated by @atomic-ehr/codegen.","GitHub: https://github.com/atomic-ehr/codegen","Any manual changes made to this file may be overwritten."]}generateDisclaimer(){this.disclaimer().forEach(e=>{this.comment(e);}),this.line();}indentBlock(e){this.indent(),e(),this.deindent();}curlyBlock(e,t,r){this.line(`${e.filter(Boolean).join(" ")} {`),this.indent(),t(),this.deindent(),this.line(`}${r?.filter(Boolean).join(" ")??""}`);}squareBlock(e,t,r){this.line(`${e.filter(Boolean).join(" ")} [`),this.indent(),t(),this.deindent(),this.line(`]${r?.filter(Boolean).join(" ")??""}`);}};var le=(i,e=true)=>{let t=i.split("/").pop();if(t&&(e&&t.includes("#")&&(t=t.split("#")[0]),!!t))return /^\d/.test(t)&&(t=`number_${t}`),t};var v=i=>`${i.name}#${i.version}`,pe=i=>`${i.name}@${i.version}`;var Ne=(i,e)=>(e||(e={name:"undefined",version:"undefined"}),{...i,package_meta:i.package_meta||e,name:i.name,url:i.url,base:i.base}),Q=i=>i?.kind==="primitive-type",M=i=>i?.kind==="nested",ot=i=>i?.kind==="complex-type",st=i=>i?.kind==="profile";var A=i=>i?.identifier.kind==="resource"||i?.identifier.kind==="complex-type"||i?.identifier.kind==="logical",at=i=>i?.identifier.kind==="complex-type",E=i=>i?.identifier.kind==="resource",ct=i=>i?.identifier.kind==="primitive-type",B=i=>i?.identifier.kind==="logical",de=i=>i?.identifier.kind==="profile";function lt(i){return i?.identifier.kind==="binding"}function pt(i){return i?.identifier.kind==="value-set"}var Z=i=>i?i.choices===void 0:false,H=i=>i?i.choices!==void 0:false,dt=(i,e)=>{if(!i.url)throw new Error("ValueSet must have a URL");if(!i.name)throw new Error("ValueSet must have a name");return {...i,package_meta:i.package_meta||e,name:i.name,url:i.url}};var ut={"!":"Not","<=":"LessOrEqual",">=":"GreaterOrEqual","<":"Less",">":"Greater","=":"Equal","-":"Dash","+":"Plus","*":"Asterisk","/":"Slash","%":"Percent","&":"And","|":"Or","^":"Xor","~":"Tilde","?":"Question",".":"Dot"};function Jt(i){return i.split("-").map(e=>w(e)).join("-")}function Yt(i){let e=i;for(let t in ut)e=e.replaceAll(t,ut[t]??"");return e}function Xt(i){let e=Number(i[0]);return Number.isInteger(e)&&!Number.isNaN(e)?`_${i}`:i}function gt(i){let e=Jt(i);return e=Xt(e),e=Yt(e),e=w(e),e}function N(i){return w(ce(i.replaceAll(".","-")))}var Qt={boolean:"bool",instant:"string",time:"string",date:"string",dateTime:"string",decimal:"decimal",integer:"int",unsignedInt:"long",positiveInt:"long",integer64:"long",base64Binary:"string",uri:"string",url:"string",canonical:"string",oid:"string",uuid:"string",string:"string",code:"string",markdown:"string",id:"string",xhtml:"string"},Zt=["Reference","Expression"],er=i=>i.required?["required"]:[],tr=i=>{let e=yt(ht(i.identifier));return w(e)},rr=i=>i.base?`: ${i.base.name}`:"",ir=(i,e=true)=>{if(!i)return;let t=i.split("/").pop();if(t&&(e&&t.includes("#")&&(t=t.split("#")[0]),!!t))return /^\d/.test(t)&&(t=`number_${t}`),N(t)},ht=i=>{if(i.kind==="nested"){let e=i.url,t=ir(e,false);if(!t)return "";let[r,o]=t.split("#"),n=J((o??"").split(".")).join("");return N([r,n].join(""))}return N(i.name)},nr=i=>Zt.includes(i),yt=i=>nr(i)?`Resource${i}`:i,ue=class extends L{enums={};constructor(e){super({tabSize:4,withDebugComment:false,commentLinePrefix:"//",...e});}async generate(e){let t=e.collectComplexTypes(),r=e.collectResources(),o=Array.from(new Set(r.map(n=>N(n.identifier.package))));this.generateAllFiles(t,r,o),this.copyStaticFiles();}generateAllFiles(e,t,r){this.generateUsingFile(r),this.generateBaseTypes(e),this.generateResources(t),this.generateEnumFiles(r),this.generateResourceDictionaries(t,r),this.generateHelperFile();}generateType(e,t){let r=tr(e),o=rr(e);this.curlyBlock(["public","class",r,o],()=>{this.generateFields(e,t),this.generateNestedTypes(e,t),this.line(),this.includeHelperMethods();}),this.line();}generateFields(e,t){if(!e.fields)return;let r=Object.entries(e.fields).sort(([o],[n])=>o.localeCompare(n));for(let[o,n]of r)this.generateField(o,n,t);}generateNestedTypes(e,t){if(!(!("nested"in e)||!e.nested)){this.line();for(let r of e.nested)this.generateType(r,t);}}generateField(e,t,r){try{if(H(t))return;let o=this.buildFieldDeclaration(e,t,r);this.line(...o);}catch(o){this.logger()?.error(`Error processing field ${e}: ${o.message}`);}}buildFieldDeclaration(e,t,r){let o=this.determineFieldType(e,t,r),n=er(t),s=P(e);return ["public",...n,o,s,"{ get; set; }"].filter(Boolean)}determineFieldType(e,t,r){let o=this.getBaseTypeName(t);"enum"in t&&t.enum&&(o=this.registerAndGetEnumType(e,t,r)),o=yt(o);let n="",s=t.required?"":"?",c=t.array?"[]":"";return `${n}${o}${c}${s}`}getBaseTypeName(e){if("type"in e){let t=e.type.name.toString();return e.type.kind==="nested"?t=ht(e.type):e.type.kind==="primitive-type"&&(t=Qt[e.type.name]??"string"),t}return ""}registerAndGetEnumType(e,t,r){let n=`${N(t.binding?.name??e)}Enum`;return this.enums[r]||(this.enums[r]={}),t.enum&&(this.enums[r][n]=t.enum),n}includeHelperMethods(){this.line("public override string ToString() => "),this.line(" JsonSerializer.Serialize(this, Helper.JsonSerializerOptions);"),this.line();}generateUsingFile(e){this.cd("/",async()=>{this.cat("Usings.cs",()=>{this.generateDisclaimer(),this.generateGlobalUsings(e);});});}generateGlobalUsings(e){let t=["CSharpSDK","System.Text.Json","System.Text.Json.Serialization",this.opts.targetNamespace,...e.map(r=>`${this.opts.targetNamespace}.${r}`)];for(let r of t)this.lineSM("global","using",r);}generateBaseTypes(e){this.cd("/",async()=>{this.cat("base.cs",()=>{this.generateDisclaimer(),this.line(),this.lineSM("namespace",this.opts.targetNamespace);for(let t of e){let r=N(t.identifier.package);this.generateType(t,r);}});});}generateResources(e){for(let t of e)this.generateResourceFile(t);}generateResourceFile(e){let t=N(e.identifier.package);this.cd(`/${t}`,async()=>{this.cat(`${e.identifier.name}.cs`,()=>{this.generateDisclaimer(),this.line(),this.lineSM("namespace",`${this.opts.targetNamespace}.${t}`),this.line(),this.generateType(e,t);});});}generateEnumFiles(e){for(let t of e)this.generatePackageEnums(t);}generatePackageEnums(e){let t=this.enums[e];!t||Object.keys(t).length===0||this.cd(`/${e}`,async()=>{this.cat(`${e}Enums.cs`,()=>{this.generateDisclaimer(),this.generateEnumFileContent(e,t);});});}generateEnumFileContent(e,t){this.lineSM("using","System.ComponentModel"),this.line(),this.lineSM(`namespace ${this.opts.targetNamespace}.${e}`);for(let[r,o]of Object.entries(t))this.generateEnum(r,o);}generateEnum(e,t){this.curlyBlock(["public","enum",e],()=>{for(let r of t)this.line(`[Description("${r}")]`),this.line(`${gt(r)},`);}),this.line();}generateResourceDictionaries(e,t){this.cd("/",async()=>{for(let r of t){let o=e.filter(n=>N(n.identifier.package)===r);if(o.length===0)return;this.cat(`${r}ResourceDictionary.cs`,()=>{this.generateDisclaimer(),this.line(),this.lineSM(`namespace ${this.opts.targetNamespace}`),this.generateResourceDictionaryClass(r,o);});}});}generateResourceDictionaryClass(e,t){this.curlyBlock(["public","static","class","ResourceDictionary"],()=>{this.curlyBlock(["public static readonly Dictionary<Type, string> Map = new()"],()=>{for(let r of t){let o=r.identifier.name;this.line(`{ typeof(${e}.${o}), "${o}" },`);}}),this.lineSM();});}copyStaticFiles(){if(!this.opts.staticSourceDir)return;let e=I__default.resolve(this.opts.staticSourceDir);R__default.cpSync(e,this.opts.outputDir,{recursive:true});}generateHelperFile(){let e="src/api/writer-generator/csharp/Helper.cs",t=I__default.join(this.opts.outputDir,"Helper.cs");R__default.copyFileSync(e,t);}};function De(i){let e=i.split("|")[0];return e||i}function or(i){return i.split("|")[1]}function sr(i){return i.derivation==="constraint"?"profile":i.kind==="primitive-type"?"primitive-type":i.kind==="complex-type"?"complex-type":i.kind==="resource"?"resource":i.kind==="logical"?"logical":"resource"}function G(i){return {kind:sr(i),package:i.package_meta.name,version:i.package_meta.version,name:i.name,url:i.url}}var ar=i=>{let e=i.split("/"),t=e[e.length-1];return t&&t.length>0?t.split(/[-_]/).map(r=>r.charAt(0).toUpperCase()+r.slice(1).toLowerCase()).join(""):i};function ge(i,e,t){let r=De(t),o=ar(r),n={package_meta:{name:"missing_valuesets",version:or(r)||"0.0.0"},id:t},s=i.resolveVs(e,r)||n,c=s?.id&&!/^[a-zA-Z0-9_-]{20,}$/.test(s.id)?s.id:o;return {kind:"value-set",package:s.package_meta.name,version:s.package_meta.version,name:c,url:r}}function fe(i,e,t){let r=e.join("."),[o,n,s]=t?[{name:"shared",version:"1.0.0"},t,`urn:fhir:binding:${t}`]:[i.package_meta,`${i.name}.${r}_binding`,`${i.url}#${r}_binding`];return {kind:"binding",package:o.name,version:o.version,name:n,url:s}}function me(i,e,t,r){let o={};if(e.derivation==="constraint"){let a=i.resolveFsSpecializations(e.package_meta,e.url).map(l=>Oe(i,l,r)).filter(l=>l!==void 0).flat();for(let l of a.reverse())o[l.identifier.name]=l.identifier.url;}let n=t.join("."),s=o[n]??`${e.url}#${n}`;return {kind:"nested",package:e.package_meta.name,version:e.package_meta.version,name:n,url:s}}function St(i,e,t){let r=[];for(let[o,n]of Object.entries(t)){let s=[...e,o];ee(n)&&r.push([s,n]),n.elements&&r.push(...St(i,s,n.elements));}return r}function cr(i,e,t,r,o){let n={};for(let[s,c]of Object.entries(r)){let a=[...t,s],l=i.resolveElementSnapshot(e,a);ee(l)?n[s]=ye(i,e,a,l,o):n[s]=he(i,e,a,l,o);}return n}function Oe(i,e,t){if(!e.elements)return;let r=St(e,[],e.elements).filter(([n,s])=>s.elements&&Object.keys(s.elements).length>0),o=[];for(let[n,s]of r){let c=me(i,e,n,t),a;s.type==="BackboneElement"||!s.type?a="BackboneElement":a=s.type;let l=i.ensureSpecializationCanonicalUrl(a),p=i.resolveFs(e.package_meta,l);if(!p)throw new Error(`Could not resolve base type ${a}`);let m={kind:"complex-type",package:p.package_meta.name,version:p.package_meta.version,name:a,url:l},y=cr(i,e,n,s.elements??{},t),d={identifier:c,base:m,fields:y};o.push(d);}return o.sort((n,s)=>n.identifier.url.localeCompare(s.identifier.url)),o.length===0?void 0:o}function bt(i){let e=[];for(let t of i){t.base&&e.push(t.base);for(let r of Object.values(t.fields||{}))"type"in r&&r.type&&e.push(r.type),"binding"in r&&r.binding&&e.push(r.binding);}return e}function vt(i,e,t){let r=t[t.length-1];if(!r)throw new Error(`Internal error: fieldName is missing for path ${t.join("/")}`);let o=t.slice(0,-1),n=i.resolveFsGenealogy(e.package_meta,e.url).flatMap(s=>{if(o.length===0)return s.required||[];if(!s.elements)return [];let c=s;for(let a of o)c=c?.elements?.[a];return c?.required||[]});return new Set(n).has(r)}function Ct(i,e,t){let r=t[t.length-1];if(!r)throw new Error(`Internal error: fieldName is missing for path ${t.join("/")}`);let o=t.slice(0,-1),n=i.resolveFsGenealogy(e.package_meta,e.url).flatMap(s=>{if(o.length===0)return s.excluded||[];if(!s.elements)return [];let c=s;for(let a of o)c=c?.elements?.[a];return c?.excluded||[]});return new Set(n).has(r)}var lr=(i,e,t)=>{if(t.refers)return t.refers.map(r=>{let o=i.ensureSpecializationCanonicalUrl(r),n=i.resolveFs(e.package_meta,o);if(!n)throw new Error(`Failed to resolve fs for ${o}`);return G(n)})};function Le(i,e,t,r,o){if(r.elementReference){let n=r.elementReference.slice(1).filter((s,c)=>c%2===1);return me(i,e,n,o)}else if(r.type){let n=i.ensureSpecializationCanonicalUrl(r.type),s=i.resolveFs(e.package_meta,n);if(!s)throw new Error(`Could not resolve field type: '${r.type}' (from '${e.url}' in '${v(e.package_meta)}')`);return G(s)}else {if(r.choices)return;if(e.derivation==="constraint")return;throw o?.error(`Can't recognize element type '${e.url}' (${e.derivation}) at '${t.join(".")}': ${JSON.stringify(r,void 0,2)}`),new Error("Unrecognized element type")}}var he=(i,e,t,r,o)=>{let n,s;r.binding&&(n=fe(e,t,r.binding.bindingName),r.binding.strength==="required"&&r.type==="code"&&(s=Me(i,e,r,o)));let c=Le(i,e,t,r,o);return c||o?.warn(`Field type not found for '${e.url}#${t.join(".")}' (${e.derivation})`),{type:c,required:vt(i,e,t),excluded:Ct(i,e,t),reference:lr(i,e,r),array:r.array||false,min:r.min,max:r.max,choices:r.choices,choiceOf:r.choiceOf,binding:n,enum:s}};function ee(i){let e=i.type==="BackboneElement",t=i.type==="Element"&&i.elements!==void 0&&Object.keys(i.elements).length>0,r=i.type===void 0&&i.choiceOf===void 0&&i.elements!==void 0&&Object.keys(i.elements).length>0;return e||t||r}function ye(i,e,t,r,o){return {type:me(i,e,t,o),array:r.array||false,required:vt(i,e,t),excluded:Ct(i,e,t)}}function Be(i,e,t,r){let o=De(t)||t,n=i.resolveVs(e,o);if(n)return dr(i,n)}function dr(i,e,t){if(e.expansion?.contains)return e.expansion.contains.filter(o=>o.code!==void 0).map(o=>(Pt(o.code),{code:o.code,display:o.display,system:o.system}));let r=[];if(e.compose?.include){for(let o of e.compose.include)if(o.concept)for(let n of o.concept)r.push({system:o.system,code:n.code,display:n.display});else if(o.system&&!o.filter)try{let n=i.resolveAny(o.system);if(n?.concept){let s=(c,a)=>{for(let l of c)r.push({system:a,code:l.code,display:l.display}),l.concept&&s(l.concept,a);};s(n.concept,o.system);}}catch{}}return r.length>0?r:void 0}var kt=100;function Me(i,e,t,r){if(!t.binding)return;let o=t.binding.strength,n=t.binding.valueSet;if(!n||!(o==="required"||o==="extensible"&&(t.type==="code"||t.type==="Coding")||o==="preferred"&&(t.type==="code"||t.type==="Coding")))return;let c=Be(i,e.package_meta,n);if(!c||c.length===0)return;let a=c.map(l=>l.code).filter(l=>l&&typeof l=="string"&&l.trim().length>0);if(a.length>kt){r?.dry_warn(`Value set ${n} has ${a.length} which is more than ${kt} codes, which may cause issues with code generation.`);return}return a.length>0?a:void 0}function ur(i,e,t,r,o){if(!r.binding?.valueSet)return;let n=fe(e,t,r.binding.bindingName),s=Le(i,e,t,r,o),c=ge(i,e.package_meta,r.binding.valueSet),a=[];s&&a.push(s),a.push(c);let l=Me(i,e,r,o);return {identifier:n,type:s,valueset:c,strength:r.binding.strength,enum:l,dependencies:a}}function Tt(i,e,t){let r=new Set;if(!e.elements)return [];let o=[];function n(a,l){for(let[p,m]of Object.entries(a)){let y=[...l,p],d=y.join(".");if(!r.has(d)){if(r.add(d),m.binding){let f=ur(i,e,y,m,t);f&&o.push(f);}m.elements&&n(m.elements,y);}}}n(e.elements,[]),o.sort((a,l)=>a.identifier.name.localeCompare(l.identifier.name));let s=[],c=new Set;for(let a of o)c.has(a.identifier.url)||(c.add(a.identifier.url),s.push(a));return s}function gr(i,e,t,r,o){if(!r)return;let n={};for(let s of i.getAllElementKeys(r)){let c=[...t,s],a=i.resolveElementSnapshot(e,c);ee(a)?n[s]=ye(i,e,c,a,o):n[s]=he(i,e,c,a,o);}return n}function fr(i){let e=[];for(let t of Object.values(i))"type"in t&&t.type&&e.push(t.type),"binding"in t&&t.binding&&e.push(t.binding);return e}function mr(i,e){return !!(i.base==="Extension"||i.base==="http://hl7.org/fhir/StructureDefinition/Extension"||i.url?.includes("/extension/")||i.url?.includes("-extension")||i.name?.toLowerCase().includes("extension")||i.type==="Extension")}async function Se(i,e,t){if(!e.url)throw new Error("ValueSet URL is required");let r=ge(i,e.package_meta,e.url),o=Be(i,e.package_meta,e.url);return {identifier:r,description:e.description,concept:o,compose:o?void 0:e.compose}}function _e(i,e,t,r){let o=[];e&&o.push(e),t&&o.push(...fr(t)),r&&o.push(...bt(r));let n={};for(let a of o)a.url!==i.url&&(n[a.url]=a);let s=new Set(r?.map(a=>a.identifier.url)),c=Object.values(n).filter(a=>st(i)||!M(a)?true:!s.has(a.url)).sort((a,l)=>a.url.localeCompare(l.url));return c.length>0?c:void 0}function hr(i,e,t){let r=G(e),o;if(e.base&&e.type!=="Element"){let p=i.resolveFs(e.package_meta,i.ensureSpecializationCanonicalUrl(e.base));if(!p)throw new Error(`Base resource not found '${e.base}' for <${e.url}> from ${v(e.package_meta)}`);o=G(p);}let n=gr(i,e,[],e.elements,t),s=Oe(i,e,t),c=_e(r,o,n,s),a={identifier:r,base:o,fields:n,nested:s,description:e.description,dependencies:c},l=Tt(i,e,t);return [a,...l]}async function be(i,e,t){let r=hr(i,e,t);if(mr(e,G(e))){let o=r[0];if(!o)throw new Error("Expected schema to be defined");o.metadata={isExtension:true};}return r}var z=i=>{let e={};for(let t of i){let r=t.identifier.package;e[r]||(e[r]=[]),e[r].push(t);}for(let[t,r]of Object.entries(e)){let o={};for(let s of r)o[JSON.stringify(s.identifier)]=s;let n=Object.values(o);n.sort((s,c)=>s.identifier.name.localeCompare(c.identifier.name)),e[t]=n;}return e},yr=(i,e,t)=>{if(i=structuredClone(i),ct(i)||pt(i)||lt(i))return i;for(let r of e.ignoreFields??[]){if(i.fields&&!i.fields[r])throw new Error(`Field ${r} not found`);i.fields&&delete i.fields[r];}return i.dependencies=_e(i.identifier,i.base,i.fields,i.nested),i},Ft=(i,e,{resolutionTree:t,logger:r})=>{let o=[];for(let[c,a]of Object.entries(e))for(let[l,p]of Object.entries(a)){let m=i.resolveByUrl(c,l);if(!m)throw new Error(`Schema not found for ${c} ${l}`);let y=yr(m,p);o.push(y);}let n=(c,a)=>{if(c.length===0)return Object.values(a);for(let p of c)a[JSON.stringify(p.identifier)]=p;let l=[];for(let p of c)if(A(p)){if(!p.dependencies)continue;if(p.dependencies.forEach(m=>{let y=i.resolve(m);if(!y)throw new Error(`Schema not found for ${m}`);let d=JSON.stringify(y.identifier);a[d]||l.push(y);}),p.nested)for(let m of p.nested){if(M(m.identifier))continue;let y=JSON.stringify(m.identifier);a[y]||l.push(m);}}return n(l,a)},s=n(o,{});return je(s,{resolutionTree:t,logger:r})},Sr=i=>{let e={};for(let r of i)e[r.identifier.name]=r;let t={};for(let r of i){let o=r.identifier.name,n=r.base?.name;t[o]||(t[o]=[]),n&&e[n]&&t[o].push(n);}return t},br=i=>{let e=[],t={},r={},o=n=>{if(r[n])throw new Error(`Graph has cycles ${n}`);if(!t[n]){r[n]=true;for(let s of i[n]??[])o(s);r[n]=false,t[n]=true,e.push(n);}};for(let n in i)t[n]||o(n);return e},It=i=>{let e=Sr(i);return br(e).map(r=>i.find(o=>o.identifier.name===r)).filter(Boolean)},vr=i=>{let e=i.filter(n=>E(n)||B(n)),t=[];for(let n of e)n.base&&t.push({parent:n.base,child:n.identifier});let r=[...t],o=n=>{let s=t.filter(a=>a.parent.name===n.name).map(a=>a.child),c=[];for(let a of s)c.push(...o(a));return [...s,...c]};for(let n of t){let s=o(n.child);for(let c of s)t.some(a=>a.parent.name===n.parent.name&&a.child.name===c.name)||r.push({parent:n.parent,child:c});}return r},je=(i,{resolutionTree:e,logger:t})=>{let r={},o=u=>{let g=u.identifier.url,S=u.identifier.package;if(r[g]||(r[g]={}),r[g][u.identifier.package]&&S!=="shared"){let C=JSON.stringify(u.identifier,void 0,2),b=JSON.stringify(r[g][S]?.identifier,void 0,2);if(C!==b)throw new Error(`Duplicate schema: ${C} and ${b}`);return}r[g][S]=u;};for(let u of i)o(u);let n=vr(i),s=u=>r[u.url]?.[u.package],c=(u,g)=>{if(e){let S=e[u]?.[g]?.[0];if(S)return r[g]?.[S.pkg.name]}return r[g]?.[u]},a=u=>n.filter(g=>g.parent.name===u.name).map(g=>g.child),l=u=>{let g=[],S=u;for(;S;){g.push(S);let C=S.base;if(C===void 0)break;let b=s(C);if(!b){t?.warn(`Failed to resolve base type: ${g.map(x=>`${x.identifier.url} (${x.identifier.kind})`).join(", ")}`);return}S=b;}return g},p=u=>{let g=l(u);if(g===void 0)throw new Error(`Failed to resolve base type: ${u.identifier.url} (${u.identifier.kind})`);return g},m=u=>{let g=p(u).find(S=>S.identifier.kind!=="profile");if(!g)throw new Error(`No non-constraint schema found in hierarchy for: ${u.identifier.name}`);return g};return {_schemaIndex:r,_relations:n,collectComplexTypes:()=>i.filter(at),collectResources:()=>i.filter(E),collectLogicalModels:()=>i.filter(B),collectProfiles:()=>i.filter(de),resolve:s,resolveByUrl:c,resourceChildren:a,tryHierarchy:l,hierarchy:p,findLastSpecialization:m,findLastSpecializationByIdentifier:u=>{let g=s(u);return g?m(g).identifier:u},flatProfile:u=>{let g=p(u),S=g.filter(D=>D.identifier.kind==="profile"),C=g.find(D=>D.identifier.kind!=="profile");if(!C)throw new Error(`No non-constraint schema found in hierarchy for ${u.identifier.name}`);let b={};for(let D of S.slice().reverse()){let oe=D;if(oe.fields)for(let[se,Ye]of Object.entries(oe.fields))b[se]?b[se]={...b[se],...Ye}:b[se]={...Ye};}let x={};for(let D of S.flatMap(oe=>oe.dependencies??[]))x[D.url]=D;let j=Object.values(x);return {...u,base:C.identifier,fields:b,dependencies:j}},isWithMetaField:u=>{let g=l(u);return g?g.filter(A).some(S=>S.fields?.meta!==void 0):false},exportTree:async u=>{let g={};for(let[C,b]of Object.entries(z(i))){g[C]={"primitive-type":{},"complex-type":{},resource:{},"value-set":{},nested:{},binding:{},profile:{},logical:{}};for(let x of b)g[C][x.identifier.kind][x.identifier.url]={};}let S=u.endsWith(".yaml")?xt.stringify(g):JSON.stringify(g,void 0,2);await _.mkdir(I.dirname(u),{recursive:true}),await _.writeFile(u,S);}}};var kr={boolean:"bool",instant:"str",time:"str",date:"str",dateTime:"str",decimal:"float",integer:"int",unsignedInt:"int",positiveInt:"PositiveInt",integer64:"int",base64Binary:"str",uri:"str",url:"str",canonical:"str",oid:"str",uuid:"str",string:"str",code:"str",markdown:"str",id:"str",xhtml:"str"},Ve={snake_case:$,PascalCase:P,camelCase:ce},Tr=new Set(["False","None","True","and","as","assert","async","await","break","class","continue","def","del","elif","else","except","finally","for","from","global","if","import","in","is","lambda","nonlocal","not","or","pass","raise","return","try","while","with","yield","List"]),wt=100,Rr=i=>Tr.has(i)?`${i}_`:i,xr=i=>i==="Resource"||i==="Element"?["BaseModel"]:[],Fr=(i,e=true)=>{if(!i)return;let t=i.split("/").pop();if(t&&(e&&t.includes("#")&&(t=t.split("#")[0]),!!t))return /^\d/.test(t)&&(t=`number_${t}`),$(t)},Ce=i=>{if(i.kind==="nested"){let e=i.url,t=Fr(e,false);if(!t)return "";let[r,o]=t.split("#"),n=J((o??"").split(".")).join("");return P([r,n].join(""))}return P(i.name)},Et=i=>{let e=I.dirname(fileURLToPath(import.meta.url));return __filename.endsWith("dist/index.js")?I.resolve(e,"..","assets","api","writer-generator","python",i):I.resolve(e,"../../..","assets","api","writer-generator","python",i)},ke=class extends L{nameFormatFunction;tsIndex;constructor(e){super(e),this.nameFormatFunction=this.getFieldFormatFunction(e.fieldFormat);}async generate(e){this.tsIndex=e;let t={groupedComplexTypes:z(e.collectComplexTypes()),groupedResources:z(e.collectResources())};this.generateRootPackages(t),this.generateSDKPackages(t);}generateRootPackages(e){this.generateRootInitFile(e),R__default.cpSync(Et("requirements.txt"),I.resolve(this.opts.outputDir,"requirements.txt"));}generateSDKPackages(e){this.generateComplexTypesPackages(e.groupedComplexTypes),this.generateResourcePackages(e);}generateComplexTypesPackages(e){for(let[t,r]of Object.entries(e))this.cd(`/${$(t)}`,()=>{this.generateBasePy(r);});}generateResourcePackages(e){for(let[t,r]of Object.entries(e.groupedResources))this.cd(`/${$(t)}`,()=>{this.generateResourcePackageContent(t,r,e.groupedComplexTypes[t]||[]);});}generateResourcePackageContent(e,t,r){let o=this.pyFhirPackageByName(e);this.generateResourcePackageInit(o,t,r),this.generateResourceFamilies(t);for(let n of t)this.generateResourceModule(n);}generateRootInitFile(e){this.cd("/",()=>{this.cat("__init__.py",()=>{this.generateDisclaimer();let t=this.collectAndImportAllModels(e);this.generateModelRebuilds(t);});});}collectAndImportAllModels(e){let t=[];for(let r of Object.keys(e.groupedResources)){let o=this.pyFhirPackageByName(r);t.push(...this.importComplexTypes(o,e.groupedComplexTypes[r])),t.push(...this.importResources(o,false,e.groupedResources[r]));}return this.line(),t}generateModelRebuilds(e){for(let t of e.sort())this.line(`${t}.model_rebuild()`);}generateBasePy(e){this.cat("base.py",()=>{this.generateDisclaimer(),this.generateDefaultImports(),this.line(),this.generateComplexTypes(e),this.line();});}generateComplexTypes(e){for(let t of It(e))this.generateNestedTypes(t),this.line(),this.generateType(t);}generateResourcePackageInit(e,t,r){this.cat("__init__.py",()=>{this.generateDisclaimer(),this.importComplexTypes(e,r);let o=this.importResources(e,true,t);this.line(),this.generateExportsDeclaration(r,o);});}importComplexTypes(e,t){if(!t||t.length===0)return [];let r=t.map(o=>o.identifier.name).sort();return this.pyImportFrom(`${e}.base`,...r),this.line(),r}buildImportLine(e,t){let r="";for(;e.length>0&&r.length<t;){let o=e.shift();if(!o)throw new Error("Unexpected empty entity");r.length>0&&(r+=", "),r+=o;}return e.length>0&&(r+=", \\"),r}importResources(e,t,r){if(!r||r.length===0)return [];let o=[];for(let n of r){let s=this.importOneResource(n,e);!t&&!n.fields||o.push(...s);}return o}importOneResource(e,t){let r=`${t}.${$(e.identifier.name)}`,o=this.collectResourceImportNames(e);this.pyImportFrom(r,...o);let n=[...o];if(this.shouldImportResourceFamily(e)){let s=`${e.identifier.name}Family`;this.pyImportFrom(`${t}.resource_families`,s);}return n}collectResourceImportNames(e){let t=[Ce(e.identifier)];for(let r of e.nested??[]){let o=Ce(r.identifier);t.push(o);}return t}shouldImportResourceFamily(e){return Pt(this.tsIndex!==void 0),e.identifier.kind==="resource"&&this.tsIndex.resourceChildren(e.identifier).length>0}generateExportsDeclaration(e,t){this.squareBlock(["__all__","="],()=>{let r=[...(e||[]).map(o=>o.identifier.name),...t].sort();for(let o of r)this.line(`'${o}',`);});}generateResourceModule(e){this.cat(`${$(e.identifier.name)}.py`,()=>{this.generateDisclaimer(),this.generateDefaultImports(),this.line(),this.generateDependenciesImports(e),this.line(),this.generateNestedTypes(e),this.line(),this.generateType(e);});}generateType(e){let t=Ce(e.identifier),r=this.getSuperClasses(e);this.line(`class ${t}(${r.join(", ")}):`),this.indentBlock(()=>{this.generateClassBody(e);}),this.line();}getSuperClasses(e){return [...e.base?[e.base.name]:[],...xr(e.identifier.name)]}generateClassBody(e){if(this.generateModelConfig(),!e.fields){this.line("pass");return}e.identifier.kind==="resource"&&this.generateResourceTypeField(e),this.generateFields(e),e.identifier.kind==="resource"&&this.generateResourceMethods(e);}generateModelConfig(){let e=this.opts.allowExtraFields?"allow":"forbid";this.line(`model_config = ConfigDict(validate_by_name=True, serialize_by_alias=True, extra="${e}")`);}generateResourceTypeField(e){this.line(`${this.nameFormatFunction("resourceType")}: str = Field(`),this.indentBlock(()=>{this.line(`default='${e.identifier.name}',`),this.line("alias='resourceType',"),this.line("serialization_alias='resourceType',"),this.line("frozen=True,"),this.line(`pattern='${e.identifier.name}'`);}),this.line(")");}generateFields(e){let t=Object.entries(e.fields??[]).sort(([r],[o])=>r.localeCompare(o));for(let[r,o]of t){if("choices"in o&&o.choices)continue;let n=this.buildFieldInfo(r,o);this.line(`${n.name}: ${n.type}${n.defaultValue}`);}}buildFieldInfo(e,t){let r=Rr(this.nameFormatFunction(e)),o=this.determineFieldType(t),n=this.getFieldDefaultValue(t,e);return {name:r,type:o,defaultValue:n}}determineFieldType(e){let t=e?this.getBaseFieldType(e):"";return "enum"in e&&e.enum&&(t=`Literal[${e.enum.map(o=>`"${o}"`).join(", ")}]`),e.array&&(t=`PyList[${t}]`),e.required||(t=`${t} | None`),t}getBaseFieldType(e){return "type"in e&&e.type.kind==="resource"?`${e.type.name}Family`:"type"in e&&e.type.kind==="nested"?Ce(e.type):"type"in e&&e.type.kind==="primitive-type"?kr[e.type.name]??"str":"type"in e?e.type.name:""}getFieldDefaultValue(e,t){let r=`alias="${t}", serialization_alias="${t}"`;return e.required?` = Field(${r})`:` = Field(None, ${r})`}generateResourceMethods(e){let t=e.identifier.name.toString();this.line(),this.line("def to_json(self, indent: int | None = None) -> str:"),this.line(" return self.model_dump_json(exclude_unset=True, exclude_none=True, indent=indent)"),this.line(),this.line("@classmethod"),this.line(`def from_json(cls, json: str) -> ${t}:`),this.line(" return cls.model_validate_json(json)");}generateNestedTypes(e){if(e.nested){this.line();for(let t of e.nested)this.generateType(t);}}generateDefaultImports(){this.pyImportFrom("__future__","annotations"),this.pyImportFrom("pydantic","BaseModel","ConfigDict","Field","PositiveInt"),this.pyImportFrom("typing","List as PyList","Literal");}generateDependenciesImports(e){!e.dependencies||e.dependencies.length===0||(this.importComplexTypeDependencies(e.dependencies),this.importResourceDependencies(e.dependencies));}importComplexTypeDependencies(e){let t=e.filter(o=>o.kind==="complex-type"),r=this.groupDependenciesByPackage(t);for(let[o,n]of Object.entries(r))this.pyImportFrom(o,...n.sort());}importResourceDependencies(e){let t=e.filter(r=>r.kind==="resource");for(let r of t){this.pyImportType(r);let o=`${P(r.name)}Family`,n=`${this.pyFhirPackage(r)}.resource_families`;this.pyImportFrom(n,o);}}groupDependenciesByPackage(e){let t={};for(let r of e){let o=this.pyPackage(r);t[o]||(t[o]=[]),t[o].push(r.name);}return t}pyImportFrom(e,...t){let r=`from ${e} import ${t.join(", ")}`;this.shouldUseSingleLineImport(r,t)?this.line(r):this.writeMultiLineImport(e,t);}shouldUseSingleLineImport(e,t){return e.length<=wt||t.length===1}writeMultiLineImport(e,t){this.line(`from ${e} import (\\`),this.indentBlock(()=>{let r=[...t];for(;r.length>0;){let o=this.buildImportLine(r,wt);this.line(o);}}),this.line(")");}pyImportType(e){this.pyImportFrom(this.pyPackage(e),P(e.name));}generateResourceFamilies(e){Pt(this.tsIndex!==void 0);let t=Object.keys(z(e)).map(n=>`${this.opts.rootPackageName}.${n.replaceAll(".","_")}`),r={};for(let n of this.tsIndex.collectResources()){let s=this.tsIndex.resourceChildren(n.identifier).map(c=>c.name);if(s.length>0){let c=`${n.identifier.name}Family`;r[c]=s;}}let o=Object.keys(r);o.length!==0&&this.buildResourceFamiliesFile(t,r,o);}buildResourceFamiliesFile(e,t,r){this.cat("resource_families.py",()=>{this.generateDisclaimer(),this.includeResourceFamilyValidator(),this.line(),this.generateFamilyDefinitions(e,t),this.generateFamilyExports(r);});}includeResourceFamilyValidator(){let e=R__default.readFileSync(Et("resource_family_validator.py"),"utf-8");this.line(e);}generateFamilyDefinitions(e,t){this.line(`packages = [${e.map(r=>`'${r}'`).join(", ")}]`),this.line();for(let[r,o]of Object.entries(t))this.generateFamilyDefinition(r,o);}generateFamilyDefinition(e,t){let r=`${e}_resources`;this.line(`${r} = [${t.map(o=>`'${o}'`).join(", ")}]`),this.line(),this.line(`def validate_and_downcast_${e}(v: Any) -> Any:`),this.line(` return validate_and_downcast(v, packages, ${r})`),this.line(),this.line(`type ${e} = Annotated[Any, BeforeValidator(validate_and_downcast_${e})]`),this.line();}generateFamilyExports(e){this.line(`__all__ = [${e.map(t=>`'${t}'`).join(", ")}]`);}buildPyPackageName(e){return (e?[$(e)]:[""]).join(".")}pyFhirPackage(e){return this.pyFhirPackageByName(e.package)}pyFhirPackageByName(e){return [this.opts.rootPackageName,this.buildPyPackageName(e)].join(".")}pyPackage(e){return e.kind==="complex-type"?`${this.pyFhirPackage(e)}.base`:e.kind==="resource"?[this.pyFhirPackage(e),$(e.name)].join("."):this.pyFhirPackage(e)}getFieldFormatFunction(e){return Ve[e]?Ve[e]:(this.logger()?.warn(`Unknown field format '${e}'. Defaulting to SnakeCase.`),this.logger()?.warn(`Supported formats: ${Object.keys(Ve).join(", ")}`),$)}};var Ue=i=>i!==null&&typeof i=="object"&&i.resourceType==="CodeSystem";var Te=i=>i!==null&&typeof i=="object"&&i.resourceType==="ValueSet";var Ir=async(i,e)=>{let r=(await i.packageJson(e.name)).dependencies;return r!==void 0?Object.entries(r).map(([o,n])=>({name:o,version:n})):[]},Ot=i=>({pkg:i,canonicalResolution:{},fhirSchemas:{},valueSets:{}}),Lt=async(i,e,t,r,o)=>{let n=v(e);if(o?.info(`${" ".repeat(t*2)}+ ${n}`),r[n])return r[n];let s=Ot(e);for(let a of await i.search({package:e})){let l=a.url;if(!l||!(isStructureDefinition(a)||Te(a)||Ue(a)))continue;let p=l;s.canonicalResolution[p]&&o?.dry_warn(`Duplicate canonical URL: ${p} at ${n}.`),s.canonicalResolution[p]=[{deep:t,pkg:e,pkgId:n,resource:a}];}let c=await Ir(i,e);for(let a of c){let{canonicalResolution:l}=await Lt(i,a,t+1,r,o);for(let[p,m]of Object.entries(l)){let y=p;s.canonicalResolution[y]=[...s.canonicalResolution[y]||[],...m];}}for(let a of Object.values(s.canonicalResolution))a.sort((l,p)=>l.deep-p.deep);return r[n]=s,s},Nt=(i,e,t)=>{let r=Object.values(i).flatMap(o=>o.canonicalResolution[e]);if(!r)throw new Error(`No canonical resolution found for ${e} in any package`);return r[0]?.resource},te=async(i,{logger:e,fallbackPackageForNameResolution:t,focusedPackages:r})=>{let o=r??await i.packages(),n={};for(let d of o)await Lt(i,d,0,n,e);for(let{pkg:d,canonicalResolution:f}of Object.values(n)){let h=v(d);if(!n[h])throw new Error(`Package ${h} not found`);let u=0;e?.info(`FHIR Schema conversion for '${v(d)}' begins...`);for(let[g,S]of Object.entries(f)){let C=S[0];if(!C)throw new Error("Resource not found");let b=C.resource,x=C.pkg;if(isStructureDefinition(b)){let j=Ne(Dt.translate(b),x);u++,n[h].fhirSchemas[j.url]=j;}if(Te(b)){let j=dt(b,x);n[h].valueSets[j.url]=j;}}e?.info(`FHIR Schema conversion for '${v(d)}' completed: ${u} successful`);}let s=(d,f)=>n[v(d)]?.fhirSchemas[f]||t&&n[v(t)]?.fhirSchemas[f],c=(d,f)=>n[v(d)]?.valueSets[f]||t&&n[v(t)]?.valueSets[f],a=d=>d.match(/^[a-zA-Z0-9]+$/)&&`http://hl7.org/fhir/StructureDefinition/${d}`||d,l=(d,f)=>{let h=s(d,f);if(h===void 0)throw new Error(`Failed to resolve FHIR Schema: '${f}'`);let u=[h];for(;h?.base;){let g=h.package_meta,S=a(h.base);if(h=s(g,S),h===void 0)throw new Error(`Failed to resolve FHIR Schema base for '${f}'. Problem: '${S}' from '${v(g)}'`);u.push(h);}return u};return {...i,testAppendFs(d){let f=Ne(d),h=v(f.package_meta);n[h]||(n[h]=Ot(f.package_meta)),n[h].fhirSchemas[f.url]=f;},resolveFs:s,resolveFsGenealogy:l,resolveFsSpecializations:(d,f)=>l(d,f).filter(h=>h.derivation==="specialization"),ensureSpecializationCanonicalUrl:a,resolveSd:(d,f)=>{let h=Nt(n,f);if(isStructureDefinition(h))return h},allFs:()=>Object.values(n).flatMap(d=>Object.values(d.fhirSchemas)),allVs:()=>Object.values(n).flatMap(d=>Object.values(d.valueSets)),resolveVs:c,resolveAny:d=>Nt(n,d),resolveElementSnapshot:(d,f)=>{let h=l(d.package_meta,d.url),u=Pr(h,f);return $r(u)},getAllElementKeys:d=>{let f=new Set;for(let[h,u]of Object.entries(d)){f.add(h);for(let g of u?.choices||[])d[g]||f.add(g);}return Array.from(f)},resolver:n,resolutionTree:()=>{let d={};for(let[f,h]of Object.entries(n)){let u=h.pkg.name;d[u]={};for(let[g,S]of Object.entries(h.canonicalResolution)){let C=g;d[u][C]=[];for(let b of S)d[u][C].push({deep:b.deep,pkg:b.pkg});}}return d}}};var Pr=(i,e)=>{let[t,...r]=e;return t===void 0?[]:i.map(o=>{if(!o.elements)return;let n=o.elements?.[t];for(let s of r)n=n?.elements?.[s];return n}).filter(o=>o!==void 0)};function $r(i){let e=i.reverse(),t=Object.assign({},...e);return t.elements=void 0,t}var re=class{manager;options;logger;constructor(e){this.options={...e},this.manager=e.manager,this.logger=e.logger||O({prefix:"TypeSchema"});}async registerFromPackageMetas(e){let t=e.map(v);return this.logger?.step(`Loading FHIR packages: ${t.join(", ")}`),await this.manager.init(),te(this.manager,{focusedPackages:e})}generateFhirSchemas(e){this.logger?.progress(`Converting ${e.length} StructureDefinitions to FHIRSchemas`);let t=[],r=0,o=0;for(let n of e)try{let s=Dt.translate(n);t.push(s),r++,this.logger?.debug(`Converted StructureDefinition: ${n.name||n.id} (${n.resourceType})`);}catch(s){o++,this.logger?.warn(`Failed to convert StructureDefinition ${n.name||n.id}: ${s instanceof Error?s.message:String(s)}`);}return this.logger?.success(`FHIR Schema conversion completed: ${r}/${e.length} successful, ${o} failed`),t}async generateValueSetSchemas(e,t){e.length>0&&this.logger?.debug(`${e.length} ValueSets available for enum extraction`);let r=await te(this.manager,{logger:this.logger}),o=[];if(e.length>0){this.logger?.progress(`Converting ${e.length} ValueSets to TypeSchema`);let n=0,s=0;for(let c of e)try{let a=await Se(r,c,t);a&&(o.push(a),n++,this.logger?.debug(`Converted ValueSet: ${c.name||c.id}`));}catch(a){s++,this.logger?.warn(`Failed to convert ValueSet ${c.name||c.id}: ${a instanceof Error?a.message:String(a)}`);}this.logger?.success(`ValueSet conversion completed: ${n}/${e.length} successful, ${s} failed`);}return o}async generateFromPackage(e,t,r){let o={name:e,version:t||"latest"},n=await this.registerFromPackageMetas([o]),s=await this.generateValueSetSchemas(n.allVs(),r);return [...(await Promise.all(n.allFs().map(async l=>await be(n,l,r)))).flat(),...s]}};var ie="Use CodeableReference which is not provided by FHIR R4.",wr="Use Availability which is not provided by FHIR R4.",Bt={"hl7.fhir.uv.extensions.r4#1.0.0":{"http://hl7.org/fhir/StructureDefinition/extended-contact-availability":wr,"http://hl7.org/fhir/StructureDefinition/immunization-procedure":ie,"http://hl7.org/fhir/StructureDefinition/specimen-additive":ie,"http://hl7.org/fhir/StructureDefinition/workflow-barrier":ie,"http://hl7.org/fhir/StructureDefinition/workflow-protectiveFactor":ie,"http://hl7.org/fhir/StructureDefinition/workflow-reason":ie},"hl7.fhir.r5.core#5.0.0":{"http://hl7.org/fhir/StructureDefinition/shareablecodesystem":"FIXME: CodeSystem.concept.concept defined by ElementReference. FHIR Schema generator output broken value in it, so we just skip it for now."}},_t=async(i,e)=>{let t=[];for(let r of i.allFs()){let o=v(r.package_meta);if(Bt[o]?.[r.url]){e?.dry_warn(`Skip ${r.url} from ${o}. Reason: ${Bt[o]?.[r.url]}`);continue}t.push(...await be(i,r,e));}for(let r of i.allVs())t.push(await Se(i,r));return t};var Er={boolean:"boolean",instant:"string",time:"string",date:"string",dateTime:"string",decimal:"number",integer:"number",unsignedInt:"number",positiveInt:"number",integer64:"number",base64Binary:"string",uri:"string",url:"string",canonical:"string",oid:"string",uuid:"string",string:"string",code:"string",markdown:"string",id:"string",xhtml:"string"},jt=i=>{let e=Er[i];if(e===void 0)throw new Error(`Unknown primitive type ${i}`);return e},Ut=i=>rt(i),ze=i=>i.kind==="profile"?`${T(i)}_profile`:P(i.name),Nr=i=>`${ze(i)}.ts`,Re=i=>`${Ut(i.package)}/${ze(i)}`,He=(i,e=true)=>{if(!i)return;let t=le(i,e);if(t)return Ge(t)},T=i=>{if(i.kind==="nested"){let e=i.url,t=He(e,false);if(!t)return "";let[r,o]=t.split("#"),n=J((o??"").split(".")).join("");return Ge([r,n].join(""))}return Ge(i.name)},At=new Set(["class","function","return","if","for","while","const","let","var","import","export","interface"]),X=i=>At.has(i)?`"${i}"`:i.includes(" ")||i.includes("-")?`"${i}"`:i,Ge=i=>(At.has(i)&&(i=`${i}_`),i.replace(/[- ]/g,"_")),W=(i,e)=>e.startsWith('"')?`${i}[${e}]`:`${i}.${e}`,Vt=i=>`(${i.map(e=>`"${e}"`).join(" | ")})`,xe=class extends L{tsImportType(e,...t){this.lineSM(`import type { ${t.join(", ")} } from "${e}"`);}generateFhirPackageIndexFile(e){this.cat("index.ts",()=>{let t=e.flatMap(r=>[{identifier:r.identifier,tsPackageName:ze(r.identifier),resourceName:T(r.identifier),nestedTypes:E(r)&&r.nested||B(r)&&r.nested?r.nested.map(o=>T(o.identifier)):[],helpers:E(r)||B(r)?[`is${T(r.identifier)}`]:[]}]).sort((r,o)=>r.resourceName.localeCompare(o.resourceName));t=Array.from(new Map(t.map(r=>[r.resourceName.toLowerCase(),r])).values()).sort((r,o)=>r.resourceName.localeCompare(o.resourceName));for(let r of t)this.debugComment(r.identifier),this.lineSM(`export type { ${[r.resourceName,...r.nestedTypes].join(", ")} } from "./${r.tsPackageName}"`),r.helpers.length>0&&this.lineSM(`export { ${r.helpers.join(", ")} } from "./${r.tsPackageName}"`);});}generateDependenciesImports(e,t){if(t.dependencies){let r=[],o=[];for(let n of t.dependencies)if(["complex-type","resource","logical"].includes(n.kind))r.push({tsPackage:`../${Re(n)}`,name:w(n.name),dep:n});else if(M(n)){let s={...n};s.name=He(n.url),r.push({tsPackage:`../${Re(s)}`,name:T(n),dep:n});}else o.push(n);r.sort((n,s)=>n.name.localeCompare(s.name));for(let n of r)this.debugComment(n.dep),this.tsImportType(n.tsPackage,n.name);for(let n of o)this.debugComment("skip:",n);if(this.line(),this.withPrimitiveTypeExtension(t)&&t.identifier.name!=="Element"&&t.dependencies.find(n=>n.name==="Element")===void 0){let n="http://hl7.org/fhir/StructureDefinition/Element",s=e.resolveByUrl(t.identifier.package,n);if(!s)throw new Error(`'${n}' not found for ${t.identifier.package}.`);this.tsImportType(`../${Re(s.identifier)}`,"Element");}}}generateComplexTypeReexports(e){let t=e.dependencies?.filter(ot).map(r=>({tsPackage:`../${Re(r)}`,name:w(r.name)}));if(t&&t.length>0){for(let r of t)this.lineSM(`export type { ${r.name} } from "${r.tsPackage}"`);this.line();}}addFieldExtension(e){let t=X(`_${e}`);this.lineSM(`${t}?: Element`);}generateType(e,t){let r;t.identifier.name==="Reference"?r="Reference<T extends string = string>":(t.identifier.kind,r=T(t.identifier));let o;if(t.base&&(o=`extends ${He(t.base.url)}`),this.debugComment(t.identifier),!t.fields&&!o&&!E(t)){this.lineSM(`export type ${r} = object`);return}this.curlyBlock(["export","interface",r,o],()=>{if(E(t)||B(t)){let s=[t.identifier];s.push(...e.resourceChildren(t.identifier));let c=this.opts.openResourceTypeSet&&s.length>1?" | string":"";this.lineSM(`resourceType: ${s.sort((a,l)=>a.name.localeCompare(l.name)).map(a=>`"${a.name}"`).join(" | ")}${c}`),this.line();}if(!t.fields)return;let n=Object.entries(t.fields).sort((s,c)=>s[0].localeCompare(c[0]));for(let[s,c]of n){if(H(c))continue;this.debugComment(s,":",c);let a=X(s),l;c.enum?l=Vt(c.enum):t.identifier.name==="Reference"&&a==="reference"?l="`${T}/${string}`":c.reference&&c.reference.length>0?l=`Reference<${c.reference.map(d=>`"${d.name}"`).join(" | ")}>`:Q(c.type)?l=jt(c.type.name):M(c.type)?l=T(c.type):l=c.type.name;let p=c.required?"":"?",m=c.array?"[]":"";this.lineSM(`${a}${p}: ${l}${m}`),this.withPrimitiveTypeExtension(t)&&Q(c.type)&&this.addFieldExtension(s);}});}withPrimitiveTypeExtension(e){if(!this.opts.primitiveTypeExtension||!A(e))return false;for(let t of Object.values(e.fields??{}))if(!H(t)&&Q(t.type))return true;return false}generateResourceTypePredicate(e){if(!E(e)&&!B(e))return;let t=T(e.identifier);this.curlyBlock(["export","const",`is${t}`,"=",`(resource: unknown): resource is ${t}`,"=>"],()=>{this.lineSM(`return resource !== null && typeof resource === "object" && (resource as {resourceType: string}).resourceType === "${e.identifier.name}"`);});}generateNestedTypes(e,t){if(t.nested)for(let r of t.nested)this.generateType(e,r),this.line();}generateProfileType(e,t){this.debugComment("flatProfile",t);let r=T(t.identifier);this.debugComment("identifier",t.identifier),this.debugComment("base",t.base),this.curlyBlock(["export","interface",r],()=>{this.lineSM(`__profileUrl: "${t.identifier.url}"`),this.line();for(let[o,n]of Object.entries(t.fields??{})){if(H(n))continue;this.debugComment(o,n);let s=X(o),c;if(n.enum)c=Vt(n.enum);else if(n.reference&&n.reference.length>0){let a=e.findLastSpecialization(t);if(!A(a))throw new Error(`Invalid specialization for ${t.identifier}`);let l=a.fields?.[o];if(l===void 0||H(l)||l.reference===void 0)throw new Error(`Invalid field declaration for ${o}`);let p=l.reference.map(y=>y.name),m=n.reference.map(y=>{let d=e.findLastSpecializationByIdentifier(y);return d.name!==y.name?`"${d.name}" /*${y.name}*/`:`'${y.name}'`}).join(" | ");p.length===1&&p[0]==="Resource"&&m!=='"Resource"'?c=`Reference<"Resource" /* ${m} */ >`:c=`Reference<${m}>`;}else if(M(n.type))c=T(n.type);else if(Q(n.type))c=jt(n.type.name);else {if(n.type===void 0)throw new Error(`Undefined type for '${o}' field at ${nt(t)}`);c=n.type.name;}this.lineSM(`${s}${n.required?"":"?"}: ${c}${n.array?"[]":""}`);}}),this.line();}generateAttachProfile(e){let t=T(e.base),r=T(e.identifier),o=Object.entries(e.fields||{}).filter(([n,s])=>s&&Z(s)&&s.type!==void 0).map(([n])=>X(n));this.curlyBlock([`export const attach_${r}_to_${t} =`,`(resource: ${t}, profile: ${r}): ${t}`,"=>"],()=>{this.curlyBlock(["return"],()=>{this.line("...resource,"),this.curlyBlock(["meta:"],()=>{this.line(`profile: ['${e.identifier.url}']`);},[","]),o.forEach(n=>{this.line(`${n}: ${W("profile",n)},`);});});}),this.line();}generateExtractProfile(e,t){let r=T(t.base),o=T(t.identifier),n=Object.entries(t.fields||{}).filter(([a,l])=>Z(l)&&l.type!==void 0).map(([a])=>a),s=e.findLastSpecialization(t);if(!A(s))throw new Error(`Specialization not found for ${t.identifier.url}`);let c={};this.curlyBlock([`export const extract_${o}_from_${r} =`,`(resource: ${r}): ${o}`,"=>"],()=>{n.forEach(a=>{let l=X(a),p=t.fields?.[a],m=s.fields?.[a];if(!Z(p)||!Z(m))return;p.required&&!m.required&&this.curlyBlock([`if (${W("resource",l)} === undefined)`],()=>this.lineSM(`throw new Error("'${l}' is required for ${t.identifier.url}")`));let y=p?.reference?.map(f=>f.name),d=m?.reference?.map(f=>f.name);if(y&&d&&y.length!==d.length){let f=`reference_is_valid_${l}`;this.curlyBlock(["const",f,"=","(ref?: Reference)","=>"],()=>{this.line("return !ref"),this.indentBlock(()=>{d.forEach(u=>{this.line(`|| ref.reference?.startsWith('${u}/')`);}),this.line(";");});});let h=p?.required?"":`!${W("resource",l)} || `;p.array?h+=`${W("resource",l)}.every( (ref) => ${f}(ref) )`:h+=`!${f}(${W("resource",l)})`,this.curlyBlock(["if (",h,")"],()=>{this.lineSM(`throw new Error("'${a}' has different references in profile and specialization")`);}),this.line(),c[a]=true;}}),this.curlyBlock(["return"],()=>{this.line(`__profileUrl: '${t.identifier.url}',`),n.forEach(a=>{let l=X(a);c[a]?this.line(`${l}:`,`${W("resource",l)} as ${o}['${l}'],`):this.line(`${l}:`,`${W("resource",l)},`);});});});}generateResourceModule(e,t){this.cat(`${Nr(t.identifier)}`,()=>{if(this.generateDisclaimer(),["complex-type","resource","logical"].includes(t.identifier.kind))this.generateDependenciesImports(e,t),this.generateComplexTypeReexports(t),this.generateNestedTypes(e,t),this.comment("CanonicalURL:",t.identifier.url),this.generateType(e,t),this.generateResourceTypePredicate(t);else if(de(t)){let r=e.flatProfile(t);this.generateDependenciesImports(e,r),this.comment("CanonicalURL:",t.identifier.url),this.generateProfileType(e,r),this.generateAttachProfile(r),this.generateExtractProfile(e,r);}else throw new Error(`Profile generation not implemented for kind: ${t.identifier.kind}`)});}async generate(e){let t=[...e.collectComplexTypes(),...e.collectResources(),...e.collectLogicalModels(),...this.opts.generateProfile?e.collectProfiles().filter(o=>e.isWithMetaField(o)):[]],r=z(t);this.cd("/",()=>{for(let[o,n]of Object.entries(r)){let s=Ut(o);this.cd(s,()=>{for(let c of n)this.generateResourceModule(e,c);this.generateFhirPackageIndexFile(n);});}});}};var Ht=i=>{let e=i.replace(/[^a-zA-Z0-9\-_.@#()]/g,"");return e.length===0?"unknown":e},Or=async(i,e)=>{e.info("Cleaning outputs...");try{e.info(`Clean ${i.outputDir}`),R.rmSync(i.outputDir,{recursive:!0,force:!0}),i.typeSchemaOutputDir&&(e.info(`Clean ${i.typeSchemaOutputDir}`),R.rmSync(i.typeSchemaOutputDir,{recursive:!0,force:!0})),i.exportTypeTree&&(e.info(`Clean ${i.exportTypeTree}`),R.rmSync(i.exportTypeTree,{recursive:!0,force:!0}));}catch(t){e.warn(`Error cleaning output directory: ${t instanceof Error?t.message:String(t)}`);}},Lr=async(i,e,t)=>{await _.mkdir(e,{recursive:true}),t.info(`Writing TypeSchema files to ${e}/...`);let r={};for(let o of i){let n={name:o.identifier.package,version:o.identifier.version},s=Ht(v(n)),c=Ht(`${o.identifier.name}(${le(o.identifier.url)})`),a=JSON.stringify(o,null,2),l=I.join(e,s,c);r[l]||(r[l]=[]),r[l]?.some(p=>p===a)||r[l].push(a);}for(let[o,n]of Object.entries(r))await Promise.all(n.map(async(s,c)=>{let a;c===0?a=`${o}.typeschema.json`:a=`${o}-${c}.typeschema.json`,await _.mkdir(I.dirname(a),{recursive:true}),await _.writeFile(a,s);}));},Mr=async(i,e,t)=>{t.info(`Writing TypeSchema files to: ${e}`),await _.mkdir(I.dirname(e),{recursive:true}),t.info(`Writing TypeSchemas to one file ${e}...`);for(let r of i){let o=JSON.stringify(r,null,2);await _.appendFile(e,`${o}
|
|
7
|
+
`);}},Br=async(i,e,t)=>{if(e.typeSchemaOutputDir)try{I.extname(e.typeSchemaOutputDir)===".ndjson"?await Mr(i,e.typeSchemaOutputDir,t):await Lr(i,e.typeSchemaOutputDir,t),t.info("Writing TypeSchema - DONE");}catch(r){if(t.error("Failed to write TypeSchema output",r instanceof Error?r:new Error(String(r))),e.throwException)throw r}},ne=class{schemas=[];options;generators=new Map;logger;packages=[];localStructurePackages=[];localTgzArchives=[];progressCallback;typeSchemaConfig;constructor(e={}){this.options={outputDir:e.outputDir||"./generated",overwrite:e.overwrite??true,cache:e.cache??true,cleanOutput:e.cleanOutput??true,typeSchemaConfig:e.typeSchemaConfig,manager:e.manager||null,throwException:e.throwException||false,typeSchemaOutputDir:e.typeSchemaOutputDir,exportTypeTree:e.exportTypeTree,treeShake:e.treeShake,registry:e.registry},this.typeSchemaConfig=e.typeSchemaConfig,this.logger=e.logger||O({prefix:"API",level:e.logLevel});}fromPackage(e,t){let r=pe({name:e,version:t||"latest"});return this.packages.push(r),this}fromPackageRef(e){return this.packages.push(e),this}registry(e){return this.options.registry=e,this}localStructureDefinitions(e){return this.localStructurePackages.push(e),this}localTgzPackage(e){return this.localTgzArchives.push(I.resolve(e)),this}fromSchemas(e){return this.logger.debug(`Adding ${e.length} TypeSchemas to generation`),this.schemas=[...this.schemas,...e],this}typescript(e){let o={...{...{logger:this.logger,outputDir:I.join(this.options.outputDir,"/types"),tabSize:4,withDebugComment:false,commentLinePrefix:"//",generateProfile:true},openResourceTypeSet:false,primitiveTypeExtension:true},...Object.fromEntries(Object.entries(e).filter(([s,c])=>c!==void 0))},n=new xe(o);return this.generators.set("typescript",n),this.logger.debug(`Configured TypeScript generator (${JSON.stringify(o,void 0,2)})`),this}python(e){let o={...{...{logger:this.logger,outputDir:this.options.outputDir,tabSize:4,withDebugComment:false,commentLinePrefix:"#"},rootPackageName:"fhir_types",fieldFormat:"snake_case"},...Object.fromEntries(Object.entries(e).filter(([s,c])=>c!==void 0))},n=new ke(o);return this.generators.set("python",n),this.logger.debug("Configured python generator"),this}csharp(e,t){let r=new ue({outputDir:I.join(this.options.outputDir,"/types"),staticSourceDir:t??void 0,targetNamespace:e,logger:new q({prefix:"C#",timestamp:true,suppressLoggingLevel:[]})});return this.generators.set("C#",r),this.logger.debug("Configured C# generator"),this}onProgress(e){return this.progressCallback=e,this}outputTo(e){this.logger.debug(`Setting output directory: ${e}`),this.options.outputDir=e;for(let t of this.generators.values())t.setOutputDir(e);return this}setLogLevel(e){return this.logger?.setLevel(e),this}throwException(e=true){return this.options.throwException=e,this}cleanOutput(e=true){return this.options.cleanOutput=e,this}writeTypeTree(e){return this.options.exportTypeTree=e,this}treeShake(e){return this.options.treeShake=e,this}writeTypeSchemas(e){return this.options.typeSchemaOutputDir=e,this}async generate(){let e=performance.now(),t={success:false,outputDir:this.options.outputDir,filesGenerated:[],errors:[],warnings:[],duration:0};this.logger.debug(`Starting generation with ${this.generators.size} generators`);try{this.options.cleanOutput&&Or(this.options,this.logger),this.logger.info("Initialize Canonical Manager");let r=this.options.manager||CanonicalManager({packages:this.packages,workingDir:".codegen-cache/canonical-manager-cache",registry:this.options.registry||void 0});if(this.localStructurePackages.length>0)for(let p of this.localStructurePackages)this.logger.info(`Registering local StructureDefinitions for ${p.package.name}@${p.package.version}`),await r.addLocalPackage({name:p.package.name,version:p.package.version,path:p.path,dependencies:p.dependencies?.map(m=>pe(m))});for(let p of this.localTgzArchives)this.logger.info(`Registering local tgz package: ${p}`),await r.addTgzPackage({archivePath:p});let o=await r.init(),n=Object.values(o),s=await te(r,{logger:this.logger,focusedPackages:n}),c=await _t(s,this.logger);await Br(c,this.options,this.logger);let a={resolutionTree:s.resolutionTree(),logger:this.logger},l=je(c,a);this.options.treeShake&&(l=Ft(l,this.options.treeShake,a)),this.options.exportTypeTree&&await l.exportTree(this.options.exportTypeTree),this.logger.debug(`Executing ${this.generators.size} generators`),await this.executeGenerators(t,l),this.logger.info("Generation completed successfully"),t.success=t.errors.length===0,this.logger.debug(`Generation completed: ${t.filesGenerated.length} files`);}catch(r){if(this.logger.error("Code generation failed",r instanceof Error?r:new Error(String(r))),t.errors.push(r instanceof Error?r.message:String(r)),this.options.throwException)throw r}return {...t,success:t.errors.length===0,duration:performance.now()-e}}reset(){return this.schemas=[],this.generators.clear(),this.progressCallback=void 0,this.packages=[],this.localStructurePackages=[],this.localTgzArchives=[],this}getSchemas(){return [...this.schemas]}getGenerators(){return Array.from(this.generators.keys())}async executeGenerators(e,t){for(let[r,o]of this.generators.entries()){this.logger.info(`Generating ${r}...`);try{await o.generate(t);let n=o.writtenFiles();e.filesGenerated.push(...n.map(s=>s.absPath)),this.logger.info(`Generating ${r} finished successfully`);}catch(n){if(e.errors.push(`${r} generator failed: ${n instanceof Error?n.message:String(n)}`),this.options.throwException)throw n}}}};var We={outputDir:"./generated",verbose:false,overwrite:true,validate:true,cache:true,cleanOutput:true,registry:"",typescript:{moduleFormat:"esm",generateIndex:true,includeDocuments:false,namingConvention:"PascalCase",strictMode:true,includeProfiles:true,includeExtensions:false,includeCodeSystems:false,includeOperations:false,generateValueSets:false,valueSetDirectory:"valuesets",valueSetMode:"required-only",valueSetStrengths:["required"],includeValueSetHelpers:false,fhirVersion:"R4",resourceTypes:[],maxDepth:10,profileOptions:{generateKind:"interface",includeConstraints:true,includeDocumentation:true,strictMode:false,subfolder:"profiles"},generateBuilders:false,builderOptions:{includeValidation:true,includeFactoryMethods:true,includeInterfaces:true,generateNestedBuilders:true,includeHelperMethods:true,supportPartialBuild:true,includeJSDoc:true,generateFactories:true,includeTypeGuards:true,handleChoiceTypes:true,generateArrayHelpers:true},validatorOptions:{includeCardinality:true,includeTypes:true,includeConstraints:true,includeInvariants:false,validateRequired:true,allowAdditional:false,strictValidation:false,collectMetrics:false,generateAssertions:true,generatePartialValidators:true,optimizePerformance:true,includeJSDoc:true,generateCompositeValidators:true},guardOptions:{includeRuntimeValidation:true,includeErrorMessages:true,treeShakeable:true,targetTSVersion:"5.0",strictGuards:false,includeNullChecks:true,verbose:false}},typeSchema:{enablePersistence:true,cacheDir:".typeschema-cache",maxAge:1440*60*1e3,validateCached:true,forceRegenerate:false,shareCache:true,cacheKeyPrefix:"",treeshake:[],singleFile:false,profiles:{autoDetect:true}},packages:[],files:[],$schema:""},Ie=["atomic-codegen.config.ts","atomic-codegen.config","atomic-codegen.config.json",".atomic-codegenrc","atomic-codegen.json",".atomic-codegen.json","codegen.config.json","codegen.json"],qe=class{validate(e){let t={valid:true,errors:[],warnings:[]};if(!e||typeof e!="object")return t.valid=false,t.errors.push({path:"root",message:"Configuration must be an object",value:e}),t;let r=e;r.outputDir!==void 0&&typeof r.outputDir!="string"&&t.errors.push({path:"outputDir",message:"outputDir must be a string",value:r.outputDir});let o=["verbose","overwrite","validate","cache"];for(let n of o)r[n]!==void 0&&typeof r[n]!="boolean"&&t.errors.push({path:n,message:`${n} must be a boolean`,value:r[n]});if(r.typescript!==void 0){let n=this.validateTypeScriptConfig(r.typescript);t.errors.push(...n);}if(r.typeSchema!==void 0){let n=this.validateTypeSchemaConfig(r.typeSchema);t.errors.push(...n);}return r.packages!==void 0&&(Array.isArray(r.packages)?r.packages.forEach((n,s)=>{typeof n!="string"&&t.errors.push({path:`packages[${s}]`,message:"package name must be a string",value:n});}):t.errors.push({path:"packages",message:"packages must be an array",value:r.packages})),r.files!==void 0&&(Array.isArray(r.files)?r.files.forEach((n,s)=>{typeof n!="string"&&t.errors.push({path:`files[${s}]`,message:"file path must be a string",value:n});}):t.errors.push({path:"files",message:"files must be an array",value:r.files})),t.valid=t.errors.length===0,t.valid&&(t.config=r),t}validateTypeScriptConfig(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript",message:"typescript config must be an object",value:e}),t;let r=e;r.moduleFormat!==void 0&&(["esm","cjs"].includes(r.moduleFormat)||t.push({path:"typescript.moduleFormat",message:'moduleFormat must be "esm" or "cjs"',value:r.moduleFormat})),r.namingConvention!==void 0&&(["PascalCase","camelCase"].includes(r.namingConvention)||t.push({path:"typescript.namingConvention",message:'namingConvention must be "PascalCase" or "camelCase"',value:r.namingConvention}));let o=["generateIndex","includeDocuments","strictMode","includeProfiles","includeExtensions","includeCodeSystems","includeOperations","generateValueSets","includeValueSetHelpers"];for(let n of o)r[n]!==void 0&&typeof r[n]!="boolean"&&t.push({path:`typescript.${n}`,message:`${n} must be a boolean`,value:r[n]});if(r.validatorOptions!==void 0){let n=this.validateValidatorOptions(r.validatorOptions);t.push(...n);}if(r.guardOptions!==void 0){let n=this.validateGuardOptions(r.guardOptions);t.push(...n);}if(r.profileOptions!==void 0){let n=this.validateProfileOptions(r.profileOptions);t.push(...n);}return t}validateValidatorOptions(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript.validatorOptions",message:"validatorOptions must be an object",value:e}),t;let r=e,o=["includeCardinality","includeTypes","includeConstraints","includeInvariants","validateRequired","allowAdditional","strictValidation","collectMetrics","generateAssertions","generatePartialValidators","optimizePerformance","includeJSDoc","generateCompositeValidators"];for(let n of o)r[n]!==void 0&&typeof r[n]!="boolean"&&t.push({path:`typescript.validatorOptions.${n}`,message:`${n} must be a boolean`,value:r[n]});return t}validateGuardOptions(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript.guardOptions",message:"guardOptions must be an object",value:e}),t;let r=e;r.targetTSVersion!==void 0&&(["3.8","4.0","4.5","5.0"].includes(r.targetTSVersion)||t.push({path:"typescript.guardOptions.targetTSVersion",message:'targetTSVersion must be one of: "3.8", "4.0", "4.5", "5.0"',value:r.targetTSVersion}));let o=["includeRuntimeValidation","includeErrorMessages","treeShakeable","strictGuards","includeNullChecks","verbose"];for(let n of o)r[n]!==void 0&&typeof r[n]!="boolean"&&t.push({path:`typescript.guardOptions.${n}`,message:`${n} must be a boolean`,value:r[n]});return t}validateProfileOptions(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript.profileOptions",message:"profileOptions must be an object",value:e}),t;let r=e;r.generateKind!==void 0&&(["interface","type","both"].includes(r.generateKind)||t.push({path:"typescript.profileOptions.generateKind",message:'generateKind must be "interface", "type", or "both"',value:r.generateKind})),r.subfolder!==void 0&&typeof r.subfolder!="string"&&t.push({path:"typescript.profileOptions.subfolder",message:"subfolder must be a string",value:r.subfolder});let o=["includeConstraints","includeDocumentation","strictMode"];for(let n of o)r[n]!==void 0&&typeof r[n]!="boolean"&&t.push({path:`typescript.profileOptions.${n}`,message:`${n} must be a boolean`,value:r[n]});return t}validateTypeSchemaConfig(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typeSchema",message:"typeSchema config must be an object",value:e}),t;let r=e,o=["enablePersistence","validateCached","forceRegenerate","shareCache"];for(let s of o)r[s]!==void 0&&typeof r[s]!="boolean"&&t.push({path:`typeSchema.${s}`,message:`${s} must be a boolean`,value:r[s]});let n=["cacheDir","cacheKeyPrefix"];for(let s of n)r[s]!==void 0&&typeof r[s]!="string"&&t.push({path:`typeSchema.${s}`,message:`${s} must be a string`,value:r[s]});if(r.maxAge!==void 0&&(typeof r.maxAge!="number"||r.maxAge<=0)&&t.push({path:"typeSchema.maxAge",message:"maxAge must be a positive number",value:r.maxAge}),r.profiles!==void 0)if(typeof r.profiles!="object"||r.profiles===null)t.push({path:"typeSchema.profiles",message:"profiles must be an object",value:r.profiles});else {let s=r.profiles;s.autoDetect!==void 0&&typeof s.autoDetect!="boolean"&&t.push({path:"typeSchema.profiles.autoDetect",message:"autoDetect must be a boolean",value:s.autoDetect});}return t}},Ke=class{validator=new qe;async autoload(e=process.cwd()){let t=await this.findConfigFile(e);return t?this.loadFromFile(t):{...We}}async loadFromFile(e){try{let t;if(e.endsWith(".ts")||e.endsWith("")){let n=await import(resolve(e));t=n.default||n;}else {let o=await readFile(e,"utf-8");t=JSON.parse(o);}let r=this.validator.validate(t);if(!r.valid){let o=r.errors.map(n=>`${n.path}: ${n.message}`).join(`
|
|
8
8
|
`);throw new Error(`Configuration validation failed:
|
|
9
|
-
${o}`)}if(!
|
|
9
|
+
${o}`)}if(!r.config)throw new Error("Invalid configuration");return this.mergeWithDefaults(r.config)}catch(t){throw t instanceof Error?new Error(`Failed to load config from ${e}: ${t.message}`):t}}async findConfigFile(e){for(let t of Ie){let r=resolve(e,t);if(existsSync(r))return r}return null}mergeWithDefaults(e){return {...We,...e,typescript:{...We.typescript,...e.typescript}}}},Ur=new Ke;async function Pe(i){return Ur.autoload(i)}var zt={command:"generate",describe:"Generate code based on configuration file settings",builder:i=>i.option("verbose",{alias:"v",type:"boolean",default:false,description:"Enable verbose output"}).example("$0 generate","Generate code using settings from config file").example("$0 generate --verbose","Generate with verbose output"),handler:async i=>{if(i._.length>1){let s=i._.slice(1).join(" ");F(`Invalid syntax: 'atomic-codegen generate ${s}'
|
|
10
10
|
|
|
11
11
|
The CLI has been simplified and no longer uses subcommands.
|
|
12
12
|
|
|
@@ -14,8 +14,8 @@ The CLI has been simplified and no longer uses subcommands.
|
|
|
14
14
|
\u274C Old: atomic-codegen generate typescript
|
|
15
15
|
|
|
16
16
|
All generation settings are now configured in your config file.
|
|
17
|
-
Create an atomic-codegen.config.ts file to get started.`),process.exit(1);}let e=process.cwd(),t=await
|
|
18
|
-
`);
|
|
17
|
+
Create an atomic-codegen.config.ts file to get started.`),process.exit(1);}let e=process.cwd(),t=await Gr(e);if(!t){let s=Ie.map(c=>` - ${c}`).join(`
|
|
18
|
+
`);F(`No configuration file found. Please create one of the following files in your project root:
|
|
19
19
|
${s}
|
|
20
20
|
|
|
21
21
|
Example atomic-codegen.config.ts:
|
|
@@ -27,13 +27,13 @@ export default defineConfig({
|
|
|
27
27
|
typescript: {
|
|
28
28
|
generateIndex: true
|
|
29
29
|
}
|
|
30
|
-
});`),process.exit(1);}let
|
|
31
|
-
`),await writeFile(p,m,"utf-8");let y=Date.now()-c;if(
|
|
30
|
+
});`),process.exit(1);}let r=await Pe(e),o=i.verbose??r.verbose??false,n=O({prefix:"Generate"});try{Qe("Starting generation from config"),o&&(n.info(`Config file: ${t}`),n.info(`Output directory: ${r.outputDir||"./generated"}`),n.info(`Packages: ${r.packages?.length||0}`),n.info(`Files: ${r.files?.length||0}`),n.info(`TypeScript generation: ${r.typescript?"enabled":"disabled"}`));let s=new ne({outputDir:r.outputDir||"./generated",overwrite:r.overwrite??!0,cache:r.cache??!0,typeSchemaConfig:r.typeSchema,logger:n});if(r.packages&&r.packages.length>0){n.info(`Loading packages from config: ${r.packages.join(", ")}`);for(let a of r.packages){let[l,p]=a.includes("@")?a.split("@"):[a,void 0];s.fromPackage(l,p);}}else throw r.files&&r.files.length>0?(n.info(`Loading files from config: ${r.files.join(", ")}`),new Error("Not Implemented")):new Error("No data source specified in config. Please configure 'packages' or 'files' in your config file.");if(r.typescript)throw new Error("Not Implemented");if(!r.typescript)throw new Error("No generators configured. Please enable 'typescript' in your config file.");o&&s.onProgress((a,l,p,m)=>{let y=Math.round(l/p*100);n.progress(`[${a}] ${y}% - ${m||"Processing..."}`);}),n.step("Executing generation...");let c=await s.generate();if(c.success){if($e(`Generated ${c.filesGenerated.length} files in ${c.duration.toFixed(2)}ms`),n.dim(`Output directory: ${c.outputDir}`),c.warnings.length>0)for(let a of c.warnings)Xe(a);}else {F(`Generation failed with ${c.errors.length} errors`);for(let a of c.errors)n.dim(` ${a}`);process.exit(1);}}catch(s){F("Generation failed with unexpected error",s instanceof Error?s:new Error(String(s))),process.exit(1);}}};async function Gr(i){for(let e of Ie){let t=resolve(i,e);if(existsSync(t))return t}return null}var Wt={command:"generate <packages..>",describe:"Generate TypeSchema files from FHIR packages",builder:{packages:{type:"string",array:true,demandOption:true,describe:"FHIR packages to process (e.g., hl7.fhir.r4.core@4.0.1)"},output:{alias:"o",type:"string",describe:"Output file or directory",default:"./schemas.ndjson"},format:{alias:"f",type:"string",choices:["ndjson","json"],default:"ndjson",describe:"Output format for TypeSchema files"},treeshake:{alias:"t",type:"string",array:true,describe:"Only generate TypeSchemas for specific ResourceTypes (treeshaking)"},singleFile:{alias:"s",type:"boolean",default:false,describe:"Generate single TypeSchema file instead of multiple files (NDJSON format)"},verbose:{alias:"v",type:"boolean",default:false,describe:"Enable verbose output"},registry:{alias:"r",type:"string",describe:"Custom FHIR package registry URL (default: https://fs.get-ig.org/pkgs/)"}},handler:async i=>{let e=O({prefix:"TypeSchema"});try{let t=await Pe(process.cwd());e.step("Generating TypeSchema from FHIR packages"),e.info(`Packages: ${i.packages.join(", ")}`),e.info(`Output: ${i.output}`);let r=i.singleFile!==void 0?i.singleFile:t.typeSchema?.singleFile??!1,o=r?"ndjson":i.format;e.debug(`Format: ${o}${r&&i.format==="json"?" (forced from json due to singleFile)":""}`);let n=i.treeshake&&i.treeshake.length>0?i.treeshake:t.typeSchema?.treeshake;n&&n.length>0&&e.info(`Treeshaking enabled for ResourceTypes: ${n.join(", ")}`),r&&e.info("Single file output enabled (NDJSON format)");let s=i.registry||t.registry;s&&e.info(`Using custom registry: ${s}`);let c=Date.now(),a=new re({treeshake:n,registry:s,manager:CanonicalManager({packages:[],workingDir:".codegen-cache/canonical-manager-cache",registry:s})}),l=[];for(let d of i.packages){let[f,h]=d.includes("@")?d.split("@"):[d,void 0];e.progress(`Processing package: ${f}${h?`@${h}`:""}`);let u=await a.generateFromPackage(f,h,e);l.push(...u);}if(l.length===0)throw new Error("No schemas were generated from the specified packages");let p=i.output;if(!p)throw new Error("Output format not specified");await mkdir(dirname(p),{recursive:!0});let m;o==="json"?m=JSON.stringify(l,null,2):m=l.map(d=>JSON.stringify(d)).join(`
|
|
31
|
+
`),await writeFile(p,m,"utf-8");let y=Date.now()-c;if(tt(`Generated ${l.length} TypeSchema definitions`,y,{schemas:l.length}),e.dim(`Output: ${p}`),i.verbose){e.debug("Generated schemas:");let d=l.map(f=>`${f.identifier?.name||"Unknown"} (${f.identifier?.kind||"unknown"})`);K(d);}}catch(t){e.error("Failed to generate TypeSchema",t instanceof Error?t:new Error(String(t))),process.exit(1);}}};var qt={command:"typeschema [subcommand]",describe:"TypeSchema operations - generate, validate and merge schemas",builder:i=>i.command(Wt).help().example("$0 typeschema generate hl7.fhir.r4.core@4.0.1","Generate TypeSchema from FHIR R4 core package"),handler:i=>{if(!i.subcommand&&i._.length===1){we("Available typeschema subcommands:"),K(["generate Generate TypeSchema files from FHIR packages"]),console.log(`
|
|
32
32
|
Use 'atomic-codegen typeschema <subcommand> --help' for more information about a subcommand.`),console.log(`
|
|
33
|
-
Examples:`),
|
|
34
|
-
`),
|
|
35
|
-
Use 'atomic-codegen typeschema <subcommand> --help' for more information about a subcommand.`),process.exit(1));}};function
|
|
33
|
+
Examples:`),K(["atomic-codegen typeschema generate hl7.fhir.r4.core@4.0.1 -o schemas.ndjson","atomic-codegen typeschema validate schemas.ndjson","atomic-codegen typeschema merge schema1.ndjson schema2.ndjson -o merged.ndjson"]);return}i.subcommand&&!["generate","validate","merge"].includes(i.subcommand)&&(F(`Unknown typeschema subcommand: ${i.subcommand}
|
|
34
|
+
`),we("Available typeschema subcommands:"),K(["generate Generate TypeSchema files from FHIR packages","validate Validate TypeSchema files for correctness and consistency","merge Merge multiple TypeSchema files into a single file"]),console.log(`
|
|
35
|
+
Use 'atomic-codegen typeschema <subcommand> --help' for more information about a subcommand.`),process.exit(1));}};function Xr(i){return i?{debug:0,info:1,warn:2,error:3,silent:4}[i.toLowerCase()]:void 0}async function Qr(i){let e=Xr(i.logLevel);e===void 0&&(i.debug||i.verbose?e=0:e=1),Ze({timestamp:i.debug,level:e});}function Zr(){return Jr(hideBin(process.argv)).scriptName("atomic-codegen").usage("$0 <command> [options]").middleware(Qr).command(qt).command(zt).option("verbose",{alias:"v",type:"boolean",description:"Enable verbose output",default:false,global:true}).option("debug",{alias:"d",type:"boolean",description:"Enable debug output with detailed logging",default:false,global:true}).option("log-level",{alias:"l",type:"string",choices:["debug","info","warn","error","silent"],description:"Set the log level (default: info)",global:true}).option("config",{alias:"c",type:"string",description:"Path to configuration file (.atomic-codegen.json by default)",global:true}).demandCommand(0).middleware(i=>{i._.length===0&&(et("Welcome to Atomic Codegen!"),console.log("Available commands:"),console.log(" typeschema Generate, validate and merge TypeSchema files"),console.log(" generate Generate code based on configuration file"),console.log(`
|
|
36
36
|
Use 'atomic-codegen <command> --help' for more information about a command.`),console.log(`
|
|
37
37
|
Quick examples:`),console.log(" atomic-codegen typeschema generate hl7.fhir.r4.core@4.0.1 -o schemas.ndjson"),console.log(" atomic-codegen generate # Uses atomic-codegen.config.ts"),console.log(`
|
|
38
|
-
Use 'atomic-codegen --help' to see all options.`),process.exit(0));}).help().version("0.1.0").example("$0 generate","Generate code using atomic-codegen.config.ts").example("$0 generate --verbose","Generate with detailed progress output").example("$0 --config custom-config.ts generate","Use custom configuration file").example("$0 typeschema generate hl7.fhir.r4.core@4.0.1 -o schemas.ndjson","Generate TypeSchemas from FHIR package").fail((
|
|
39
|
-
Use --help for usage information`),process.exit(1);}).wrap(Math.min(120,process.stdout.columns||80))}async function
|
|
38
|
+
Use 'atomic-codegen --help' to see all options.`),process.exit(0));}).help().version("0.1.0").example("$0 generate","Generate code using atomic-codegen.config.ts").example("$0 generate --verbose","Generate with detailed progress output").example("$0 --config custom-config.ts generate","Use custom configuration file").example("$0 typeschema generate hl7.fhir.r4.core@4.0.1 -o schemas.ndjson","Generate TypeSchemas from FHIR package").fail((i,e,t)=>{e?F(e.message,e):F(i),F(`
|
|
39
|
+
Use --help for usage information`),process.exit(1);}).wrap(Math.min(120,process.stdout.columns||80))}async function Je(){await Zr().parseAsync();}import.meta.main&&Je().catch(i=>{i("Unexpected error:",i),process.exit(1);});Je().catch(i=>{console.error("CLI Error:",i instanceof Error?i.message:i),process.exit(1);});
|
package/dist/index.d.ts
CHANGED
|
@@ -305,6 +305,26 @@ type TypeSchemaShakeRule = {
|
|
|
305
305
|
};
|
|
306
306
|
type TreeShake = Record<string, Record<string, TypeSchemaShakeRule>>;
|
|
307
307
|
|
|
308
|
+
type FileSystemWriterOptions = {
|
|
309
|
+
outputDir: string;
|
|
310
|
+
inMemoryOnly?: boolean;
|
|
311
|
+
logger?: CodegenLogger;
|
|
312
|
+
};
|
|
313
|
+
type WriterOptions = FileSystemWriterOptions & {
|
|
314
|
+
tabSize: number;
|
|
315
|
+
withDebugComment?: boolean;
|
|
316
|
+
commentLinePrefix: string;
|
|
317
|
+
generateProfile?: boolean;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
type StringFormatKey = "snake_case" | "PascalCase" | "camelCase";
|
|
321
|
+
interface PythonGeneratorOptions extends WriterOptions {
|
|
322
|
+
allowExtraFields?: boolean;
|
|
323
|
+
staticDir?: string;
|
|
324
|
+
rootPackageName: string;
|
|
325
|
+
fieldFormat: StringFormatKey;
|
|
326
|
+
}
|
|
327
|
+
|
|
308
328
|
/**
|
|
309
329
|
* New Config Schema for High-Level API
|
|
310
330
|
*
|
|
@@ -526,18 +546,6 @@ declare function isConfig(obj: unknown): obj is Config;
|
|
|
526
546
|
*/
|
|
527
547
|
declare function defineConfig(config: Config): Config;
|
|
528
548
|
|
|
529
|
-
type FileSystemWriterOptions = {
|
|
530
|
-
outputDir: string;
|
|
531
|
-
inMemoryOnly?: boolean;
|
|
532
|
-
logger?: CodegenLogger;
|
|
533
|
-
};
|
|
534
|
-
type WriterOptions = FileSystemWriterOptions & {
|
|
535
|
-
tabSize: number;
|
|
536
|
-
withDebugComment?: boolean;
|
|
537
|
-
commentLinePrefix: string;
|
|
538
|
-
generateProfile?: boolean;
|
|
539
|
-
};
|
|
540
|
-
|
|
541
549
|
type TypeScriptOptions = {
|
|
542
550
|
/** openResourceTypeSet -- for resource families (Resource, DomainResource) use open set for resourceType field.
|
|
543
551
|
*
|
|
@@ -623,6 +631,7 @@ declare class APIBuilder {
|
|
|
623
631
|
localTgzPackage(archivePath: string): APIBuilder;
|
|
624
632
|
fromSchemas(schemas: TypeSchema[]): APIBuilder;
|
|
625
633
|
typescript(userOpts: Partial<TypeScriptOptions>): this;
|
|
634
|
+
python(userOptions: Partial<PythonGeneratorOptions>): APIBuilder;
|
|
626
635
|
csharp(namespace: string, staticSourceDir?: string | undefined): APIBuilder;
|
|
627
636
|
/**
|
|
628
637
|
* Set a progress callback for monitoring generation
|